From 0c3e712f8f813242ce0966bf6252b08bc7848d4b Mon Sep 17 00:00:00 2001 From: quark Date: Fri, 24 May 2024 21:49:23 +0200 Subject: [PATCH] =?UTF-8?q?cr=C3=A9ation=20d'un=20form=20pour=20l'ajout=20?= =?UTF-8?q?d'aliments=20basiques?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/food/forms.py | 8 +-- .../migrations/0002_auto_20240524_2139.py | 19 ++++++ apps/food/models.py | 1 + apps/food/templates/food/basic_food_form.html | 20 ++++++ apps/food/urls.py | 11 +++- apps/food/views.py | 64 ++++++++++++++++++- 6 files changed, 115 insertions(+), 8 deletions(-) create mode 100644 apps/food/migrations/0002_auto_20240524_2139.py create mode 100644 apps/food/templates/food/basic_food_form.html diff --git a/apps/food/forms.py b/apps/food/forms.py index e072c00a..92887c32 100644 --- a/apps/food/forms.py +++ b/apps/food/forms.py @@ -7,7 +7,7 @@ from django import forms from django.utils.translation import gettext_lazy as _ from django.utils import timezone from member.models import Club -from note_kfet.inputs import Autocomplete, DateTimePickerInput +from note_kfet.inputs import Autocomplete, DatePickerInput from note_kfet.middlewares import get_current_request from permission.backends import PermissionBackend @@ -40,12 +40,12 @@ class Basic_foodForms(forms.ModelForm): class Meta: model = Basic_food fields = ('name', 'owner', 'is_DLC', 'is_DDM', 'expiry_date', 'label') - widget = { + widgets = { "owner": Autocomplete( model = Club, attrs = {"api_url": "/api/members/club/"}, ), - 'expiry_date': DateTimePickerInput(), + 'expiry_date': DatePickerInput(), } @@ -78,7 +78,7 @@ class Transformed_foodForms(forms.ModelForm): model = Club, attrs = {"api_url": "/api/members/club/"}, ), - 'creation_date': DateTimePickerInput(), + 'creation_date': DatePickerInput(), } class AllergenForms(forms.ModelForm): """ diff --git a/apps/food/migrations/0002_auto_20240524_2139.py b/apps/food/migrations/0002_auto_20240524_2139.py new file mode 100644 index 00000000..d4423976 --- /dev/null +++ b/apps/food/migrations/0002_auto_20240524_2139.py @@ -0,0 +1,19 @@ +# Generated by Django 2.2.28 on 2024-05-24 19:39 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + dependencies = [ + ('food', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='basic_food', + name='expiry_date', + field=models.DateTimeField(blank=True, default=django.utils.timezone.now, null=True, verbose_name='expiry date'), + ), + ] diff --git a/apps/food/models.py b/apps/food/models.py index 7deeb902..e957ae99 100644 --- a/apps/food/models.py +++ b/apps/food/models.py @@ -176,6 +176,7 @@ class Basic_food(models.Model): verbose_name=_('expiry date'), default=timezone.now, blank=True, + null = True, ) owner = models.ForeignKey( diff --git a/apps/food/templates/food/basic_food_form.html b/apps/food/templates/food/basic_food_form.html new file mode 100644 index 00000000..a56aa063 --- /dev/null +++ b/apps/food/templates/food/basic_food_form.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} +{% load i18n crispy_forms_tags %} + +{% block content %} +
+

+ {{ title }} +

+
+
+ {% csrf_token %} + {{ form|crispy }} + +
+
+
+{% endblock %} diff --git a/apps/food/urls.py b/apps/food/urls.py index 2fcca0db..aff8b8b5 100644 --- a/apps/food/urls.py +++ b/apps/food/urls.py @@ -1,7 +1,16 @@ +# Copyright (C) 2018-2024 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + from django.urls import path from . import views +app_name = 'food' + urlpatterns = [ - path('', views.index, name='index') + path('test_basic_food_form',views.test_basic_foodform), + path('test_transformed_food_form', views.test_transformed_foodform), + path('test_allergen_form', views.test_allergenform), + path('0', views.Basic_foodCreateView.as_view(), name = 'basic_food'), ] + diff --git a/apps/food/views.py b/apps/food/views.py index c4cfa086..43161b0d 100644 --- a/apps/food/views.py +++ b/apps/food/views.py @@ -1,5 +1,63 @@ -from django.shortcuts import render +# 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 from django.http import HttpResponse +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 -def index(request): - return HttpResponse('test') +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) + + def get_success_url(self, **kwargs): + self.objects.refresh_from_db() + return reverse_lazy('food:basic_food', kwargs={"pk": self.object.pk})