1
|
# -*- coding: utf-8 -*-
|
2
|
|
3
|
from datetime import datetime, timedelta
|
4
|
|
5
|
from django import forms
|
6
|
|
7
|
from ..dossiers.models import PatientRecord
|
8
|
from ..personnes.models import Worker
|
9
|
from ..ressources.models import ActType
|
10
|
from ..middleware.request import get_request
|
11
|
|
12
|
from ajax_select import make_ajax_field
|
13
|
from ajax_select.fields import AutoCompleteSelectField
|
14
|
from models import Event, EventWithAct, EventType
|
15
|
|
16
|
class BaseForm(forms.ModelForm):
|
17
|
date = forms.DateField(label=u'Date', localize=True)
|
18
|
time = forms.TimeField(label=u'Heure de début')
|
19
|
duration = forms.CharField(label=u'Durée',
|
20
|
help_text=u'en minutes; vous pouvez utiliser la roulette de votre souris.')
|
21
|
participants = make_ajax_field(EventWithAct, 'participants', 'worker-or-group', True)
|
22
|
|
23
|
|
24
|
class NewAppointmentForm(BaseForm):
|
25
|
patient = make_ajax_field(EventWithAct, 'patient', 'patientrecord', False)
|
26
|
|
27
|
class Meta:
|
28
|
model = EventWithAct
|
29
|
fields = (
|
30
|
'start_datetime',
|
31
|
'date',
|
32
|
'time',
|
33
|
'duration',
|
34
|
'patient',
|
35
|
'participants',
|
36
|
'ressource',
|
37
|
'act_type',
|
38
|
'description',
|
39
|
'recurrence_periodicity',
|
40
|
'recurrence_end_date'
|
41
|
)
|
42
|
widgets = {
|
43
|
'start_datetime': forms.HiddenInput,
|
44
|
}
|
45
|
|
46
|
|
47
|
|
48
|
def __init__(self, instance, service=None, **kwargs):
|
49
|
self.service = None
|
50
|
super(NewAppointmentForm, self).__init__(instance=instance, **kwargs)
|
51
|
self.fields['date'].css = 'datepicker'
|
52
|
self.fields['participants'].required = True
|
53
|
if service:
|
54
|
self.service = service
|
55
|
self.fields['participants'].queryset = \
|
56
|
Worker.objects.for_service(service)
|
57
|
self.fields['patient'].queryset = \
|
58
|
PatientRecord.objects.for_service(service)
|
59
|
self.fields['act_type'].queryset = \
|
60
|
ActType.objects.for_service(service) \
|
61
|
.order_by('name')
|
62
|
|
63
|
def clean_duration(self):
|
64
|
duration = self.cleaned_data['duration']
|
65
|
try:
|
66
|
return int(duration)
|
67
|
except ValueError:
|
68
|
return 0
|
69
|
|
70
|
def clean(self):
|
71
|
cleaned_data = super(NewAppointmentForm, self).clean()
|
72
|
if not cleaned_data.get('recurrence_periodicity'):
|
73
|
cleaned_data['recurrence_end_date'] = None
|
74
|
if cleaned_data.has_key('date') and cleaned_data.has_key('time'):
|
75
|
cleaned_data['start_datetime'] = datetime.combine(cleaned_data['date'],
|
76
|
cleaned_data['time'])
|
77
|
return cleaned_data
|
78
|
|
79
|
def save(self, commit=True):
|
80
|
appointment = super(NewAppointmentForm, self).save(commit=False)
|
81
|
appointment.start_datetime = datetime.combine(self.cleaned_data['date'],
|
82
|
self.cleaned_data['time'])
|
83
|
appointment.end_datetime = appointment.start_datetime + timedelta(
|
84
|
minutes=self.cleaned_data['duration'])
|
85
|
appointment.creator = get_request().user
|
86
|
appointment.clean()
|
87
|
if commit:
|
88
|
appointment.save()
|
89
|
appointment.services = [self.service]
|
90
|
return appointment
|
91
|
|
92
|
class UpdateAppointmentForm(NewAppointmentForm):
|
93
|
|
94
|
class Meta(NewAppointmentForm.Meta):
|
95
|
fields = (
|
96
|
'start_datetime',
|
97
|
'date',
|
98
|
'time',
|
99
|
'duration',
|
100
|
'patient',
|
101
|
'participants',
|
102
|
'ressource',
|
103
|
'act_type',
|
104
|
)
|
105
|
|
106
|
|
107
|
class UpdatePeriodicAppointmentForm(NewAppointmentForm):
|
108
|
recurrence_periodicity = forms.ChoiceField(label=u"Périodicité",
|
109
|
choices=Event.PERIODICITIES, required=True)
|
110
|
|
111
|
def clean(self):
|
112
|
cleaned_data = super(UpdatePeriodicAppointmentForm, self).clean()
|
113
|
acts = self.instance.act_set.filter(is_billed=True).order_by('-date')
|
114
|
if acts and cleaned_data.get('recurrence_end_date'):
|
115
|
recurrence_end_date = cleaned_data['recurrence_end_date']
|
116
|
if recurrence_end_date < acts[0].date:
|
117
|
self._errors['recurrence_end_date'] = self.error_class([
|
118
|
u"La date doit être supérieur au dernier acte facturé de la récurrence"])
|
119
|
return cleaned_data
|
120
|
|
121
|
class DisablePatientAppointmentForm(UpdateAppointmentForm):
|
122
|
|
123
|
def __init__(self, instance, service=None, **kwargs):
|
124
|
super(DisablePatientAppointmentForm, self).__init__(instance,
|
125
|
service, **kwargs)
|
126
|
if instance and instance.pk:
|
127
|
self.fields['patient'].required = False
|
128
|
self.fields['patient'].widget.attrs['disabled'] = 'disabled'
|
129
|
|
130
|
def clean_patient(self):
|
131
|
instance = getattr(self, 'instance', None)
|
132
|
if instance:
|
133
|
return instance.patient
|
134
|
else:
|
135
|
return self.cleaned_data.get('patient', None)
|
136
|
|
137
|
|
138
|
class NewEventForm(BaseForm):
|
139
|
title = forms.CharField(label=u"Complément de l'intitulé", max_length=32, required=False)
|
140
|
|
141
|
class Meta:
|
142
|
model = Event
|
143
|
fields = (
|
144
|
'start_datetime',
|
145
|
'title',
|
146
|
'date',
|
147
|
'time',
|
148
|
'duration',
|
149
|
'ressource',
|
150
|
'participants',
|
151
|
'event_type',
|
152
|
'services',
|
153
|
'description',
|
154
|
'recurrence_periodicity',
|
155
|
'recurrence_end_date'
|
156
|
)
|
157
|
widgets = {
|
158
|
'start_datetime': forms.HiddenInput,
|
159
|
'services': forms.CheckboxSelectMultiple,
|
160
|
}
|
161
|
|
162
|
def __init__(self, instance, service=None, **kwargs):
|
163
|
self.service = service
|
164
|
super(NewEventForm, self).__init__(instance=instance, **kwargs)
|
165
|
self.fields['date'].css = 'datepicker'
|
166
|
self.fields['event_type'].queryset = \
|
167
|
EventType.objects.exclude(id=1).exclude(id=3).order_by('rank', 'label')
|
168
|
|
169
|
def clean_duration(self):
|
170
|
duration = self.cleaned_data['duration']
|
171
|
try:
|
172
|
return int(duration)
|
173
|
except:
|
174
|
raise forms.ValidationError('Veuillez saisir un entier')
|
175
|
|
176
|
def save(self, commit=True):
|
177
|
event = super(NewEventForm, self).save(commit=False)
|
178
|
event.start_datetime = datetime.combine(self.cleaned_data['date'],
|
179
|
self.cleaned_data['time'])
|
180
|
event.end_datetime = event.start_datetime + timedelta(
|
181
|
minutes=self.cleaned_data['duration'])
|
182
|
event.creator = get_request().user
|
183
|
event.clean()
|
184
|
if commit:
|
185
|
event.save()
|
186
|
event.services = [self.service]
|
187
|
return event
|
188
|
|
189
|
def clean(self):
|
190
|
cleaned_data = super(NewEventForm, self).clean()
|
191
|
if cleaned_data.has_key('date') and cleaned_data.has_key('time'):
|
192
|
cleaned_data['start_datetime'] = datetime.combine(cleaned_data['date'],
|
193
|
cleaned_data['time'])
|
194
|
if not cleaned_data.get('recurrence_periodicity'):
|
195
|
cleaned_data['recurrence_end_date'] = None
|
196
|
event_type = cleaned_data.get('event_type')
|
197
|
if event_type and event_type.id == 4 and not cleaned_data.get('title'): # 'Autre'
|
198
|
self._errors['title'] = self.error_class([
|
199
|
u"Ce champ est obligatoire pour les événements de type « Autre »."])
|
200
|
return cleaned_data
|
201
|
|
202
|
class UpdatePeriodicEventForm(NewEventForm):
|
203
|
recurrence_periodicity = forms.ChoiceField(label=u"Périodicité",
|
204
|
choices=Event.PERIODICITIES, required=True)
|
205
|
|
206
|
class UpdateEventForm(NewEventForm):
|
207
|
class Meta(NewEventForm.Meta):
|
208
|
fields = (
|
209
|
'start_datetime',
|
210
|
'title',
|
211
|
'date',
|
212
|
'time',
|
213
|
'duration',
|
214
|
'ressource',
|
215
|
'participants',
|
216
|
'event_type',
|
217
|
'services',
|
218
|
)
|
219
|
|
220
|
class PeriodicEventsSearchForm(forms.Form):
|
221
|
start_date = forms.DateField(localize=True, required = False)
|
222
|
end_date = forms.DateField(required=False, localize=True)
|
223
|
|
224
|
event_type = forms.MultipleChoiceField(
|
225
|
choices=(
|
226
|
(0, 'Rendez-vous patient'),
|
227
|
(1, 'Évènement')),
|
228
|
widget=forms.CheckboxSelectMultiple,
|
229
|
initial=[0,1])
|
230
|
no_end_date = forms.BooleanField(label=u'Sans date de fin', required=False)
|
231
|
patient = AutoCompleteSelectField('patientrecord', required=False, help_text='')
|
232
|
worker = AutoCompleteSelectField('worker', required=False, help_text='')
|
233
|
|
234
|
def clean(self):
|
235
|
cleaned_data = super(PeriodicEventsSearchForm, self).clean()
|
236
|
if cleaned_data.get('start_date') and cleaned_data.get('end_date'):
|
237
|
if cleaned_data['start_date'] > cleaned_data['end_date']:
|
238
|
raise forms.ValidationError(u'La date de début doit être supérieure à la date de fin')
|
239
|
return cleaned_data
|