From 4afaac6761f2c6839764fb91533f0033cb7a8fc9 Mon Sep 17 00:00:00 2001 From: Bhav Beri Date: Mon, 16 Dec 2024 21:39:38 +0530 Subject: [PATCH] Delete old logo/banner functionality for clubs --- mutations.py | 27 +++++++++++++++++++-------- utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/mutations.py b/mutations.py index c682685..3261a13 100644 --- a/mutations.py +++ b/mutations.py @@ -1,10 +1,8 @@ -from datetime import datetime - import strawberry from fastapi.encoders import jsonable_encoder from db import clubsdb -from models import Club +from models import Club, create_utc_time # import all models and types from otypes import ( @@ -14,7 +12,12 @@ SimpleClubInput, SimpleClubType, ) -from utils import getUser, update_events_members_cid, update_role +from utils import ( + check_remove_old_file, + getUser, + update_events_members_cid, + update_role, +) @strawberry.mutation @@ -88,6 +91,10 @@ def editClub(clubInput: FullClubInput, info: Info) -> FullClubType: club_input["state"] = exists["state"] club_input["_id"] = exists["_id"] + check_remove_old_file(exists, club_input, "logo") + check_remove_old_file(exists, club_input, "banner") + check_remove_old_file(exists, club_input, "banner_square") + clubsdb.replace_one({"code": club_input["code"]}, club_input) if "socials" in club_input.keys(): clubsdb.update_one( @@ -115,7 +122,7 @@ def editClub(clubInput: FullClubInput, info: Info) -> FullClubType: { "$set": { "created_time": exists["created_time"], - "updated_time": datetime.utcnow(), + "updated_time": create_utc_time(), } }, ) @@ -162,6 +169,10 @@ def editClub(clubInput: FullClubInput, info: Info) -> FullClubType: club_input["state"] = exists["state"] club_input["_id"] = exists["_id"] + check_remove_old_file(exists, club_input, "logo") + check_remove_old_file(exists, club_input, "banner") + check_remove_old_file(exists, club_input, "banner_square") + clubsdb.replace_one({"cid": uid}, club_input) if "socials" in club_input.keys(): clubsdb.update_one( @@ -189,7 +200,7 @@ def editClub(clubInput: FullClubInput, info: Info) -> FullClubType: { "$set": { "created_time": exists["created_time"], - "updated_time": datetime.utcnow(), + "updated_time": create_utc_time(), } }, ) @@ -220,7 +231,7 @@ def deleteClub(clubInput: SimpleClubInput, info: Info) -> SimpleClubType: clubsdb.update_one( {"cid": club_input["cid"]}, - {"$set": {"state": "deleted", "updated_time": datetime.utcnow()}}, + {"$set": {"state": "deleted", "updated_time": create_utc_time()}}, ) update_role(club_input["cid"], info.context.cookies, "public") @@ -249,7 +260,7 @@ def restartClub(clubInput: SimpleClubInput, info: Info) -> SimpleClubType: clubsdb.update_one( {"cid": club_input["cid"]}, - {"$set": {"state": "active", "updated_time": datetime.utcnow()}}, + {"$set": {"state": "active", "updated_time": create_utc_time()}}, ) update_role(club_input["cid"], info.context.cookies, "club") diff --git a/utils.py b/utils.py index 1af7c0a..fe64d10 100644 --- a/utils.py +++ b/utils.py @@ -135,3 +135,32 @@ def getUser(uid, cookies=None): return request.json()["data"]["userProfile"] except Exception: return None + + +def delete_file(filename): + response = requests.post( + "http://files/delete-file", + params={ + "filename": filename, + "inter_communication_secret": inter_communication_secret, + }, + ) + + if response.status_code != 200: + raise Exception(response.text) + + return response.text + + +def check_remove_old_file(old_obj, new_obj, name="logo"): + old_file = old_obj.get(name) + new_file = new_obj.get(name) + + if old_file and new_file and old_file != new_file: + try: + delete_file(old_file) + except Exception as e: + print(f"Error in deleting old {name} file: {e}") + return False + + return True