From d5c271b3a0994c58d970365ae2d9190624277e01 Mon Sep 17 00:00:00 2001 From: Serghei MIHAI Date: Wed, 16 Jul 2014 11:09:37 +0200 Subject: [PATCH] agenda: allow empty time and duration for events of type "TELEPHONE" and "COURRIEL" Closes #5015 --- calebasse/agenda/forms.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/calebasse/agenda/forms.py b/calebasse/agenda/forms.py index 73a946b..daaaa49 100644 --- a/calebasse/agenda/forms.py +++ b/calebasse/agenda/forms.py @@ -1,8 +1,9 @@ # -*- coding: utf-8 -*- -from datetime import datetime, timedelta +from datetime import datetime, timedelta, time from django import forms +from django.db.models import Q from ..dossiers.models import PatientRecord from ..personnes.models import Worker @@ -48,8 +49,13 @@ class NewAppointmentForm(BaseForm): super(NewAppointmentForm, self).__init__(instance=instance, **kwargs) self.fields['date'].css = 'datepicker' self.fields['participants'].required = True + self.fields['time'].required = False + self.fields['duration'].required = False if service: self.service = service + self.special_types = [str(act.id) for act in ActType.objects.for_service(self.service).filter(Q(name__iexact='courriel') + | Q(name__iexact='telephone') + | Q(name__iexact='téléphone'))] self.fields['participants'].queryset = \ Worker.objects.for_service(service) self.fields['patient'].queryset = \ @@ -58,12 +64,31 @@ class NewAppointmentForm(BaseForm): ActType.objects.for_service(service) \ .order_by('name') + def clean_time(self): + act_type = self.data.get('act_type') + # act type is available as raw data from the post request + if act_type in self.special_types: + return time(8, 0) + if self.cleaned_data['time']: + return self.cleaned_data['time'] + raise forms.ValidationError('Ce champs est obligatoire') + + def clean_duration(self): - duration = self.cleaned_data['duration'] - try: - return int(duration) - except ValueError: - raise forms.ValidationError('Veuillez saisir un entier') + act_type = self.data.get('act_type') + if act_type in self.special_types: + return 10 + + if self.cleaned_data['duration']: + duration = self.cleaned_data['duration'] + try: + duration = int(duration) + if duration <= 0: + raise forms.ValidationError('Le champ doit contenir un chiffre positif') + return duration + except ValueError: + raise forms.ValidationError('Le champ doit contenir un chiffre') + raise forms.ValidationError('Ce champs est obligatoire') def clean_patient(self): patients = self.cleaned_data['patient'] -- 2.0.1