ingredients saving

This commit is contained in:
vabene1111
2019-11-19 20:42:10 +01:00
parent ca7d0ccef4
commit e62219f031
4 changed files with 37 additions and 10 deletions

View File

@ -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',
),
]

View File

@ -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):

View File

@ -23,7 +23,7 @@
<div id="ingredients-table"></div>
<br>
<div class="table-controls">
<button class="btn" id="new_empty" type="button"><i class="far fa-plus"></i></button>
<button class="btn" id="new_empty" type="button"><i class="fas fa-plus-circle"></i></button>
</div>
<input type="hidden" id="ingredients_data_input" name="ingredients">
@ -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),

View File

@ -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])})