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
|
2024-05-25 20:34:59 +00:00
|
|
|
|
2024-05-24 19:49:23 +00:00
|
|
|
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 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
|
|
|
|
|
|
|
|
2024-05-25 13:27:26 +00:00
|
|
|
class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
2024-05-25 20:34:59 +00:00
|
|
|
#####################################################################
|
|
|
|
# TO DO
|
|
|
|
# - fix picture save
|
|
|
|
# - implement solution crop and convert image (reuse or recode ImageForm from members apps
|
|
|
|
# - implement AllergenForms
|
|
|
|
# - redirect to another view after the poll is submitted
|
|
|
|
#####################################################################
|
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'
|
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 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)
|
2024-05-25 20:34:59 +00:00
|
|
|
# We assume the date of labeling and the same as the date of arrival
|
|
|
|
basic_food.arrival_date = timezone.now
|
2024-05-25 13:27:26 +00:00
|
|
|
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()
|
2024-05-25 20:34:59 +00:00
|
|
|
# TEMPORARY, I create a fonctionnal view before
|
2024-05-25 13:27:26 +00:00
|
|
|
# return reverse_lazy('food:basicfood', kwargs={"pk": self.object.pk})
|
|
|
|
return '0'
|
|
|
|
|
|
|
|
class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
2024-05-25 20:34:59 +00:00
|
|
|
###############################################
|
|
|
|
# TO DO
|
|
|
|
# -redirect to another view after submit
|
|
|
|
###############################################
|
2024-05-25 13:27:26 +00:00
|
|
|
"""
|
|
|
|
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)
|
2024-05-25 20:34:59 +00:00
|
|
|
# Without microbiological analyzes, the storage time is 3 days
|
2024-05-25 13:27:26 +00:00
|
|
|
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()
|
2024-05-25 20:34:59 +00:00
|
|
|
# TEMPORARY, I create a fonctionnal view before
|
2024-05-25 13:27:26 +00:00
|
|
|
# return reverse_lazy('food:tranformed_food', kwargs={"pk": self.object.pk})
|
|
|
|
return '1'
|