Skip to content

Commit

Permalink
feat: List remaining items for shopping lists
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Aug 13, 2024
1 parent d2135f8 commit 6403fcc
Show file tree
Hide file tree
Showing 15 changed files with 442 additions and 442 deletions.
5 changes: 5 additions & 0 deletions backend/app/controller/shoppinglist/schemas.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from marshmallow import fields, Schema, EXCLUDE


class GetShoppingLists(Schema):
orderby = fields.Integer()
recent_limit = fields.Integer(load_default=9, validate=lambda x: x > 0 and x <= 60)


class AddItemByName(Schema):
name = fields.String(required=True)
description = fields.String()
Expand Down
43 changes: 41 additions & 2 deletions backend/app/controller/shoppinglist/shoppinglist_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)
from app.helpers import validate_args, authorize_household
from .schemas import (
GetShoppingLists,
RemoveItem,
UpdateDescription,
AddItemByName,
Expand Down Expand Up @@ -44,9 +45,41 @@ def createShoppinglist(args, household_id):
@shoppinglistHousehold.route("", methods=["GET"])
@jwt_required()
@authorize_household()
def getShoppinglists(household_id):
@validate_args(GetShoppingLists)
def getShoppinglists(args, household_id):
shoppinglists = Shoppinglist.all_from_household(household_id)
return jsonify([e.obj_to_dict() for e in shoppinglists])
recentItems = {}
for shoppinglist in shoppinglists:
recentItems[shoppinglist.id] = [
e.item.obj_to_dict() | {"description": e.description}
for e in History.get_recent(shoppinglist.id, args["recent_limit"])
]

orderby = [Item.name]
if "orderby" in args and args["orderby"] == 1:
orderby = [Item.ordering == 0, Item.ordering]

items = {}
for shoppinglist in shoppinglists:
items[shoppinglist.id] = (
ShoppinglistItems.query.filter(
ShoppinglistItems.shoppinglist_id == shoppinglist.id
)
.join(ShoppinglistItems.item)
.order_by(*orderby, Item.name)
.all()
)

return jsonify(
[
shoppinglist.obj_to_dict()
| {
"recentItems": recentItems[shoppinglist.id],
"items": [e.obj_to_item_dict() for e in items[shoppinglist.id]],
}
for shoppinglist in shoppinglists
]
)


@shoppinglist.route("/<int:id>", methods=["POST"])
Expand Down Expand Up @@ -114,6 +147,9 @@ def updateItemDescription(args, id, item_id):
@jwt_required()
@validate_args(GetItems)
def getAllShoppingListItems(args, id):
'''
Deprecated in favor of including it directly in the shopping list
'''
shoppinglist = Shoppinglist.find_by_id(id)
if not shoppinglist:
raise NotFoundRequest()
Expand All @@ -139,6 +175,9 @@ def getAllShoppingListItems(args, id):
@jwt_required()
@validate_args(GetRecentItems)
def getRecentItems(args, id):
'''
Deprecated in favor of including it directly in the shopping list
'''
shoppinglist = Shoppinglist.find_by_id(id)
if not shoppinglist:
raise NotFoundRequest()
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/lib/cubits/item_edit_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ItemEditCubit<T extends Item> extends Cubit<ItemEditState> {
if (shoppingList != null && state.hasChangedDescription(_item)) {
await TransactionHandler.getInstance()
.runTransaction(TransactionShoppingListUpdateItem(
household: household!,
shoppinglist: shoppingList!,
item: _item,
description: state.description,
Expand Down
1 change: 1 addition & 0 deletions kitchenowl/lib/cubits/planner_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class PlannerCubit extends Cubit<PlannerCubitState> {
) {
return TransactionHandler.getInstance()
.runTransaction(TransactionShoppingListAddRecipeItems(
household: household,
shoppinglist: shoppingList,
items: items,
));
Expand Down
11 changes: 5 additions & 6 deletions kitchenowl/lib/cubits/recipe_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ class RecipeCubit extends Cubit<RecipeState> {
final TransactionHandler _transactionHandler;

RecipeCubit(Household? household, Recipe recipe, int? selectedYields)
: this.forTesting(TransactionHandler.getInstance(), household, recipe, selectedYields);
: this.forTesting(TransactionHandler.getInstance(), household, recipe,
selectedYields);

RecipeCubit.forTesting(
TransactionHandler transactionHandler,
this.household,
Recipe recipe,
int? selectedYields)
RecipeCubit.forTesting(TransactionHandler transactionHandler, this.household,
Recipe recipe, int? selectedYields)
: _transactionHandler = transactionHandler,
super(RecipeState(recipe: recipe, selectedYields: selectedYields)) {
refresh();
Expand Down Expand Up @@ -69,6 +67,7 @@ class RecipeCubit extends Cubit<RecipeState> {
if (shoppingList != null) {
await _transactionHandler
.runTransaction(TransactionShoppingListAddRecipeItems(
household: household!,
shoppinglist: shoppingList,
items: state.dynamicRecipe.items
.where((item) => state.selectedItems.contains(item.name))
Expand Down
Loading

0 comments on commit 6403fcc

Please sign in to comment.