2024-05-24 19:49:23 +00:00
|
|
|
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
'''
|
|
|
|
from crispy_forms.helper import FormHelper
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.core.exceptions import ValidationError, PermissionDenied
|
|
|
|
from django.db.models import Q
|
|
|
|
from django.forms import Form
|
2024-05-21 09:21:13 +00:00
|
|
|
from django.http import HttpResponse
|
2024-05-24 19:49:23 +00:00
|
|
|
from django.shortcuts import redirect
|
|
|
|
'''
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from django.utils import timezone
|
2024-05-25 13:27:26 +00:00
|
|
|
from datetime import timedelta
|
2024-05-24 19:49:23 +00:00
|
|
|
'''
|
|
|
|
from django.views.generic import UpdateView, DetailView
|
|
|
|
from django.views.generic.base import View, TemplateView
|
|
|
|
from django.views.generic.edit import BaseFormView, DeleteView
|
|
|
|
from django_tables2 import SingleTableView
|
|
|
|
'''
|
|
|
|
from permission.views import ProtectQuerysetMixin, ProtectedCreateView
|
|
|
|
|
|
|
|
from django.shortcuts import render
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
from .forms import BasicFoodForms, TransformedFoodForms, AllergenForms
|
|
|
|
from .models import BasicFood, TransformedFood, Allergen
|
2024-05-24 19:49:23 +00:00
|
|
|
|
|
|
|
def test_basic_foodform(request):
|
2024-05-25 13:27:26 +00:00
|
|
|
return render(request,'food/test.html', {"form": BasicFoodForms()})
|
2024-05-24 19:49:23 +00:00
|
|
|
|
|
|
|
def test_transformed_foodform(request):
|
2024-05-25 13:27:26 +00:00
|
|
|
return render(request,'food/transformed_food_form.html', {"form": TransformedFoodForms()})
|
2024-05-24 19:49:23 +00:00
|
|
|
|
|
|
|
def test_allergenform(request):
|
|
|
|
return render(request,'food/test.html', {"form": AllergenForms()})
|
|
|
|
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
2024-05-24 19:49:23 +00:00
|
|
|
"""
|
|
|
|
A view to add a basic food
|
|
|
|
"""
|
2024-05-25 13:27:26 +00:00
|
|
|
model = BasicFood
|
|
|
|
form_class = BasicFoodForms
|
|
|
|
template_name = 'food/basic_food_form.html'
|
|
|
|
second_form = AllergenForms
|
2024-05-24 19:49:23 +00:00
|
|
|
extra_context = {"title": _("Add a new aliment")}
|
|
|
|
|
|
|
|
def get_sample_object(self):
|
2024-05-25 13:27:26 +00:00
|
|
|
return Allergen(
|
|
|
|
groundnut = "False",
|
|
|
|
)
|
|
|
|
'''
|
|
|
|
return BasicFood(
|
2024-05-24 19:49:23 +00:00
|
|
|
name="",
|
|
|
|
is_DLC=False,
|
|
|
|
is_DDM=False,
|
|
|
|
expiry_date=timezone.now(),
|
|
|
|
label='pic/default.png',
|
|
|
|
)
|
2024-05-25 13:27:26 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.creater = self.request.user
|
|
|
|
basic_food_form = BasicFoodForms(data=self.request.POST)
|
|
|
|
allergen_form = AllergenForms(data=self.request.POST)
|
|
|
|
if not basic_food_form.is_valid() or not allergen_form.is_valid():
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
# Save the aliment and the allergens associed
|
|
|
|
basic_food = form.save(commit=False)
|
|
|
|
basic_food._force_save = True
|
|
|
|
basic_food.save()
|
|
|
|
basic_food.refresh_from_db()
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
self.object.refresh_from_db()
|
|
|
|
# return reverse_lazy('food:basicfood', kwargs={"pk": self.object.pk})
|
|
|
|
return '0'
|
|
|
|
|
|
|
|
class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
|
|
|
"""
|
|
|
|
A view to add a tranformed food
|
|
|
|
"""
|
|
|
|
model = TransformedFood
|
|
|
|
template_name = 'food/transformed_food_form.html'
|
|
|
|
form_class = TransformedFoodForms
|
|
|
|
extra_context = {"title": _("Add a new meal")}
|
|
|
|
|
|
|
|
def get_sample_object(self):
|
|
|
|
return TransformedFood(
|
|
|
|
name="",
|
|
|
|
creation_date=timezone.now(),
|
|
|
|
)
|
2024-05-24 19:49:23 +00:00
|
|
|
|
|
|
|
@transaction.atomic
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.creater = self.request.user
|
2024-05-25 13:27:26 +00:00
|
|
|
transformed_food_form = TransformedFoodForms(data=self.request.POST)
|
|
|
|
if not transformed_food_form.is_valid():
|
|
|
|
return self.form_invalid(form)
|
|
|
|
|
|
|
|
# Save the aliment and allergens associated
|
|
|
|
transformed_food = form.save(commit=False)
|
|
|
|
# Without microbiologic analysis, the time of conservation is 3 days
|
|
|
|
transformed_food.expiry_date = transformed_food.creation_date + timedelta(days = 3)
|
|
|
|
transformed_food._force_save = True
|
|
|
|
transformed_food.save()
|
|
|
|
transformed_food.refresh_from_db()
|
2024-05-24 19:49:23 +00:00
|
|
|
return super().form_valid(form)
|
2024-05-17 18:46:38 +00:00
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
|
2024-05-24 19:49:23 +00:00
|
|
|
def get_success_url(self, **kwargs):
|
2024-05-25 13:27:26 +00:00
|
|
|
self.object.refresh_from_db()
|
|
|
|
# return reverse_lazy('food:tranformed_food', kwargs={"pk": self.object.pk})
|
|
|
|
return '1'
|