forms.py
8.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
"""
Copyright 2016, 2017 UFPE - Universidade Federal de Pernambuco
Este arquivo é parte do programa Amadeus Sistema de Gestão de Aprendizagem, ou simplesmente Amadeus LMS
O Amadeus LMS é um software livre; você pode redistribui-lo e/ou modifica-lo dentro dos termos da Licença Pública Geral GNU como publicada pela Fundação do Software Livre (FSF); na versão 2 da Licença.
Este programa é distribuído na esperança que possa ser útil, mas SEM NENHUMA GARANTIA; sem uma garantia implícita de ADEQUAÇÃO a qualquer MERCADO ou APLICAÇÃO EM PARTICULAR. Veja a Licença Pública Geral GNU para maiores detalhes.
Você deve ter recebido uma cópia da Licença Pública Geral GNU, sob o título "LICENSE", junto com este programa, se não, escreva para a Fundação do Software Livre (FSF) Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
"""
from django import forms
from django.utils.translation import ugettext_lazy as _
import datetime
from users.models import User
from .models import Subject, Tag
class ParticipantsMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
label = str(obj) + " - (" + obj.email + ")"
return label
class CreateSubjectForm(forms.ModelForm):
category_id = None
students = ParticipantsMultipleChoiceField(queryset = User.objects.all(), required = False)
professor = ParticipantsMultipleChoiceField(queryset = User.objects.all(), required = False)
def __init__(self, *args, **kwargs):
super(CreateSubjectForm, self).__init__(*args, **kwargs)
if kwargs['initial']:
if kwargs['initial']['category']:
categories = kwargs['initial']['category']
if categories.count() > 0:
self.category_id = categories[0].id
if not kwargs['instance'] is None:
self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
# TODO: Define form fields here
tags = forms.CharField(label = _('Tags'), required = False)
class Meta:
model = Subject
fields = ('name', 'description_brief', 'description', 'subscribe_begin', 'subscribe_end', 'init_date', 'end_date',
'visible', 'professor', 'students', )
widgets = {
'description_brief': forms.Textarea,
'description': forms.Textarea,
'professor': forms.SelectMultiple,
'students': forms.SelectMultiple,
}
def save(self, commit=True):
super(CreateSubjectForm, self).save(commit = True)
self.instance.save()
previous_tags = self.instance.tags.all()
tags = self.cleaned_data['tags'].split(",")
#Excluding unwanted tags
for prev in previous_tags:
if not prev.name in tags:
self.instance.tags.remove(prev)
for tag in tags:
tag = tag.strip()
exist = Tag.objects.filter(name = tag).exists()
if exist:
new_tag = Tag.objects.get(name = tag)
else:
new_tag = Tag.objects.create(name = tag)
if not new_tag in self.instance.tags.all():
self.instance.tags.add(new_tag)
return self.instance
def clean_name(self):
name = self.cleaned_data.get('name')
if self.instance.id:
same_name = Subject.objects.filter(name__unaccent__iexact = name, category = self.category_id).exclude(id = self.instance.id)
else:
same_name = Subject.objects.filter(name__unaccent__iexact = name, category = self.category_id)
if same_name.count() > 0:
self._errors['name'] = [_('There is another subject with this name, try another one.')]
return ValueError
return name
def clean_subscribe_begin(self):
subscribe_begin = self.cleaned_data['subscribe_begin']
if subscribe_begin < datetime.datetime.today().date():
self._errors['subscribe_begin'] = [_('This date must be today or after')]
return ValueError
return subscribe_begin
def clean_subscribe_end(self):
subscribe_end = self.cleaned_data['subscribe_end']
subscribe_begin = self.cleaned_data['subscribe_begin']
if subscribe_begin is ValueError or subscribe_end < subscribe_begin:
self._errors['subscribe_end'] = [_('This date must be equal subscribe begin or after')]
return ValueError
return subscribe_end
def clean_init_date(self):
init_date = self.cleaned_data['init_date']
subscribe_end = self.cleaned_data['subscribe_end']
if subscribe_end is ValueError or init_date <= subscribe_end:
self._errors['init_date'] = [_('This date must be after subscribe end')]
return ValueError
return init_date
def clean_end_date(self):
end_date = self.cleaned_data['end_date']
init_date = self.cleaned_data['init_date']
if init_date is ValueError or end_date < init_date:
self._errors['end_date'] = [_('This date must be equal init date or after')]
return ValueError
return end_date
class UpdateSubjectForm(forms.ModelForm):
students = ParticipantsMultipleChoiceField(queryset = User.objects.all(), required = False)
professor = ParticipantsMultipleChoiceField(queryset = User.objects.all(), required = False)
def __init__(self, *args, **kwargs):
super(UpdateSubjectForm, self).__init__(*args, **kwargs)
if not kwargs['instance'] is None:
self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True))
# TODO: Define form fields here
tags = forms.CharField(label = _('Tags'), required = False)
class Meta:
model = Subject
fields = ('name', 'description_brief', 'description', 'subscribe_begin', 'subscribe_end', 'init_date', 'end_date',
'visible', 'professor', 'students', )
widgets = {
'description_brief': forms.Textarea,
'description': forms.Textarea,
'professor': forms.SelectMultiple,
'students': forms.SelectMultiple,
}
def save(self, commit=True):
super(UpdateSubjectForm, self).save(commit = True)
self.instance.save()
previous_tags = self.instance.tags.all()
tags = self.cleaned_data['tags'].split(",")
#Excluding unwanted tags
for prev in previous_tags:
if not prev.name in tags:
self.instance.tags.remove(prev)
for tag in tags:
tag = tag.strip()
exist = Tag.objects.filter(name = tag).exists()
if exist:
new_tag = Tag.objects.get(name = tag)
else:
new_tag = Tag.objects.create(name = tag)
if not new_tag in self.instance.tags.all():
self.instance.tags.add(new_tag)
return self.instance
def clean_name(self):
name = self.cleaned_data.get('name')
categoria = self.instance.category
if self.instance.id:
same_name = Subject.objects.filter(name__unaccent__iexact = name , category = categoria).exclude(id = self.instance.id)
else:
same_name = Subject.objects.filter(name__unaccent__iexact = name, category = categoria)
if same_name.count() > 0:
self._errors['name'] = [_('There is another subject with this name, try another one.')]
return name
# def clean_subscribe_begin(self):
# subscribe_begin = self.cleaned_data['subscribe_begin']
#
# if subscribe_begin < datetime.datetime.today().date() and subscribe_begin < self.instance.subscribe_begin:
# self._errors['subscribe_begin'] = [_('This date must be today or after')]
# return ValueError
#
# return subscribe_begin
def clean_subscribe_end(self):
subscribe_end = self.cleaned_data['subscribe_end']
subscribe_begin = self.cleaned_data['subscribe_begin']
if subscribe_begin is ValueError or subscribe_end < subscribe_begin:
self._errors['subscribe_end'] = [_('This date must be equal subscribe begin or after')]
return ValueError
return subscribe_end
def clean_init_date(self):
init_date = self.cleaned_data['init_date']
subscribe_end = self.cleaned_data['subscribe_end']
if subscribe_end is ValueError or init_date <= subscribe_end:
self._errors['init_date'] = [_('This date must be after subscribe end')]
return ValueError
return init_date
def clean_end_date(self):
end_date = self.cleaned_data['end_date']
init_date = self.cleaned_data['init_date']
if init_date is ValueError or end_date < init_date:
self._errors['end_date'] = [_('This date must be equal init date or after')]
return ValueError
return end_date
class CreateTagForm(forms.ModelForm):
class Meta:
model = Tag
fields = ('name',)