|
|
|
|
@@ -308,7 +308,7 @@ class ManageIngredientsView(LoginRequiredMixin, UpdateView):
|
|
|
|
|
prefix = 'form-' + str(i) + '-'
|
|
|
|
|
|
|
|
|
|
ingredient = None
|
|
|
|
|
if form.data[prefix + 'qrcode'] not in ['0', '']:
|
|
|
|
|
if form.data[prefix + 'qrcode'] not in ['0', '', 'NaN']:
|
|
|
|
|
ingredient = QRCode.objects.get(pk=form.data[prefix + 'qrcode']).food_container
|
|
|
|
|
|
|
|
|
|
elif form.data[prefix + 'name'] != '':
|
|
|
|
|
@@ -917,7 +917,7 @@ class RecipeCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
|
return reverse_lazy('food:recipe_create')
|
|
|
|
|
return reverse_lazy('food:recipe_list')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class RecipeListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
|
|
|
|
|
@@ -1000,17 +1000,43 @@ class UseRecipeView(LoginRequiredMixin, UpdateView):
|
|
|
|
|
Add ingredients to a TransformedFood using a Recipe
|
|
|
|
|
"""
|
|
|
|
|
model = TransformedFood
|
|
|
|
|
fields = '__all__'
|
|
|
|
|
fields = ('ingredients',)
|
|
|
|
|
template_name = 'food/use_recipe_form.html'
|
|
|
|
|
extra_context = {"title": _("Use a recipe:")}
|
|
|
|
|
extra_context = {"title": _("Use a recipe for:")}
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
|
|
|
|
|
|
context["form"] = UseRecipeForm()
|
|
|
|
|
|
|
|
|
|
return context
|
|
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
|
old_ingredients = list(self.object.ingredients.all()).copy()
|
|
|
|
|
old_allergens = list(self.object.allergens.all()).copy()
|
|
|
|
|
old_traces = list(self.object.traces.all()).copy()
|
|
|
|
|
if "ingredients" in form.data:
|
|
|
|
|
ingredients_pk = form.data.getlist("ingredients")
|
|
|
|
|
ingredients = Food.objects.all().filter(pk__in=ingredients_pk)
|
|
|
|
|
for ingredient in ingredients:
|
|
|
|
|
self.object.ingredients.add(ingredient)
|
|
|
|
|
|
|
|
|
|
# We recalculate new expiry date and allergens
|
|
|
|
|
self.object.expiry_date = self.object.creation_date + self.object.shelf_life
|
|
|
|
|
self.object.allergens.clear()
|
|
|
|
|
self.object.traces.clear()
|
|
|
|
|
|
|
|
|
|
for ingredient in self.object.ingredients.iterator():
|
|
|
|
|
if not (ingredient.polymorphic_ctype.model == 'basicfood' and ingredient.date_type == 'DDM'):
|
|
|
|
|
self.object.expiry_date = min(self.object.expiry_date, ingredient.expiry_date)
|
|
|
|
|
self.object.allergens.set(self.object.allergens.union(ingredient.allergens.all()))
|
|
|
|
|
self.object.traces.set(self.object.traces.union(ingredient.traces.all()))
|
|
|
|
|
|
|
|
|
|
self.object.save(old_ingredients=old_ingredients, old_allergens=old_allergens, old_traces=old_traces)
|
|
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
|
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
|
return reverse_lazy('food:transformedfood_view', kwargs={"pk": self.object.pk})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@require_GET
|
|
|
|
|
def get_ingredients_for_recipe(request):
|
|
|
|
|
@@ -1041,6 +1067,7 @@ def get_ingredients_for_recipe(request):
|
|
|
|
|
query |= Q(name__istartswith=name)
|
|
|
|
|
|
|
|
|
|
qs = Food.objects.filter(query).distinct()
|
|
|
|
|
qs = qs.filter(PermissionBackend.filter_queryset(request, Food, 'view'))
|
|
|
|
|
|
|
|
|
|
data = [{'id': f.id, 'name': f.name, 'qr_code_numbers': ", ".join(str(q.qr_code_number) for q in f.QR_code.all())} for f in qs]
|
|
|
|
|
return JsonResponse({'ingredients': data})
|
|
|
|
|
|