From 74362653195709b08d7ab90bcd4998594537740b Mon Sep 17 00:00:00 2001 From: Jacky Date: Thu, 9 Feb 2017 21:28:52 +1100 Subject: [PATCH] A few small path changes/ fixed to use abs paths not relative paths, /static vs static, --- back_end/Imaging.py | 2 +- back_end/common.py | 2 +- back_end/profile.py | 1 + back_end/user.py | 24 ++++++++++++++++-------- templates/profile.html | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/back_end/Imaging.py b/back_end/Imaging.py index d88840d..ee3dd74 100644 --- a/back_end/Imaging.py +++ b/back_end/Imaging.py @@ -23,6 +23,7 @@ def is_image(img): def open_image(data, change_icon=False, b64=False): """Returns an image object, given image binary data (gotten from request.get_file) """ + if b64: try: with open('out.txt', 'w') as f: @@ -30,7 +31,6 @@ def open_image(data, change_icon=False, b64=False): data = base64.b64decode(data[22:] if data.startswith("data:image/png;base64,") else data) except binascii.Error: print("unable to decode b64!") - raise return INVALID_IMAGE io = BytesIO(data) diff --git a/back_end/common.py b/back_end/common.py index f549b21..f9608a8 100644 --- a/back_end/common.py +++ b/back_end/common.py @@ -57,7 +57,7 @@ def fetch_file(url, filename): #TODO: maybe move this to db_api instead? def get_user_picture(userObj): - return userObj.picture if userObj.picture is not None and userObj.picture != "" else "nouser.png" + return userObj.picture if userObj.picture is not None and userObj.picture != "" else os.path.join('/static', 'uploads', 'user_image', 'nouser.png') def reply_malformed(request, header_text="The request was malformed", text="Sorry!\n It looks like your client has sent a invalid request.\n Please reload the page or contact us."): """Sets the header of request to 400 (bad request) and writes the text supplied""" diff --git a/back_end/profile.py b/back_end/profile.py index e89395e..cddba01 100644 --- a/back_end/profile.py +++ b/back_end/profile.py @@ -8,6 +8,7 @@ def view_handler_post(request, username): def view_handler(request, username): user = db.User.find(username=username) print('on profile page, profile pic = ' + str(user.picture) + ' check if user logged in or user of profile page') + print(get_user_picture(user)) if user is None: request.write("username is not in db") else: diff --git a/back_end/user.py b/back_end/user.py index a39927d..f3cdc11 100644 --- a/back_end/user.py +++ b/back_end/user.py @@ -24,12 +24,20 @@ def signup_handler_post(request): if image_type == 'profile_img_webcam': # webcam print("reading from webcam") - print(request.get_field('webcam-input')) - image = Imaging.open_image(request.get_field('webcam-input'), change_icon=True, b64=True) + if request.get_field('webcam-input') is None: + image = None + else: + image = Imaging.open_image(request.get_field('webcam-input'), change_icon=True, b64=True) + elif image_type == 'profile_img_upload': # file upload print("Reading from file upload") - image = Imaging.open_image(request.get_file('profile_picture')[2], change_icon=True, b64=False) + if request.get_file('profile_picture') == (None, None, None): + # no upload + image = None + else: + image = Imaging.open_image(request.get_file('profile_picture')[2], change_icon=True, b64=False) + else: print("No file received! img_type=", image_type) image = None @@ -62,11 +70,11 @@ def signup_handler_post(request): elif Imaging.is_image(image): ext = image.type - - file_path = os.path.abspath(os.path.join('static', 'uploads', 'user_image', str(new_user.id) + '.' + ext)) - print("Saving img to", file_path) - image.img.save(file_path) - db.User.update_some(picture=file_path) + file_path = os.path.join('static', 'uploads', 'user_image', str(new_user.id) + '.' + ext) + abs_file_path = os.path.abspath(file_path) + print("Saving img to", abs_file_path, "with link", file_path) + image.img.save(abs_file_path) + db.User.update_some(picture='/' + file_path) # need a / to make it absolute path so it doesnt do /profile/static... request.set_secure_cookie("current_user", username) request.redirect('/') diff --git a/templates/profile.html b/templates/profile.html index bc56d25..6db66a9 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -17,7 +17,7 @@ {% end if %}

Bio

{{ user.bio }}
-

Picture: +

Picture: