From 57cf9f2c5c5dc31fbec43d01663340dce4996391 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 | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/calebasse/agenda/forms.py b/calebasse/agenda/forms.py index 73a946b..7e919dc 100644 --- a/calebasse/agenda/forms.py +++ b/calebasse/agenda/forms.py @@ -1,8 +1,10 @@ # -*- 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 django.utils.translation import ugettext_lazy as _ from ..dossiers.models import PatientRecord from ..personnes.models import Worker @@ -48,8 +50,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.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 +65,29 @@ 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(_(u'This field is required.')) + 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 ValueError + return duration + except ValueError: + raise forms.ValidationError(_(u'Le champ doit contenir uniquement des chiffres')) + raise forms.ValidationError(_(u'This field is required.')) def clean_patient(self): patients = self.cleaned_data['patient'] -- 2.0.1