diff --git a/cookbook/migrations/0076_shoppinglist_entries.py b/cookbook/migrations/0076_shoppinglist_entries.py new file mode 100644 index 000000000..2c4b4eebc --- /dev/null +++ b/cookbook/migrations/0076_shoppinglist_entries.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2020-08-26 18:46 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0075_shoppinglist_shoppinglistentry_shoppinglistrecipe'), + ] + + operations = [ + migrations.AddField( + model_name='shoppinglist', + name='entries', + field=models.ManyToManyField(blank=True, to='cookbook.ShoppingListEntry'), + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index 20dfd7e39..7f6e7450c 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -283,6 +283,7 @@ class ShoppingList(models.Model): uuid = models.UUIDField(default=uuid.uuid4) note = models.TextField(blank=True, null=True) recipes = models.ManyToManyField(ShoppingListRecipe, blank=True) + entries = models.ManyToManyField(ShoppingListEntry, blank=True) shared = models.ManyToManyField(User, blank=True, related_name='list_share') created_by = models.ForeignKey(User, on_delete=models.CASCADE) created_at = models.DateTimeField(auto_now_add=True) diff --git a/cookbook/serializer.py b/cookbook/serializer.py index f7a004c42..d66c40f43 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -209,17 +209,25 @@ class ShoppingListRecipeSerializer(serializers.ModelSerializer): 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 Meta: model = ShoppingListEntry fields = ('list_recipe', 'food', 'unit', 'amount', 'order', 'checked') -class ShoppingListSerializer(serializers.ModelSerializer): +class ShoppingListSerializer(WritableNestedModelSerializer): recipes = ShoppingListRecipeSerializer(many=True, allow_null=True, read_only=True) + entries = ShoppingListEntrySerializer(many=True, allow_null=True) class Meta: model = ShoppingList - fields = ('id', 'uuid', 'note', 'recipes', 'shared', 'created_by', 'created_at',) + fields = ('id', 'uuid', 'note', 'recipes', 'entries', 'shared', 'created_by', 'created_at',) class ShareLinkSerializer(serializers.ModelSerializer): diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index 2db45f28a..855dc27fd 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -30,15 +30,32 @@
-
+
-
    -
  • [[x.name]]
  • +
  • [[x.name]] + +
+
+
    +
  • [[x.recipe.name]] +
  • +
+
+
+
+
+ + + + +
[[x]]
+
@@ -65,6 +82,7 @@ edit_mode: true, recipe_query: '', recipes: [], + shopping_list: undefined, }, directives: { tabindex: { @@ -89,6 +107,13 @@ */ mounted: function () { + //TODO default share users + this.shopping_list = { + "recipes": [], + "entries": [], + "shared": [], + "created_by": 1 + } }, methods: { /* @@ -112,6 +137,25 @@ this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') }) }, + addRecipeToList: function (recipe) { + console.log(this.shopping_list) + this.shopping_list.recipes.push({ + "recipe": recipe, + "multiplier": 1 + }) + + for (let s of recipe.steps) { + for (let i of s.ingredients) { + this.shopping_list.entries.push({ + 'list_recipe': recipe.id, + 'food': i.food, + 'unit': i.unit, + 'amount': i.amount, + 'order': 0 + }) + } + } + }, searchKeywords: function (query) { this.keywords_loading = true this.$http.get("{% url 'api:keyword-list' %}" + '?query=' + query + '&limit=10').then((response) => {