Commit d8de98a5 authored by Romain Loth's avatar Romain Loth

make image path a constant and handle rm old image after change

parent 5c6bdc51
......@@ -26,7 +26,7 @@ __status__ = "Dev"
# ============== imports ==============
from re import sub
from os import path
from os import path, remove
from json import dumps
from datetime import timedelta
from urllib.parse import unquote
......@@ -450,6 +450,10 @@ def profile():
the_id_to_delete = current_user.uid
mlog("INFO",
"executing DELETE scholar's data at the request of user %s" % str(the_id_to_delete))
# remove saved image if any
if current_user.info['pic_fname']:
remove(path.join(*tools.IMAGE_SAVING_POINT, current_user.info['pic_fname']))
logout_user()
dbcrud.rm_scholar(the_id_to_delete)
......@@ -460,6 +464,14 @@ def profile():
# input fields data ~> normalized {cols:values}
our_records = read_record_from_request(request)
# small gotcha: absence of the file input is the default in GUI
# (even when we keep the image)
if 'pic_fname' not in our_records and 'pic_fname' in current_user.info and current_user.info['pic_fname']:
our_records['pic_fname'] = current_user.info['pic_fname']
# POSS:
# a dedicated button to indicate that the pic should be rm
# special action CREATE for a new user already known to doors
if current_user.empty:
mlog("DEBUG",
......@@ -760,6 +772,16 @@ def save_form(clean_records, update_flag=False, previous_user_info=None):
return None
else:
dbcrud.save_full_scholar(clean_records, reg_db, update_user=previous_user_info)
# remove previous image from filesystem if changed
if (previous_user_info['pic_fname']
and ('pic_fname' in clean_records
and
previous_user_info['pic_fname'] != clean_records['pic_fname'])):
remove(path.join(*tools.IMAGE_SAVING_POINT, previous_user_info['pic_fname']))
else:
luid = int(dbcrud.save_full_scholar(clean_records, reg_db))
......@@ -858,7 +880,7 @@ def read_record_from_request(request):
if hasattr(request, "files") and 'pic_file' in request.files and request.files['pic_file']:
new_fname = tools.pic_blob_to_filename(request.files['pic_file'])
clean_records['pic_fname'] = new_fname
mlog("INFO", "new_fname", new_fname)
mlog("DEBUG", "new pic with fname", new_fname)
return clean_records
......
......@@ -45,6 +45,9 @@ CONFIGMENU = [
{"sec": 'content', "var":'HAPAX_THRESHOLD', "def": '1 ' }
]
IMAGE_SAVING_POINT = ['data', 'shared_user_img']
def home_path():
"""
returns ./../.. in any OS
......@@ -246,7 +249,7 @@ def format_err(err):
from uuid import uuid4
from imghdr import what # diagnoses filetype and returns ext
def pic_blob_to_filename(pic_blob, path_elts=['data', 'shared_user_img']):
def pic_blob_to_filename(pic_blob):
"""
Saves a pic blob, returns the relative path
......@@ -256,10 +259,13 @@ def pic_blob_to_filename(pic_blob, path_elts=['data', 'shared_user_img']):
output "12345.png"
+ saved in /data/shared_user_img/12345.png
"""
# our path starts with a working copy of the saving point
path_elts=list(IMAGE_SAVING_POINT)
# random 32 hex chars
filename = uuid4().hex
fileext = what(pic_blob.stream)
fbasename = filename+'.'+fileext
fbasename = str(filename)+'.'+str(fileext)
path_elts.append(fbasename)
new_img_relpath = path.join(*path_elts)
# save
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment