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
|
|
|
|
'''
|
|
|
|
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
|
|
|
|
|
|
|
|
from .forms import Basic_foodForms, Transformed_foodForms, AllergenForms
|
|
|
|
from .models import Basic_food
|
|
|
|
|
|
|
|
def test_basic_foodform(request):
|
|
|
|
return render(request,'food/test.html', {"form": Basic_foodForms()})
|
|
|
|
|
|
|
|
def test_transformed_foodform(request):
|
|
|
|
return render(request,'food/test.html', {"form": Transformed_foodForms()})
|
|
|
|
|
|
|
|
def test_allergenform(request):
|
|
|
|
return render(request,'food/test.html', {"form": AllergenForms()})
|
|
|
|
|
|
|
|
|
|
|
|
class Basic_foodCreateView(ProtectQuerysetMixin, ProtectedCreateView):
|
|
|
|
"""
|
|
|
|
A view to add a basic food
|
|
|
|
"""
|
|
|
|
model = Basic_food
|
|
|
|
form_class = Basic_foodForms
|
|
|
|
extra_context = {"title": _("Add a new aliment")}
|
|
|
|
|
|
|
|
def get_sample_object(self):
|
|
|
|
return Basic_food(
|
|
|
|
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
|
|
|
|
return super().form_valid(form)
|
2024-05-17 18:46:38 +00:00
|
|
|
|
2024-05-24 19:49:23 +00:00
|
|
|
def get_success_url(self, **kwargs):
|
|
|
|
self.objects.refresh_from_db()
|
|
|
|
return reverse_lazy('food:basic_food', kwargs={"pk": self.object.pk})
|