refactor(usermanager): image from form extraction
This commit is contained in:
parent
76deba94f8
commit
997327338e
@ -22,9 +22,31 @@ def index():
|
|||||||
return render_template('usermanager/index.html')
|
return render_template('usermanager/index.html')
|
||||||
|
|
||||||
|
|
||||||
@bp.route("/user/<string:username>", methods=("GET", "POST"))
|
class InvalidImageException(Exception):
|
||||||
def user_detail(username: str):
|
"""Raised when an image's filename or contents are invalid."""
|
||||||
"""Detail view for a specific User."""
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def _get_image_from_uploaded_file(file) -> Image.Image:
|
||||||
|
"""Extracts a JPEG image from a file submitted via POST request.
|
||||||
|
|
||||||
|
The file's file extension and content is checked for validity as a JPEG image.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
file
|
||||||
|
A file object taken from a POST request.
|
||||||
|
|
||||||
|
Returns
|
||||||
|
-------
|
||||||
|
PIL.Image.Image
|
||||||
|
A valid JPEG Image object.
|
||||||
|
|
||||||
|
Raises
|
||||||
|
------
|
||||||
|
InvalidImageException
|
||||||
|
When the file's file extension or contents are not valid for a JPEG image.
|
||||||
|
"""
|
||||||
|
|
||||||
def _file_extension_is_valid(filename: str):
|
def _file_extension_is_valid(filename: str):
|
||||||
allowed_extensions = ["jpg", "jpeg"]
|
allowed_extensions = ["jpg", "jpeg"]
|
||||||
@ -34,6 +56,24 @@ def user_detail(username: str):
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
if not _file_extension_is_valid(file.filename):
|
||||||
|
raise InvalidImageException("Invalid file extension.")
|
||||||
|
|
||||||
|
with TemporaryDirectory() as tempdir:
|
||||||
|
path_to_file = Path(tempdir) / "upload.jpg"
|
||||||
|
file.save(path_to_file)
|
||||||
|
try:
|
||||||
|
return Image.open(path_to_file, formats=['JPEG'])
|
||||||
|
except UnidentifiedImageError:
|
||||||
|
raise InvalidImageException(
|
||||||
|
"Image is either not a JPEG, or its contents are corrupted."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.route("/user/<string:username>", methods=("GET", "POST"))
|
||||||
|
def user_detail(username: str):
|
||||||
|
"""Detail view for a specific User."""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
conn = ldap.get_connection()
|
conn = ldap.get_connection()
|
||||||
except Exception:
|
except Exception:
|
||||||
@ -77,32 +117,21 @@ def user_detail(username: str):
|
|||||||
if request.form['password']:
|
if request.form['password']:
|
||||||
user.password_hash = User.generate_password_hash(request.form['password'])
|
user.password_hash = User.generate_password_hash(request.form['password'])
|
||||||
|
|
||||||
picture_updated = False
|
new_picture = None
|
||||||
if 'picture' in request.files:
|
if 'picture' in request.files:
|
||||||
file = request.files['picture']
|
file = request.files['picture']
|
||||||
if len(file.filename):
|
if len(file.filename):
|
||||||
if _file_extension_is_valid(file.filename):
|
try:
|
||||||
with TemporaryDirectory() as tempdir:
|
new_picture = _get_image_from_uploaded_file(file)
|
||||||
path_to_file = Path(tempdir) / "upload.jpg"
|
user.picture = new_picture
|
||||||
file.save(path_to_file)
|
except InvalidImageException as e:
|
||||||
try:
|
flash(f"Invalid picture: {e}")
|
||||||
picture = Image.open(path_to_file, formats=['JPEG'])
|
|
||||||
user.picture = picture
|
|
||||||
picture_updated = True
|
|
||||||
except UnidentifiedImageError:
|
|
||||||
flash(
|
|
||||||
"There was a problem with the provided picture " \
|
|
||||||
"(it may be corrupted)."
|
|
||||||
)
|
|
||||||
form_is_valid = False
|
|
||||||
else:
|
|
||||||
flash("Invalid file extension for picture.")
|
|
||||||
form_is_valid = False
|
form_is_valid = False
|
||||||
|
|
||||||
if form_is_valid:
|
if form_is_valid:
|
||||||
ldap.update_user(conn, user)
|
ldap.update_user(conn, user)
|
||||||
flash("User information was updated!")
|
flash("User information was updated!")
|
||||||
if picture_updated:
|
if new_picture is not None:
|
||||||
ldap.get_user(conn, user.username)._generate_static_images(force=True)
|
ldap.get_user(conn, user.username)._generate_static_images(force=True)
|
||||||
|
|
||||||
conn.unbind()
|
conn.unbind()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user