|
| 1 | +from io import BytesIO |
| 2 | + |
| 3 | +from flask import Blueprint |
| 4 | +from flask import request |
| 5 | +from flask_jwt_extended import current_user |
| 6 | +from flask_jwt_extended import jwt_required |
| 7 | +from PyMatcha.models.image import Image |
| 8 | +from PyMatcha.utils.errors import BadRequestError |
| 9 | +from PyMatcha.utils.errors import NotFoundError |
| 10 | +from PyMatcha.utils.images import upload_image |
| 11 | +from PyMatcha.utils.success import Success |
| 12 | +from PyMatcha.utils.success import SuccessOutput |
| 13 | + |
| 14 | +images_bp = Blueprint("images", __name__) |
| 15 | + |
| 16 | + |
| 17 | +@images_bp.route("/profile/images", methods=["POST"]) |
| 18 | +@jwt_required |
| 19 | +def add_image_profile(): |
| 20 | + is_primary = request.args.get("is_primary", "false") == "true" |
| 21 | + # check if the post request has the file part |
| 22 | + if "file[]" not in request.files: |
| 23 | + raise BadRequestError("No file passed in request") |
| 24 | + file = request.files["file[]"] |
| 25 | + # if user does not select file, browser also |
| 26 | + # submit an empty part without filename |
| 27 | + if file.filename == "": |
| 28 | + raise BadRequestError("No filename passed in request") |
| 29 | + if file: |
| 30 | + tmp_img = BytesIO() |
| 31 | + file.save(tmp_img) |
| 32 | + link = upload_image(tmp_img, current_user.username) |
| 33 | + # TODO: Check if an image is already primary |
| 34 | + # TODO: Check if no more than 5 images |
| 35 | + Image.create(current_user.id, link, is_primary=is_primary) |
| 36 | + return SuccessOutput("image", link) |
| 37 | + else: |
| 38 | + raise ValueError("NO FILE") |
| 39 | + |
| 40 | + |
| 41 | +@images_bp.route("/profile/images/<image_id>", methods=["DELETE"]) |
| 42 | +@jwt_required |
| 43 | +def delete_image_profile(image_id): |
| 44 | + try: |
| 45 | + image = Image.get(id=image_id) |
| 46 | + except ValueError: |
| 47 | + raise NotFoundError(f"Image not found for user {current_user.id}") |
| 48 | + image.delete() |
| 49 | + return Success("Image successfully deleted.") |
| 50 | + |
| 51 | + |
| 52 | +@images_bp.route("/profile/images/<image_id>", methods=["PUT"]) |
| 53 | +@jwt_required |
| 54 | +def change_main_image(image_id): |
| 55 | + try: |
| 56 | + image = Image.get(id=image_id) |
| 57 | + except ValueError: |
| 58 | + raise NotFoundError(f"Image not found for user {current_user.id}") |
| 59 | + try: |
| 60 | + current_main_image = Image.get_multi(user_id=current_user.id, is_primary=True) |
| 61 | + except NotFoundError: |
| 62 | + # That means there was no primary image before |
| 63 | + pass |
| 64 | + else: |
| 65 | + current_main_image.is_primary = False |
| 66 | + current_main_image.save() |
| 67 | + image.is_primary = True |
| 68 | + image.save() |
| 69 | + |
| 70 | + |
| 71 | +@images_bp.route("/profile/images", methods=["GET"]) |
| 72 | +@jwt_required |
| 73 | +def get_images_profile(): |
| 74 | + images = Image.get_multis(user_id=current_user.id) |
| 75 | + ret = [] |
| 76 | + if images: |
| 77 | + ret = [image.to_dict() for image in images] |
| 78 | + return SuccessOutput("images", ret) |
0 commit comments