def upload_avatar():
u = g.user
if 'photo' in request.files:
form = request.form
filename = str(uuid3(NAMESPACE_DNS, str(u.id) + u.username + str(time.time()))).replace('-','')
try:
x = int(form['x'])
y = int(form['y'])
w = int(form['nw'])
h = int(form['nh'])
img = Image.open(request.files['photo'])
format = img.format
croped_img = crop_img(img, x, y, w, h)
filename = save_avatar(croped_img, filename, format)
url_path = current_app.config['UPLOADED_PHOTOS_URL']
old_name = u.avatar.split(url_path)[1]
remove_avatar(old_name)
u.avatar = url_path + filename
u.save()
except Exception as e:
print(e)
flash('????????2Mb?????', 'error')
return redirect(url_for('user.setting_view'))
python类NAMESPACE_DNS的实例源码
def randomUUIDField(self):
"""
Return the unique uuid from uuid1, uuid3, uuid4, or uuid5.
"""
uuid1 = uuid.uuid1().hex
uuid3 = uuid.uuid3(
uuid.NAMESPACE_URL,
self.randomize(['python', 'django', 'awesome'])
).hex
uuid4 = uuid.uuid4().hex
uuid5 = uuid.uuid5(
uuid.NAMESPACE_DNS,
self.randomize(['python', 'django', 'awesome'])
).hex
return self.randomize([uuid1, uuid3, uuid4, uuid5])
def setUp(self):
"""Setup test."""
from uuid import uuid5, NAMESPACE_DNS
class UUIDModel(db.Document):
uuid = db.UUIDField()
self.model_cls = UUIDModel
uuid = uuid5(NAMESPACE_DNS, "This is a test")
self.expected_data = {
"uuid": uuid
}
self.data = json.dumps({"uuid": str(uuid)})
self.hook = generate_object_hook(UUIDModel)
def setUp(self):
"""Setup class."""
from uuid import uuid5, NAMESPACE_DNS
self.encoder = GoodJSONEncoder()
self.data = uuid5(NAMESPACE_DNS, "This is a test")
self.expected = str(self.data)
def __init__(self, name, uuid=None):
if uuid is None:
self.uuid = uuid5(NAMESPACE_DNS, name).hex
else:
self.uuid = uuid
self.name = name
def test_create_timeuuid_with_uuid4_string_should_fail(self):
''' creating a TimeUUID with a hex uuid4 should fail'''
for i in range(1,100):
u = uuid.uuid4()
with self.assertRaises(ValueError) as cm:
t = timeuuid.TimeUUID(s=u.hex)
self.assertEqual(str(cm.exception), 'Invalid UUID type')
for fn in [uuid.uuid3, uuid.uuid5]:
for i in range(1,100):
u = fn(uuid.NAMESPACE_DNS,str(os.urandom(10)))
with self.assertRaises(ValueError) as cm:
t = timeuuid.TimeUUID(s=u.hex)
self.assertEqual(str(cm.exception), 'Invalid UUID type')