mirror of
https://gitlab.crans.org/bde/nk20
synced 2025-06-21 09:58:23 +02:00
Implementing QRcode creation, modifying Allergen model and creating of few views
This commit is contained in:
@ -1,26 +1,74 @@
|
||||
# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from django.urls import reverse_lazy
|
||||
from datetime import timedelta
|
||||
|
||||
from django.db import transaction
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.http import HttpResponseRedirect
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.utils import timezone
|
||||
from datetime import timedelta
|
||||
from django.views.generic import DetailView, UpdateView, TemplateView
|
||||
from permission.views import ProtectQuerysetMixin, ProtectedCreateView
|
||||
from django.shortcuts import render
|
||||
|
||||
from .forms import BasicFoodForms, TransformedFoodForms, AllergenForms
|
||||
from .models import BasicFood, TransformedFood, Allergen
|
||||
from .forms import BasicFoodForms, TransformedFoodForms
|
||||
from .models import BasicFood, Food, QRCode, TransformedFood
|
||||
|
||||
|
||||
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
|
||||
#####################################################################
|
||||
class QRCodeView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
||||
"""
|
||||
A view to add a basic food
|
||||
"""
|
||||
model = QRCode
|
||||
extra_context = {"title": _("Add a new meal")}
|
||||
context_object_name = "qrcode"
|
||||
slug_field = "qr_code_number"
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
qrcode = kwargs["slug"]
|
||||
if self.model.objects.filter(qr_code_number=qrcode).count() > 0:
|
||||
return super().get(*args, **kwargs)
|
||||
else:
|
||||
return HttpResponseRedirect(reverse("food:qrcode_create", kwargs=kwargs))
|
||||
|
||||
|
||||
class QRCodeCreateView(ProtectQuerysetMixin, LoginRequiredMixin, TemplateView):
|
||||
"""
|
||||
A view to add a basic food
|
||||
"""
|
||||
template_name = 'food/create_qrcode_form.html'
|
||||
extra_context = {"title": _("Add a new aliment")}
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context["slug"] = kwargs["slug"]
|
||||
return context
|
||||
|
||||
|
||||
class FoodView(ProtectQuerysetMixin, LoginRequiredMixin, DetailView):
|
||||
"""
|
||||
A view to add a basic food
|
||||
"""
|
||||
model = Food
|
||||
extra_context = {"title": _("Add a new meal")}
|
||||
context_object_name = "food"
|
||||
|
||||
|
||||
class FoodCreateView(ProtectQuerysetMixin, LoginRequiredMixin, TemplateView):
|
||||
"""
|
||||
A view to add a basic food
|
||||
"""
|
||||
template_name = 'food/create_food_form.html'
|
||||
extra_context = {"title": _("Add a new aliment")}
|
||||
|
||||
|
||||
class BasicFoodFormView(ProtectQuerysetMixin):
|
||||
#####################################################################
|
||||
# TO DO
|
||||
# - fix picture save
|
||||
# - implement solution crop and convert image (reuse or recode ImageForm from members apps)
|
||||
#####################################################################
|
||||
"""
|
||||
A view to add a basic food
|
||||
"""
|
||||
@ -28,28 +76,18 @@ class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
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():
|
||||
if not basic_food_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.arrival_date = timezone.now()
|
||||
basic_food._force_save = True
|
||||
basic_food.save()
|
||||
basic_food.refresh_from_db()
|
||||
@ -57,15 +95,74 @@ class BasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
|
||||
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'
|
||||
return reverse('food:food_view', kwargs={"pk": self.object.pk})
|
||||
|
||||
class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
###############################################
|
||||
# TO DO
|
||||
# -redirect to another view after submit
|
||||
###############################################
|
||||
|
||||
class BasicFoodUpdateView(BasicFoodFormView, LoginRequiredMixin, UpdateView):
|
||||
pass
|
||||
|
||||
|
||||
class BasicFoodCreateView(BasicFoodFormView, ProtectedCreateView):
|
||||
def get_sample_object(self):
|
||||
return BasicFood(
|
||||
name="",
|
||||
expiry_date=timezone.now(),
|
||||
)
|
||||
|
||||
|
||||
class QRCodeBasicFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
#####################################################################
|
||||
# TO DO
|
||||
# - fix picture save
|
||||
# - implement solution crop and convert image (reuse or recode ImageForm from members apps)
|
||||
#####################################################################
|
||||
"""
|
||||
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")}
|
||||
|
||||
@transaction.atomic
|
||||
def form_valid(self, form):
|
||||
form.instance.creater = self.request.user
|
||||
basic_food_form = BasicFoodForms(data=self.request.POST)
|
||||
if not basic_food_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()
|
||||
|
||||
qrcode = QRCode()
|
||||
qrcode.qr_code_number = self.kwargs['slug']
|
||||
qrcode.food_container = basic_food
|
||||
qrcode.save()
|
||||
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_success_url(self, **kwargs):
|
||||
self.object.refresh_from_db()
|
||||
return reverse('food:food_view', kwargs={"pk": self.object.pk})
|
||||
|
||||
def get_sample_object(self):
|
||||
return BasicFood(
|
||||
name="",
|
||||
expiry_date=timezone.now(),
|
||||
)
|
||||
|
||||
|
||||
class TransformedFoodFormView(ProtectQuerysetMixin):
|
||||
#####################################################################
|
||||
# TO DO
|
||||
# - fix picture save
|
||||
# - implement solution crop and convert image (reuse or recode ImageForm from members apps)
|
||||
#####################################################################
|
||||
"""
|
||||
A view to add a tranformed food
|
||||
"""
|
||||
@ -73,13 +170,7 @@ class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
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
|
||||
@ -90,15 +181,71 @@ class TransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
# 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.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()
|
||||
return reverse('food:food_view', kwargs={"pk": self.object.pk})
|
||||
|
||||
|
||||
class TransformedFoodUpdateView(TransformedFoodFormView, LoginRequiredMixin, UpdateView):
|
||||
pass
|
||||
|
||||
|
||||
class TransformedFoodCreateView(TransformedFoodFormView, ProtectedCreateView):
|
||||
def get_sample_object(self):
|
||||
return TransformedFood(
|
||||
name="",
|
||||
creation_date=timezone.now(),
|
||||
)
|
||||
|
||||
|
||||
class QRCodeTransformedFoodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
||||
#####################################################################
|
||||
# TO DO
|
||||
# - fix picture save
|
||||
# - implement solution crop and convert image (reuse or recode ImageForm from members apps)
|
||||
#####################################################################
|
||||
"""
|
||||
A view to add a basic food
|
||||
"""
|
||||
model = TransformedFood
|
||||
template_name = 'food/transformed_food_form.html'
|
||||
form_class = TransformedFoodForms
|
||||
extra_context = {"title": _("Add a new meal")}
|
||||
|
||||
@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()
|
||||
|
||||
qrcode = QRCode()
|
||||
qrcode.qr_code_number = self.kwargs['slug']
|
||||
qrcode.food_container = transformed_food
|
||||
qrcode.save()
|
||||
|
||||
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'
|
||||
return reverse('food:food_view', kwargs={"pk": self.object.pk})
|
||||
|
||||
def get_sample_object(self):
|
||||
return BasicFood(
|
||||
name="",
|
||||
expiry_date=timezone.now(),
|
||||
)
|
||||
|
Reference in New Issue
Block a user