création d'un form pour l'ajout d'aliments basiques

This commit is contained in:
quark 2024-05-24 21:49:23 +02:00
parent 708216a67f
commit 0c3e712f8f
6 changed files with 115 additions and 8 deletions

View File

@ -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):
"""

View File

@ -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'),
),
]

View File

@ -176,6 +176,7 @@ class Basic_food(models.Model):
verbose_name=_('expiry date'),
default=timezone.now,
blank=True,
null = True,
)
owner = models.ForeignKey(

View File

@ -0,0 +1,20 @@
{% extends "base.html" %}
{% comment %}
SPDX-License-Identifier: GPL-3.0-or-later
{% endcomment %}
{% load i18n crispy_forms_tags %}
{% block content %}
<div class="card bg-white mb-3">
<h3 class="card-header text-center">
{{ title }}
</h3>
<div class="card-body">
<form method="post">
{% csrf_token %}
{{ form|crispy }}
<button class="btn btn-primary" type="submit">{% trans "Submit"%}</button>
</form>
</div>
</div>
{% endblock %}

View File

@ -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'),
]

View File

@ -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})