Skip to content

Commit

Permalink
Merge pull request #16 from Clubs-Council-IIITH/delete_old
Browse files Browse the repository at this point in the history
Delete old logo/banner functionality for clubs
  • Loading branch information
bhavberi authored Dec 17, 2024
2 parents 1a59484 + 4afaac6 commit 33ad257
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
27 changes: 19 additions & 8 deletions mutations.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
}
},
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
}
},
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
29 changes: 29 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 33ad257

Please sign in to comment.