1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2025-11-14 18:51:27 +01:00

Compare commits

..

2 Commits

Author SHA1 Message Date
Ehouarn
033c466cf7 Fix some bugs 2025-11-07 15:52:42 +01:00
Ehouarn
6a77cfd4dd Add model Recipe 2025-11-07 14:29:03 +01:00
3 changed files with 34 additions and 6 deletions

View File

@@ -27,7 +27,7 @@ class FoodSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = Food model = Food
fields = ['name', 'owner', 'allergens', 'expiry_date', 'end_of_life', 'is_ready', 'order', 'owner_name'] fields = ['id', 'name', 'owner', 'allergens', 'expiry_date', 'end_of_life', 'is_ready', 'order', 'owner_name']
class BasicFoodSerializer(serializers.ModelSerializer): class BasicFoodSerializer(serializers.ModelSerializer):

View File

@@ -486,6 +486,7 @@ class Order(models.Model):
destination=self.activity.organizer.note, destination=self.activity.organizer.note,
amount=self.amount, amount=self.amount,
quantity=1, quantity=1,
reason=str(self.dish),
) )
transaction.save() transaction.save()
else: else:

View File

@@ -308,7 +308,7 @@ class ManageIngredientsView(LoginRequiredMixin, UpdateView):
prefix = 'form-' + str(i) + '-' prefix = 'form-' + str(i) + '-'
ingredient = None 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 ingredient = QRCode.objects.get(pk=form.data[prefix + 'qrcode']).food_container
elif form.data[prefix + 'name'] != '': elif form.data[prefix + 'name'] != '':
@@ -917,7 +917,7 @@ class RecipeCreateView(ProtectQuerysetMixin, ProtectedCreateView):
return context return context
def get_success_url(self): def get_success_url(self):
return reverse_lazy('food:recipe_create') return reverse_lazy('food:recipe_list')
class RecipeListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView): class RecipeListView(ProtectQuerysetMixin, LoginRequiredMixin, SingleTableView):
@@ -1000,17 +1000,43 @@ class UseRecipeView(LoginRequiredMixin, UpdateView):
Add ingredients to a TransformedFood using a Recipe Add ingredients to a TransformedFood using a Recipe
""" """
model = TransformedFood model = TransformedFood
fields = '__all__' fields = ('ingredients',)
template_name = 'food/use_recipe_form.html' 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): def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs) context = super().get_context_data(**kwargs)
context["form"] = UseRecipeForm() context["form"] = UseRecipeForm()
return context 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 @require_GET
def get_ingredients_for_recipe(request): def get_ingredients_for_recipe(request):
@@ -1041,6 +1067,7 @@ def get_ingredients_for_recipe(request):
query |= Q(name__istartswith=name) query |= Q(name__istartswith=name)
qs = Food.objects.filter(query).distinct() 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] 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}) return JsonResponse({'ingredients': data})