From e62219f031cadaefa8be8319bd9c52de58e0c7fd Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Tue, 19 Nov 2019 20:42:10 +0100 Subject: [PATCH] ingredients saving --- cookbook/migrations/0002_auto_20191119_2035.py | 18 ++++++++++++++++++ cookbook/models.py | 2 +- .../templates/forms/edit_internal_recipe.html | 14 +++++++------- cookbook/views/edit.py | 13 +++++++++++-- 4 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 cookbook/migrations/0002_auto_20191119_2035.py diff --git a/cookbook/migrations/0002_auto_20191119_2035.py b/cookbook/migrations/0002_auto_20191119_2035.py new file mode 100644 index 000000000..eff2c1154 --- /dev/null +++ b/cookbook/migrations/0002_auto_20191119_2035.py @@ -0,0 +1,18 @@ +# Generated by Django 2.2.7 on 2019-11-19 19:35 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('cookbook', '0001_initial'), + ] + + operations = [ + migrations.RenameField( + model_name='recipeingredients', + old_name='ingredient', + new_name='name', + ), + ] diff --git a/cookbook/models.py b/cookbook/models.py index b2a991328..a080ae26f 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -72,10 +72,10 @@ class Recipe(models.Model): class RecipeIngredients(models.Model): + name = models.CharField(max_length=128) recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE) unit = models.CharField(max_length=128) amount = models.DecimalField(default=0, decimal_places=2, max_digits=16) - ingredient = models.CharField(max_length=128) class Comment(models.Model): diff --git a/cookbook/templates/forms/edit_internal_recipe.html b/cookbook/templates/forms/edit_internal_recipe.html index 69fe8fe24..028316c5a 100644 --- a/cookbook/templates/forms/edit_internal_recipe.html +++ b/cookbook/templates/forms/edit_internal_recipe.html @@ -23,7 +23,7 @@

- +
@@ -57,15 +57,15 @@ data: data, columns: [ { - title: "{% trans 'ingredient' %}", - field: "ingredient", + title: "{% trans 'Ingredient' %}", + field: "name", validator: "required", editor: "input" }, - {title: "{% trans 'amount' %}", field: "amount", validator: "required", editor: "input"}, - {title: "{% trans 'unit' %}", field: "unit", validator: "required", editor: "input"}, + {title: "{% trans 'Amount' %}", field: "amount", validator: "required", editor: "input"}, + {title: "{% trans 'Unit' %}", field: "unit", validator: "required", editor: "input"}, { - title: "{% trans 'delete' %}", + title: "{% trans 'Delete' %}", field: "delete", align: "center", editor: true, @@ -87,7 +87,7 @@ document.getElementById("new_empty").addEventListener("click", function () { data.push({ - ingredient: "{% trans 'ingredient' %}", + name: "{% trans 'Ingredient' %}", amount: "100", unit: "g", id: Math.floor(Math.random() * 10000000), diff --git a/cookbook/views/edit.py b/cookbook/views/edit.py index c8d010af7..fd6968cf4 100644 --- a/cookbook/views/edit.py +++ b/cookbook/views/edit.py @@ -45,6 +45,17 @@ def internal_recipe_update(request, pk): recipe.save() + form_ingredients = json.loads(form.data['ingredients']) + RecipeIngredients.objects.filter(recipe=recipe_instance).delete() + + for i in form_ingredients: + ingredient = RecipeIngredients() + ingredient.recipe = recipe_instance + ingredient.name = i['name'] + ingredient.amount = i['amount'] + ingredient.unit = i['unit'] + ingredient.save() + recipe.keywords.set(form.cleaned_data['keywords']) messages.add_message(request, messages.SUCCESS, _('Recipe saved!')) @@ -56,8 +67,6 @@ def internal_recipe_update(request, pk): ingredients = RecipeIngredients.objects.filter(recipe=recipe_instance) - print(list(ingredients)) - return render(request, 'forms/edit_internal_recipe.html', {'form': form, 'ingredients': json.dumps(list(ingredients.values())), 'view_url': reverse('view_recipe', args=[pk])})