Skip to content

Commit 9cf77c4

Browse files
committed
Added checks for primary image and image count
1 parent bc89c8b commit 9cf77c4

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

backend/PyMatcha/routes/api/profile/images.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,27 @@ def add_image_profile():
2727
if file.filename == "":
2828
raise BadRequestError("No filename passed in request")
2929
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)
30+
if is_primary:
31+
try:
32+
Image.get_multi(user_id=current_user.id, is_primary=True)
33+
except ValueError:
34+
# That means there was no primary image before
35+
tmp_img = BytesIO()
36+
file.save(tmp_img)
37+
link = upload_image(tmp_img, current_user.username)
38+
Image.create(current_user.id, link, is_primary=True)
39+
return SuccessOutput("image", link)
40+
else:
41+
raise BadRequestError("There already is a primary image for user {}".format(current_user.id))
42+
else:
43+
image_count = len(Image.get_multis(user_id=current_user.id, is_primary=False))
44+
if image_count >= 4:
45+
raise BadRequestError("There's already enough images for this account")
46+
tmp_img = BytesIO()
47+
file.save(tmp_img)
48+
link = upload_image(tmp_img, current_user.username)
49+
Image.create(current_user.id, link, is_primary=False)
50+
return SuccessOutput("image", link)
3751
else:
3852
raise ValueError("NO FILE")
3953

0 commit comments

Comments
 (0)