Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
fix: Improve socket.io API
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Aug 17, 2023
1 parent c14b81c commit aa29ca7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
4 changes: 3 additions & 1 deletion app/controller/household/household_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,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 flask_socketio import close_room

household = Blueprint('household', __name__)

Expand Down Expand Up @@ -99,7 +100,8 @@ def updateHousehold(args, household_id):
@jwt_required()
@authorize_household(required=RequiredRights.ADMIN)
def deleteHouseholdById(household_id):
Household.delete_by_id(household_id)
if Household.delete_by_id(household_id):
close_room(household_id)
return jsonify({'msg': 'DONE'})


Expand Down
29 changes: 20 additions & 9 deletions app/controller/shoppinglist/shoppinglist_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ def updateItemDescription(args, id, item_id):

con.description = args['description'] or ''
con.save()
socketio.emit("shoppinglist_item:add", {
"item": con.obj_to_item_dict(),
"shoppinglist": shoppinglist.obj_to_dict()
}, to=shoppinglist.household_id)
return jsonify(con.obj_to_item_dict())


Expand Down Expand Up @@ -213,10 +217,11 @@ def removeShoppinglistItem(args, id):

con = removeShoppinglistItem(
shoppinglist, args['item_id'], args['removed_at'] if 'removed_at' in args else None)
if con: socketio.emit("shoppinglist_item:remove", {
"item": con.obj_to_item_dict(),
"shoppinglist": shoppinglist.obj_to_dict()
}, to=shoppinglist.household_id)
if con:
socketio.emit("shoppinglist_item:remove", {
"item": con.obj_to_item_dict(),
"shoppinglist": shoppinglist.obj_to_dict()
}, to=shoppinglist.household_id)

return jsonify({'msg': "DONE"})

Expand All @@ -233,10 +238,11 @@ def removeShoppinglistItems(args, id):
for arg in args['items']:
con = removeShoppinglistItem(
shoppinglist, arg['item_id'], arg['removed_at'] if 'removed_at' in arg else None)
if con: socketio.emit("shoppinglist_item:remove", {
"item": con.obj_to_item_dict(),
"shoppinglist": shoppinglist.obj_to_dict()
}, to=shoppinglist.household_id)
if con:
socketio.emit("shoppinglist_item:remove", {
"item": con.obj_to_item_dict(),
"shoppinglist": shoppinglist.obj_to_dict()
}, to=shoppinglist.household_id)

return jsonify({'msg': "DONE"})

Expand Down Expand Up @@ -289,7 +295,12 @@ def addRecipeItems(args, id):
db.session.add(con)

db.session.add(
History.create_added_without_save(shoppinglist, item))
History.create_added_without_save(shoppinglist, item, description))

socketio.emit("shoppinglist_item:add", {
"item": con.obj_to_item_dict(),
"shoppinglist": shoppinglist.obj_to_dict()
}, to=shoppinglist.household_id)

db.session.commit()
except Exception as e:
Expand Down
4 changes: 3 additions & 1 deletion app/helpers/db_model_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ def find_by_id(cls, target_id: int) -> Self:
return cls.query.filter(cls.id == target_id).first()

@classmethod
def delete_by_id(cls, target_id: int):
def delete_by_id(cls, target_id: int) -> bool:
mc = cls.find_by_id(target_id)
if mc:
mc.delete()
return True
return False

@classmethod
def all(cls) -> list[Self]:
Expand Down

0 comments on commit aa29ca7

Please sign in to comment.