(session,
user,
file,
file_name,
content_type,
extension=".jpg")
| 32 | |
| 33 | |
| 34 | def process_profile_image(session, |
| 35 | user, |
| 36 | file, |
| 37 | file_name, |
| 38 | content_type, |
| 39 | extension=".jpg"): |
| 40 | new_image = Image(original_filename=file_name) |
| 41 | session.add(new_image) |
| 42 | |
| 43 | try: |
| 44 | session.commit() |
| 45 | except: |
| 46 | session.rollback() |
| 47 | raise |
| 48 | |
| 49 | user.profile_image = new_image |
| 50 | |
| 51 | user.profile_image_blob = settings.USER_IMAGES_BASE_DIR + \ |
| 52 | str(user.id) + "/" + str(new_image.id) |
| 53 | |
| 54 | image = imread(file) |
| 55 | image = image[:, :, :3] # remove alpha channel |
| 56 | |
| 57 | if image is None: |
| 58 | raise IOError("Could not open") |
| 59 | |
| 60 | new_image = get_and_set_width_and_height(new_image, image) |
| 61 | |
| 62 | if new_image.height > 640 or new_image.width > 640: |
| 63 | ratio = min((640 / new_image.height), |
| 64 | (640 / new_image.width)) |
| 65 | |
| 66 | shape_x = int(round(new_image.width * ratio)) |
| 67 | shape_y = int(round(new_image.height * ratio)) |
| 68 | |
| 69 | image = imresize(image, |
| 70 | (shape_x, shape_y)) |
| 71 | new_image = get_and_set_width_and_height(new_image, image) |
| 72 | |
| 73 | blob_expiry_offset = 25920000 |
| 74 | user.profile_image_expiry = int(time.time() + blob_expiry_offset) # 10 months |
| 75 | |
| 76 | # Save File |
| 77 | temp = tempfile.mkdtemp() |
| 78 | new_temp_filename = f"{temp}/resized{str(extension)}" |
| 79 | imwrite(new_temp_filename, image) |
| 80 | |
| 81 | data_tools.upload_to_cloud_storage(temp_local_path = new_temp_filename, |
| 82 | blob_path = user.profile_image_blob, |
| 83 | content_type = "image/jpg") |
| 84 | |
| 85 | user.profile_image_url = data_tools.build_secure_url(blob_name = user.profile_image_blob, |
| 86 | expiration_offset = blob_expiry_offset) |
| 87 | |
| 88 | # Save Thumb |
| 89 | user.profile_image_thumb_blob = f"{user.profile_image_blob}_thumb" |
| 90 | |
| 91 | thumbnail_image = imresize(image, (80, 80)) |
no test coverage detected