nk20/apps/food/views.py

105 lines
3.8 KiB
Python

# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
# SPDX-License-Identifier: GPL-3.0-or-later
from django.urls import reverse_lazy
from django.db import transaction
from django.utils.translation import gettext_lazy as _
from django.utils import timezone
from datetime import timedelta
from permission.views import ProtectQuerysetMixin, ProtectedCreateView
from django.shortcuts import render
from .forms import BasicFoodForms, TransformedFoodForms, AllergenForms
from .models import BasicFood, TransformedFood, Allergen
class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
#####################################################################
# 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
#####################################################################
"""
A view to add a basic food
"""
model = BasicFood
form_class = BasicFoodForms
template_name = 'food/basic_food_form.html'
extra_context = {"title": _("Add a new aliment")}
def get_sample_object(self):
return BasicFood(
name="",
is_DLC=False,
is_DDM=False,
expiry_date=timezone.now(),
label='pic/default.png',
)
@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)
# We assume the date of labeling and the same as the date of arrival
basic_food.arrival_date = timezone.now
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()
# TEMPORARY, I create a fonctionnal view before
# return reverse_lazy('food:basicfood', kwargs={"pk": self.object.pk})
return '0'
class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
###############################################
# TO DO
# -redirect to another view after submit
###############################################
"""
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(),
)
@transaction.atomic
def form_valid(self, form):
form.instance.creater = self.request.user
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 microbiological analyzes, the storage time 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()
return super().form_valid(form)
def get_success_url(self, **kwargs):
self.object.refresh_from_db()
# TEMPORARY, I create a fonctionnal view before
# return reverse_lazy('food:tranformed_food', kwargs={"pk": self.object.pk})
return '1'