normalized ingredients

This commit is contained in:
vabene1111
2020-02-16 23:22:44 +01:00
parent f77aa7c8f0
commit 7d4630e3af
6 changed files with 58 additions and 27 deletions

View File

@ -1,6 +1,6 @@
from dal import autocomplete
from cookbook.models import Keyword, RecipeIngredient, Recipe, Unit
from cookbook.models import Keyword, RecipeIngredient, Recipe, Unit, Ingredient
class KeywordAutocomplete(autocomplete.Select2QuerySetView):
@ -19,12 +19,12 @@ class KeywordAutocomplete(autocomplete.Select2QuerySetView):
class IngredientsAutocomplete(autocomplete.Select2QuerySetView):
def get_queryset(self):
if not self.request.user.is_authenticated:
return RecipeIngredient.objects.none()
return Ingredient.objects.none()
qs = RecipeIngredient.objects.all()
qs = Ingredient.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
qs = qs.filter(name__icontains=self.q)
return qs

View File

@ -0,0 +1,18 @@
# Generated by Django 3.0.2 on 2020-02-16 22:13
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0023_auto_20200216_2311'),
]
operations = [
migrations.RenameField(
model_name='recipeingredient',
old_name='name',
new_name='ingredient',
),
]

View File

@ -103,13 +103,13 @@ class Ingredient(models.Model):
class RecipeIngredient(models.Model):
name = models.ForeignKey(Ingredient, on_delete=models.PROTECT, null=True)
ingredient = models.ForeignKey(Ingredient, on_delete=models.PROTECT, null=True)
recipe = models.ForeignKey(Recipe, on_delete=models.CASCADE)
unit = models.ForeignKey(Unit, on_delete=models.PROTECT, null=True)
amount = models.DecimalField(default=0, decimal_places=2, max_digits=16)
def __str__(self):
return str(self.amount) + ' ' + str(self.unit) + ' ' + str(self.name)
return str(self.amount) + ' ' + str(self.unit) + ' ' + str(self.ingredient)
class Comment(models.Model):

View File

@ -49,7 +49,15 @@
<script>
let select2Editor = function (cell, onRendered, success, cancel, editorParams) {
let select2UnitEditor = function (cell, onRendered, success, cancel, editorParams) {
return select2Editor(cell, onRendered, success, cancel, editorParams, '{% url 'dal_unit' %}')
};
let select2IngredientEditor = function (cell, onRendered, success, cancel, editorParams) {
return select2Editor(cell, onRendered, success, cancel, editorParams, '{% url 'dal_ingredient' %}')
};
let select2Editor = function (cell, onRendered, success, cancel, editorParams, url) {
let editor = document.createElement("select");
editor.setAttribute("class", "form-control");
@ -61,7 +69,7 @@
select_2.select2({
tags: true,
ajax: {
url: '{% url 'dal_unit' %}',
url: url,
dataType: 'json'
}
});
@ -79,7 +87,6 @@
});
});
//add editor to cell
return editor;
};
@ -133,12 +140,12 @@
},
{
title: "{% trans 'Ingredient' %}",
field: "name",
field: "ingredient__name",
validator: "required",
editor: "input"
editor: select2IngredientEditor
},
{title: "{% trans 'Amount' %}", field: "amount", validator: "required", editor: "input"},
{title: "{% trans 'Unit' %}", field: "unit__name", validator: "required", editor: select2Editor},
{title: "{% trans 'Unit' %}", field: "unit__name", validator: "required", editor: select2UnitEditor},
{
formatter: function (cell, formatterParams) {
return "<span style='color:red'><i class=\"fas fa-trash-alt\"></i></span>"
@ -176,7 +183,7 @@
document.getElementById("new_empty").addEventListener("click", function () {
data.push({
name: "{% trans 'Ingredient' %}",
ingredient__name: "{% trans 'Ingredient' %}",
amount: "100",
unit__name: "g",
id: Math.floor(Math.random() * 10000000),

View File

@ -95,7 +95,7 @@
</div>
</td>
<td style="font-size: large">{{ i.name }}</td>
<td style="font-size: large">{{ i.ingredient.name }}</td>
</tr>
{% endfor %}
</table>

View File

@ -17,7 +17,7 @@ from django.views.generic import UpdateView, DeleteView
from cookbook.forms import ExternalRecipeForm, KeywordForm, StorageForm, SyncForm, InternalRecipeForm, CommentForm, \
MealPlanForm, UnitMergeForm
from cookbook.models import Recipe, Sync, Keyword, RecipeImport, Storage, Comment, RecipeIngredient, RecipeBook, \
RecipeBookEntry, MealPlan, Unit
RecipeBookEntry, MealPlan, Unit, Ingredient
from cookbook.provider.dropbox import Dropbox
from cookbook.provider.nextcloud import Nextcloud
@ -78,23 +78,31 @@ def internal_recipe_update(request, pk):
RecipeIngredient.objects.filter(recipe=recipe_instance).delete()
for i in form_ingredients:
ingredient = RecipeIngredient()
ingredient.recipe = recipe_instance
ingredient.name = i['name']
if isinstance(i['amount'], str):
ingredient.amount = float(i['amount'].replace(',', '.'))
recipe_ingredient = RecipeIngredient()
recipe_ingredient.recipe = recipe_instance
if Ingredient.objects.filter(name=i['ingredient__name']).exists():
recipe_ingredient.ingredient = Ingredient.objects.get(name=i['ingredient__name'])
else:
ingredient.amount = i['amount']
ingredient = Ingredient()
ingredient.name = i['ingredient__name']
ingredient.save()
recipe_ingredient.ingredient = ingredient
if isinstance(i['amount'], str):
recipe_ingredient.amount = float(i['amount'].replace(',', '.'))
else:
recipe_ingredient.amount = i['amount']
if Unit.objects.filter(name=i['unit__name']).exists():
ingredient.unit = Unit.objects.get(name=i['unit__name'])
recipe_ingredient.unit = Unit.objects.get(name=i['unit__name'])
else:
unit = Unit()
unit.name = i['unit__name']
unit.save()
ingredient.unit = unit
recipe_ingredient.unit = unit
ingredient.save()
recipe_ingredient.save()
recipe.keywords.set(form.cleaned_data['keywords'])
@ -105,9 +113,7 @@ def internal_recipe_update(request, pk):
else:
form = InternalRecipeForm(instance=recipe_instance)
ingredients = RecipeIngredient.objects.select_related('unit__name').filter(recipe=recipe_instance).values('name',
'unit__name',
'amount')
ingredients = RecipeIngredient.objects.select_related('unit__name', 'ingredient__name').filter(recipe=recipe_instance).values('ingredient__name', 'unit__name', 'amount')
return render(request, 'forms/edit_internal_recipe.html',
{'form': form, 'ingredients': json.dumps(list(ingredients)),