Skip to content

Commit

Permalink
feat: Soft delete households
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Mar 1, 2024
1 parent b130f48 commit 3e10a5e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
9 changes: 6 additions & 3 deletions backend/app/controller/household/household_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from app.service.import_language import importLanguage
from app.service.file_has_access_or_download import file_has_access_or_download
from .schemas import AddHousehold, UpdateHousehold, UpdateHouseholdMember
from app import socketio
from app import socketio, db

household = Blueprint("household", __name__)

Expand Down Expand Up @@ -112,8 +112,11 @@ def updateHousehold(args, household_id):
@jwt_required()
@authorize_household(required=RequiredRights.ADMIN)
def deleteHouseholdById(household_id):
if Household.delete_by_id(household_id):
socketio.close_room(household_id)
hms = HouseholdMember.find_by_household(household_id)
for hm in hms:
db.session.delete(hm)
db.session.commit()
socketio.close_room(household_id)
return jsonify({"msg": "DONE"})


Expand Down
21 changes: 21 additions & 0 deletions backend/app/jobs/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
ChallengePasswordReset,
OIDCRequest,
)
from app.service.delete_unused import deleteEmptyHouseholds
from .item_ordering import findItemOrdering
from .item_suggestions import findItemSuggestions
from .cluster_shoppings import clusterShoppings


if not MESSAGE_BROKER:

@scheduler.task("cron", id="everyMonth", day="1", hour="0", minute="0")
def setup_daily():
with app.app_context():
monthly()

@scheduler.task("cron", id="everyDay", day_of_week="*", hour="3", minute="0")
def setup_daily():
with app.app_context():
Expand All @@ -28,6 +34,11 @@ def setup_halfHourly():
halfHourly()

else:

@celery_app.task
def monthlyTask():
monthly()

@celery_app.task
def dailyTask():
daily()
Expand All @@ -48,6 +59,16 @@ def setup_periodic_tasks(sender, **kwargs):
name="everyDay",
)

sender.add_periodic_task(
crontab(day_of_month="1", hour=0, minute=0),
monthlyTask,
name="everyMonth",
)


def monthly():
deleteEmptyHouseholds()


def daily():
app.logger.info("--- daily analysis is starting ---")
Expand Down
17 changes: 10 additions & 7 deletions backend/app/models/household.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,15 @@ def obj_to_user_dict(self) -> dict:
return res

def delete(self):
if len(self.household.member) <= 1:
self.household.delete()
elif self.owner:
if self.owner:
newOwner = next(
(m for m in self.household.member if m.admin and m != self),
next((m for m in self.household.member if m != self)),
next((m for m in self.household.member if m != self), None),
)
newOwner.admin = True
newOwner.owner = True
newOwner.save()
if newOwner:
newOwner.admin = True
newOwner.owner = True
newOwner.save()
super().delete()
else:
super().delete()
Expand All @@ -116,3 +115,7 @@ def find_by_household(cls, household_id: int) -> list[Self]:
@classmethod
def find_by_user(cls, user_id: int) -> list[Self]:
return cls.query.filter(cls.user_id == user_id).all()

@classmethod
def find_by_household(cls, household_id: int) -> list[Self]:
return cls.query.filter(cls.household_id == household_id).all()

0 comments on commit 3e10a5e

Please sign in to comment.