1
|
# -*- coding: utf-8 -*-
|
2
|
from django import forms
|
3
|
from django.forms.models import (inlineformset_factory, modelformset_factory,
|
4
|
BaseInlineFormSet)
|
5
|
from django.utils.translation import gettext_lazy as _
|
6
|
from django.contrib.auth.models import User
|
7
|
|
8
|
from calebasse.ressources.models import WorkerType
|
9
|
|
10
|
from models import Worker, UserWorker, TimeTable, Holiday
|
11
|
|
12
|
|
13
|
class UserForm(forms.ModelForm):
|
14
|
error_messages = {
|
15
|
'duplicate_username': _("A user with that username already exists."),
|
16
|
'password_mismatch': _("The two password fields didn't match."),
|
17
|
}
|
18
|
|
19
|
worker = forms.ModelChoiceField(label=_('Personnel'),
|
20
|
queryset=Worker.objects.all(), empty_label=_('None'),
|
21
|
required=False)
|
22
|
password1 = forms.CharField(label=_("Password"),
|
23
|
widget=forms.PasswordInput)
|
24
|
password2 = forms.CharField(label=_("Password confirmation"),
|
25
|
widget=forms.PasswordInput,
|
26
|
help_text=_("Enter the same password as above, for verification."))
|
27
|
|
28
|
def __init__(self, service=None, *args, **kwargs):
|
29
|
self.service = service
|
30
|
super(UserForm, self).__init__(*args, **kwargs)
|
31
|
self.fields['worker'].queryset = Worker.objects.for_service(service)
|
32
|
self.fields['username'].label = _('Identifiant')
|
33
|
if self.instance:
|
34
|
try:
|
35
|
worker = self.instance.userworker.worker
|
36
|
self.fields['worker'].initial = worker.pk
|
37
|
except UserWorker.DoesNotExist:
|
38
|
pass
|
39
|
|
40
|
def clean_username(self):
|
41
|
# Since User.username is unique, this check is redundant,
|
42
|
# but it sets a nicer error message than the ORM. See #13147.
|
43
|
username = self.cleaned_data["username"]
|
44
|
if self.instance is None:
|
45
|
try:
|
46
|
User.objects.get(username=username)
|
47
|
raise forms.ValidationError(self.error_messages['duplicate_username'])
|
48
|
except User.DoesNotExist:
|
49
|
pass
|
50
|
return username
|
51
|
|
52
|
def clean_password2(self):
|
53
|
password1 = self.cleaned_data.get("password1")
|
54
|
password2 = self.cleaned_data.get("password2")
|
55
|
if password1 and password2 and password1 != password2:
|
56
|
raise forms.ValidationError(
|
57
|
self.error_messages['password_mismatch'])
|
58
|
return password2
|
59
|
|
60
|
class Meta:
|
61
|
model = User
|
62
|
fields = ('username', 'email', 'password1', 'password2', 'worker')
|
63
|
|
64
|
def save(self, commit=True):
|
65
|
instance = super(UserForm, self).save(commit=False)
|
66
|
if self.cleaned_data['password1']:
|
67
|
instance.set_password(self.cleaned_data['password1'])
|
68
|
worker = self.cleaned_data.get('worker')
|
69
|
if worker is not None:
|
70
|
self.first_name = worker.first_name
|
71
|
self.last_name = worker.last_name
|
72
|
def save_m2m(self):
|
73
|
try:
|
74
|
userworker = self.instance.userworker
|
75
|
userworker.worker = worker
|
76
|
userworker.save()
|
77
|
except UserWorker.DoesNotExist:
|
78
|
UserWorker.objects.create(id=self.id, worker=worker)
|
79
|
self.save_m2m = save_m2m
|
80
|
else:
|
81
|
self.save_m2m = lambda self: None
|
82
|
if commit:
|
83
|
instance.save()
|
84
|
return instance
|
85
|
|
86
|
class WorkerSearchForm(forms.Form):
|
87
|
last_name = forms.CharField(label=u'Nom', required=False)
|
88
|
first_name = forms.CharField(label=u'Prénom', required=False)
|
89
|
profession = forms.ModelChoiceField(label=u'Profession',
|
90
|
queryset=WorkerType.objects.all(), required=False)
|
91
|
|
92
|
INTERVENE_STATUS_CHOICES = {
|
93
|
'a': u'Thérapeute',
|
94
|
'b': u'Non-thérapeute'
|
95
|
}
|
96
|
|
97
|
intervene_status = forms.MultipleChoiceField(
|
98
|
choices=INTERVENE_STATUS_CHOICES.iteritems(),
|
99
|
widget=forms.CheckboxSelectMultiple,
|
100
|
initial=INTERVENE_STATUS_CHOICES.keys(),
|
101
|
required=False)
|
102
|
|
103
|
class WorkerIdForm(forms.ModelForm):
|
104
|
sex = forms.CharField(label='Genre')
|
105
|
|
106
|
class Meta:
|
107
|
model = Worker
|
108
|
fields = ('last_name', 'first_name', 'sex')
|
109
|
|
110
|
class WorkerServiceForm(forms.ModelForm):
|
111
|
class Meta:
|
112
|
model = Worker
|
113
|
fields = ('services',)
|
114
|
widgets = {
|
115
|
'services': forms.CheckboxSelectMultiple,
|
116
|
}
|
117
|
|
118
|
|
119
|
class BaseTimetableFormSet(BaseInlineFormSet):
|
120
|
def __init__(self, weekday=None, *args, **kwargs):
|
121
|
kwargs['queryset'] = kwargs.get('queryset', TimeTable.objects).filter(weekday=weekday)
|
122
|
super(BaseTimetableFormSet, self).__init__(*args, **kwargs)
|
123
|
|
124
|
|
125
|
TimetableFormSet = inlineformset_factory(Worker, TimeTable,
|
126
|
formset=BaseTimetableFormSet,
|
127
|
fields=('start_time', 'end_time', 'start_date', 'end_date'))
|
128
|
|
129
|
HolidayFormSet = inlineformset_factory(
|
130
|
Worker, Holiday,
|
131
|
fields=('start_date', 'end_date', 'start_time', 'end_time'))
|
132
|
|
133
|
class HolidaySearchForm(forms.Form):
|
134
|
start_date = forms.DateField(required=False)
|
135
|
end_date = forms.DateField(required=False)
|
136
|
|
137
|
def clean(self):
|
138
|
cleaned_data = super(HolidaySearchForm, self).clean()
|
139
|
if cleaned_data.get('start_date') or cleaned_data.get('end_date'):
|
140
|
if not cleaned_data.get('start_date') \
|
141
|
or not cleaned_data.get('end_date'):
|
142
|
raise forms.ValidationError(u'Vous devez fournir une date de début et de fin')
|
143
|
if cleaned_data['start_date'] > cleaned_data['end_date']:
|
144
|
raise forms.ValidationError(u'La date de début doit être supérieure à la date de fin')
|
145
|
return cleaned_data
|
146
|
|
147
|
class YearlyHolidayForm(forms.ModelForm):
|
148
|
for_all_services = forms.BooleanField(required=False)
|
149
|
|
150
|
def save(self, commit=True):
|
151
|
instance = super(YearlyHolidayForm, self).save(commit=commit)
|
152
|
instance.for_all_services = self.cleaned_data.get('for_all_services', False)
|
153
|
return instance
|
154
|
|
155
|
class Meta:
|
156
|
form = Holiday
|
157
|
|
158
|
YearlyHolidayFormSet = modelformset_factory(Holiday,
|
159
|
can_delete=True,
|
160
|
form=YearlyHolidayForm,
|
161
|
fields=('start_date', 'end_date'))
|