Amélioration de la gestion des dates et heures dans le formulaire :

- valeur par défaut (Fix #38)
- vérification de la cohérence relative des début et fin
- ajout d'un peu de javascript pour un ajustement de la fin quand on règle le début (un classique)
This commit is contained in:
Jean-Marie Favreau
2023-11-25 14:01:03 +01:00
parent 63e9c57e87
commit 971f8aad32
7 changed files with 166 additions and 28 deletions

View File

@@ -1,11 +1,67 @@
from django.forms import ModelForm
from django.forms import ModelForm, ValidationError, TextInput
from django.views.generic import FormView
from .models import EventSubmissionForm
from .models import EventSubmissionForm, Event
from django.utils.translation import gettext_lazy as _
class EventSubmissionModelForm(ModelForm):
class Meta:
model = EventSubmissionForm
fields = ["url"]
class EventForm(ModelForm):
class Meta:
model = Event
fields = '__all__'
widgets = {
'start_day': TextInput(attrs={'type': 'date', 'onchange': 'update_datetimes(event);', "onfocus": "this.oldvalue = this.value;"}),
'start_time': TextInput(attrs={'type': 'time', 'onchange': 'update_datetimes(event);', "onfocus": "this.oldvalue = this.value;"}),
'end_day': TextInput(attrs={'type': 'date'}),
'end_time': TextInput(attrs={'type': 'time'}),
}
def __init__(self, instance, *args, **kwargs):
is_authenticated = kwargs.pop('is_authenticated', False)
super().__init__(instance=instance, *args, **kwargs)
if not is_authenticated:
del self.fields['status']
def clean_start_day(self):
start_day = self.cleaned_data.get("start_day")
if start_day is not None and start_day < date.today():
raise ValidationError(_("The start date cannot be earler than today."))
return start_day
def clean_end_day(self):
start_day = self.cleaned_data.get("start_day")
end_day = self.cleaned_data.get("end_day")
if end_day is not None and start_day is not None and end_day < start_day:
raise ValidationError(_("The end date must be after the start date."))
if end_day is not None and end_day < date.today():
raise ValidationError(_("The end date cannot be earler than today."))
return end_day
def clean_end_time(self):
start_day = self.cleaned_data.get("start_day")
end_day = self.cleaned_data.get("end_day")
start_time = self.cleaned_data.get("start_time")
end_time = self.cleaned_data.get("end_time")
# same day
if start_day is not None and (end_day is None or start_day == end_day):
# both start and end time are defined
if start_time is not None and end_time is not None:
if start_time > end_time:
raise ValidationError(_("The end time cannot be earlier than the start time."))
return end_time