From 148ce2faef9409871a4717a76284baec388209e2 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 21 Sep 2020 22:55:52 +0200 Subject: [PATCH] shopping list item checking --- cookbook/templates/shopping_list.html | 81 +++++++++++++++++++++------ cookbook/urls.py | 1 + cookbook/views/api.py | 19 ++++++- 3 files changed, 82 insertions(+), 19 deletions(-) diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index f62199df5..4902f0af3 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -14,16 +14,17 @@ + + {% endblock %} {% block content %}
-
+

{% trans 'Shopping List' %}

- -
+
{% trans 'Edit' %}
@@ -47,7 +48,10 @@
-
-
- +
+
+ +
-
-
- Non Edit +
+ +
+
+ + + + + + + +
+ [[x.amount]][[x.unit.name]][[x.food.name]]
+
+
@@ -199,7 +219,7 @@ data: { shopping_list_id: {% if shopping_list_id %}{{ shopping_list_id }}{% else %}null{% endif %}, loading: true, - edit_mode: true, + edit_mode: false, recipe_query: '', recipes: [], shopping_list: undefined, @@ -353,6 +373,26 @@ }) + }, + entryChecked: function (entry) { + + + this.shopping_list.entries.forEach((item) => { + if (item.id === entry.id) { //TODO unwrap once same entries are merged + item.checked = entry.checked + console.log('updating ', item) + this.$http.put("{% url 'api:shoppinglistentry-detail' 123456 %}".replace('123456', item.id), item, {}).then((response) => { + console.log("YEHAA", response) + }).catch((err) => { + console.log(err) + this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger') + this.loading = false + }) + + } + }) + + }, addEntry: function () { this.shopping_list.entries.push({ @@ -360,7 +400,8 @@ 'food': this.new_entry.food, 'unit': ((this.new_entry.unit !== undefined) ? this.new_entry.unit : {'name': ''}), 'amount': parseFloat(this.new_entry.amount), - 'order': 0 + 'order': 0, + 'checked': false }) this.new_entry = { @@ -373,9 +414,12 @@ }, getRecipes: function () { - let url = "{% url 'api:recipe-list' %}?limit=5" + let url = "{% url 'api:recipe-list' %}?limit=5&internal=true" if (this.recipe_query !== '') { url += '&query=' + this.recipe_query; + } else { + this.recipes = [] + return } this.$http.get(url).then((response) => { @@ -385,6 +429,9 @@ this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') }) }, + getRecipeUrl: function (id) { //TODO generic function that can be reused else were + return '{% url 'view_recipe' 123456 %}'.replace('123456', id) + }, addRecipeToList: function (recipe) { console.log(this.shopping_list) diff --git a/cookbook/urls.py b/cookbook/urls.py index 5427fb763..dbae3da29 100644 --- a/cookbook/urls.py +++ b/cookbook/urls.py @@ -24,6 +24,7 @@ router.register(r'ingredient', api.IngredientViewSet) router.register(r'meal-plan', api.MealPlanViewSet) router.register(r'meal-type', api.MealTypeViewSet) router.register(r'shopping-list', api.ShoppingListViewSet) +router.register(r'shopping-list-entry', api.ShoppingListEntryViewSet) router.register(r'shopping-list-recipe', api.ShoppingListRecipeViewSet) router.register(r'view-log', api.ViewLogViewSet) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 512664def..c8707e76b 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -28,11 +28,11 @@ from rest_framework.viewsets import ViewSetMixin from cookbook.helper.permission_helper import group_required, CustomIsOwner, CustomIsAdmin, CustomIsUser, CustomIsGuest, CustomIsShare from cookbook.helper.recipe_url_import import get_from_html -from cookbook.models import Recipe, Sync, Storage, CookLog, MealPlan, MealType, ViewLog, UserPreference, RecipeBook, Ingredient, Food, Step, Keyword, Unit, SyncLog, ShoppingListRecipe, ShoppingList +from cookbook.models import Recipe, Sync, Storage, CookLog, MealPlan, MealType, ViewLog, UserPreference, RecipeBook, Ingredient, Food, Step, Keyword, Unit, SyncLog, ShoppingListRecipe, ShoppingList, ShoppingListEntry from cookbook.provider.dropbox import Dropbox from cookbook.provider.nextcloud import Nextcloud from cookbook.serializer import MealPlanSerializer, MealTypeSerializer, RecipeSerializer, ViewLogSerializer, UserNameSerializer, UserPreferenceSerializer, RecipeBookSerializer, IngredientSerializer, FoodSerializer, StepSerializer, \ - KeywordSerializer, RecipeImageSerializer, StorageSerializer, SyncSerializer, SyncLogSerializer, UnitSerializer, ShoppingListSerializer, ShoppingListRecipeSerializer + KeywordSerializer, RecipeImageSerializer, StorageSerializer, SyncSerializer, SyncLogSerializer, UnitSerializer, ShoppingListSerializer, ShoppingListRecipeSerializer, ShoppingListEntrySerializer class UserNameViewSet(viewsets.ReadOnlyModelViewSet): @@ -203,6 +203,13 @@ class RecipeViewSet(viewsets.ModelViewSet, StandardFilterMixin): serializer_class = RecipeSerializer permission_classes = [CustomIsShare | CustomIsGuest] # TODO split read and write permission for meal plan guest + def get_queryset(self): + internal = self.request.query_params.get('internal', None) + if internal: + self.queryset = self.queryset.filter(internal=True) + + return super(RecipeViewSet, self).get_queryset() + # TODO write extensive tests for permissions @decorators.action( @@ -241,6 +248,14 @@ class ShoppingListRecipeViewSet(viewsets.ModelViewSet): # TODO custom get qs +class ShoppingListEntryViewSet(viewsets.ModelViewSet): + queryset = ShoppingListEntry.objects.all() + serializer_class = ShoppingListEntrySerializer + permission_classes = [CustomIsUser] # TODO add custom validation + + # TODO custom get qs + + class ShoppingListViewSet(viewsets.ModelViewSet): queryset = ShoppingList.objects.all() serializer_class = ShoppingListSerializer