diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 00000000..a55e7a17 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/cookbook/admin.py b/cookbook/admin.py index 0115f8fc..238f09de 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -139,6 +139,27 @@ class CookLogAdmin(admin.ModelAdmin): admin.site.register(CookLog, CookLogAdmin) +class ShoppingListRecipeAdmin(admin.ModelAdmin): + list_display = ('id', 'recipe', 'multiplier') + + +admin.site.register(ShoppingListRecipe, ShoppingListRecipeAdmin) + + +class ShoppingListEntryAdmin(admin.ModelAdmin): + list_display = ('id', 'food', 'unit', 'list_recipe', 'checked') + + +admin.site.register(ShoppingListEntry, ShoppingListEntryAdmin) + + +class ShoppingListAdmin(admin.ModelAdmin): + list_display = ('id', 'created_by', 'created_at') + + +admin.site.register(ShoppingList, ShoppingListAdmin) + + class ShareLinkAdmin(admin.ModelAdmin): list_display = ('recipe', 'created_by', 'uuid', 'created_at',) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index 9be128eb..2755d794 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -195,13 +195,6 @@ class MealPlanSerializer(serializers.ModelSerializer): class ShoppingListRecipeSerializer(serializers.ModelSerializer): - recipe = RecipeSerializer(read_only=True) - - def create(self, validated_data): - return ShoppingListRecipe.objects.create(**validated_data) - - def update(self, instance, validated_data): - return super(ShoppingListRecipeSerializer, self).update(instance, validated_data) class Meta: model = ShoppingListRecipe @@ -209,26 +202,24 @@ class ShoppingListRecipeSerializer(serializers.ModelSerializer): read_only_fields = ('id',) -class ShoppingListEntrySerializer(serializers.ModelSerializer): - - def create(self, validated_data): - return ShoppingListEntry.objects.create(**validated_data) - - def update(self, instance, validated_data): - return super(ShoppingListEntrySerializer, self).update(instance, validated_data) +class ShoppingListEntrySerializer(WritableNestedModelSerializer): + food = FoodSerializer(allow_null=True) + unit = UnitSerializer(allow_null=True) class Meta: model = ShoppingListEntry - fields = ('list_recipe', 'food', 'unit', 'amount', 'order', 'checked') + fields = ('id', 'list_recipe', 'food', 'unit', 'amount', 'order', 'checked') + read_only_fields = ('id',) class ShoppingListSerializer(WritableNestedModelSerializer): - recipes = ShoppingListRecipeSerializer(many=True, allow_null=True, read_only=True) + recipes = ShoppingListRecipeSerializer(many=True, allow_null=True) entries = ShoppingListEntrySerializer(many=True, allow_null=True) class Meta: model = ShoppingList fields = ('id', 'uuid', 'note', 'recipes', 'entries', 'shared', 'created_by', 'created_at',) + read_only_fields = ('id',) class ShareLinkSerializer(serializers.ModelSerializer): diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index ec988bd5..6e006574 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -136,6 +136,7 @@ delimiters: ['[[', ']]'], el: '#id_base_container', data: { + shopping_list_id: {{ shopping_list_id }}, edit_mode: true, recipe_query: '', recipes: [], @@ -174,17 +175,22 @@ */ mounted: function () { - //TODO default share users - this.shopping_list = { - "recipes": [], - "entries": [], - "entries_display": [], - "shared": [], - "created_by": 1 + + if (this.shopping_list_id) { + this.loadShoppingList() + } else { + //TODO default share users + this.shopping_list = { + "recipes": [], + "entries": [], + "entries_display": [], + "shared": [], + "created_by": 1 + } + + this.updateDisplay() } - - this.updateDisplay() }, methods: { /* @@ -204,28 +210,48 @@ solid: true }) }, + loadShoppingList: function () { + + this.$http.get("{% url 'api:shoppinglist-detail' shopping_list_id %}").then((response) => { + this.shopping_list = response.body + this.updateDisplay() + }).catch((err) => { + console.log(err) + this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') + }) + }, updateShoppingList: function () { - for (let r of this.shopping_list.recipes.filter(item => item.created === true)) { - let old_id = r.id - console.log('updating recipe', r) - this.$http.post("{% url 'api:shoppinglistrecipe-list' %}", r, {}).then((response) => { - r = response - for (let e of this.shopping_list.entries.filter(item => item.list_recipe === old_id)) { - e.list_recipe = r.id - } + let recipe_promises = [] + + for (let i in this.shopping_list.recipes) { + if (this.shopping_list.recipes[i].created) { + console.log('updating recipe', this.shopping_list.recipes[i]) + recipe_promises.push(this.$http.post("{% url 'api:shoppinglistrecipe-list' %}", this.shopping_list.recipes[i], {}).then((response) => { + let old_id = this.shopping_list.recipes[i].id + console.log("list recipe create respose ", response.body) + this.shopping_list.recipes[i] = response.body + for (let e of this.shopping_list.entries.filter(item => item.list_recipe === old_id)) { + console.log("found recipe updating ID") + e.list_recipe = this.shopping_list.recipes[i].id + } + }).catch((err) => { + console.log(err) + this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger') + })) + } + } + + Promise.allSettled(recipe_promises).then(() => { + console.log("proceeding to update shopping list", this.shopping_list) + + this.$http.put("{% url 'api:shoppinglist-detail' shopping_list_id %}", this.shopping_list, {}).then((response) => { + console.log(response) + this.makeToast('{% trans 'Updated' %}', '{% trans 'Changes saved successfully!' %}', 'success') + }).catch((err) => { console.log(err) this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger') }) - } - - this.$http.put("{% url 'api:shoppinglist-detail' shopping_list_id %}", this.shopping_list, {}).then((response) => { - console.log(response) - this.makeToast('{% trans 'Updated' %}', '{% trans 'Changes saved successfully!' %}', 'success') - - }).catch((err) => { - console.log(err) - this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger') }) }, addEntry: function () { @@ -265,7 +291,7 @@ let slr = { "created": true, "id": Math.random() * 1000, - "recipe": recipe, + "recipe": recipe.id, "multiplier": 1 } this.shopping_list.recipes.push(slr)