Commit 0c007745e74bc165d11ba25b8443d5e999928fbb
1 parent
632a061b
Exists in
master
and in
3 other branches
resolvendo bugs do tipo As datas de fim do assunto podem ser antes da data de inicio do assunto
Showing
1 changed file
with
64 additions
and
45 deletions
Show diff stats
subjects/forms.py
... | ... | @@ -6,7 +6,7 @@ from .models import Subject, Tag |
6 | 6 | class CreateSubjectForm(forms.ModelForm): |
7 | 7 | def __init__(self, *args, **kwargs): |
8 | 8 | super(CreateSubjectForm, self).__init__(*args, **kwargs) |
9 | - | |
9 | + | |
10 | 10 | if not kwargs['instance'] is None: |
11 | 11 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) |
12 | 12 | |
... | ... | @@ -16,7 +16,7 @@ class CreateSubjectForm(forms.ModelForm): |
16 | 16 | class Meta: |
17 | 17 | model = Subject |
18 | 18 | |
19 | - fields = ('name', 'description_brief', 'description', 'init_date', 'end_date','subscribe_begin', 'subscribe_end', | |
19 | + fields = ('name', 'description_brief', 'description', 'subscribe_begin', 'subscribe_end', 'init_date', 'end_date', | |
20 | 20 | 'visible', 'professor', 'students', ) |
21 | 21 | |
22 | 22 | |
... | ... | @@ -40,7 +40,7 @@ class CreateSubjectForm(forms.ModelForm): |
40 | 40 | for prev in previous_tags: |
41 | 41 | if not prev.name in tags: |
42 | 42 | self.instance.tags.remove(prev) |
43 | - | |
43 | + | |
44 | 44 | for tag in tags: |
45 | 45 | tag = tag.strip() |
46 | 46 | |
... | ... | @@ -56,21 +56,6 @@ class CreateSubjectForm(forms.ModelForm): |
56 | 56 | |
57 | 57 | return self.instance |
58 | 58 | |
59 | - def clean(self): | |
60 | - cleaned_data = super(CreateSubjectForm, self).clean() | |
61 | - subscribe_begin = cleaned_data.get('subscribe_begin') | |
62 | - end_date = cleaned_data.get('end_date') | |
63 | - """if subscribe_begin and end_date: | |
64 | - | |
65 | - if subscribe_begin > end_date: | |
66 | - self._errors['subscribe_begin'] = [_('Subscribe period should be between course time')] | |
67 | - return ValueError | |
68 | - | |
69 | - if cleaned_data['init_date'] > cleaned_data['end_date']: | |
70 | - self._errors['init_date'] = [_('This date must be before end date of the Subject')] | |
71 | - return ValueError""" | |
72 | - return cleaned_data | |
73 | - | |
74 | 59 | def clean_name(self): |
75 | 60 | name = self.cleaned_data.get('name') |
76 | 61 | if self.instance.id: |
... | ... | @@ -86,42 +71,45 @@ class CreateSubjectForm(forms.ModelForm): |
86 | 71 | |
87 | 72 | def clean_subscribe_begin(self): |
88 | 73 | subscribe_begin = self.cleaned_data['subscribe_begin'] |
89 | - | |
74 | + | |
90 | 75 | if subscribe_begin < datetime.datetime.today().date(): |
91 | - self._errors['subscribe_begin'] = [_('this date must be today or after')] | |
76 | + self._errors['subscribe_begin'] = [_('This date must be today or after')] | |
92 | 77 | return ValueError |
93 | - | |
78 | + | |
94 | 79 | return subscribe_begin |
80 | + | |
81 | + def clean_subscribe_end(self): | |
82 | + subscribe_end = self.cleaned_data['subscribe_end'] | |
83 | + subscribe_begin = self.cleaned_data['subscribe_begin'] | |
84 | + | |
85 | + if subscribe_begin is ValueError or subscribe_end < subscribe_begin: | |
86 | + self._errors['subscribe_end'] = [_('This date must be equal subscribe begin or after')] | |
87 | + return ValueError | |
88 | + return subscribe_end | |
89 | + | |
95 | 90 | def clean_init_date(self): |
96 | 91 | init_date = self.cleaned_data['init_date'] |
97 | - | |
92 | + subscribe_end = self.cleaned_data['subscribe_end'] | |
93 | + | |
98 | 94 | |
99 | - if init_date < datetime.datetime.today().date(): | |
100 | - self._errors['init_date'] = [_('this date must be today or after')] | |
95 | + if subscribe_end is ValueError or init_date <= subscribe_end: | |
96 | + self._errors['init_date'] = [_('This date must be after subscribe end')] | |
101 | 97 | return ValueError |
102 | 98 | return init_date |
103 | 99 | |
104 | 100 | def clean_end_date(self): |
105 | 101 | end_date = self.cleaned_data['end_date'] |
102 | + init_date = self.cleaned_data['init_date'] | |
106 | 103 | |
107 | - if end_date < datetime.datetime.today().date(): | |
108 | - self._errors['end_date'] = [_('this date must be today or after')] | |
104 | + if init_date is ValueError or end_date < init_date: | |
105 | + self._errors['end_date'] = [_('This date must be equal init date or after')] | |
109 | 106 | return ValueError |
110 | 107 | return end_date |
111 | 108 | |
112 | - def clean_subscribe_end(self): | |
113 | - subscribe_end = self.cleaned_data['subscribe_end'] | |
114 | - | |
115 | - if subscribe_end < datetime.datetime.today().date(): | |
116 | - self._errors['subscribe_end'] = [_('this date must be today or after')] | |
117 | - return ValueError | |
118 | - return subscribe_end | |
119 | - | |
120 | - | |
121 | 109 | class UpdateSubjectForm(forms.ModelForm): |
122 | 110 | def __init__(self, *args, **kwargs): |
123 | 111 | super(UpdateSubjectForm, self).__init__(*args, **kwargs) |
124 | - | |
112 | + | |
125 | 113 | if not kwargs['instance'] is None: |
126 | 114 | self.initial['tags'] = ", ".join(self.instance.tags.all().values_list("name", flat = True)) |
127 | 115 | |
... | ... | @@ -131,7 +119,7 @@ class UpdateSubjectForm(forms.ModelForm): |
131 | 119 | class Meta: |
132 | 120 | model = Subject |
133 | 121 | |
134 | - fields = ('name', 'description_brief', 'description', 'init_date', 'end_date','subscribe_begin', 'subscribe_end', | |
122 | + fields = ('name', 'description_brief', 'description', 'subscribe_begin', 'subscribe_end', 'init_date', 'end_date', | |
135 | 123 | 'visible', 'professor', 'students', ) |
136 | 124 | |
137 | 125 | |
... | ... | @@ -155,7 +143,7 @@ class UpdateSubjectForm(forms.ModelForm): |
155 | 143 | for prev in previous_tags: |
156 | 144 | if not prev.name in tags: |
157 | 145 | self.instance.tags.remove(prev) |
158 | - | |
146 | + | |
159 | 147 | for tag in tags: |
160 | 148 | tag = tag.strip() |
161 | 149 | |
... | ... | @@ -171,11 +159,6 @@ class UpdateSubjectForm(forms.ModelForm): |
171 | 159 | |
172 | 160 | return self.instance |
173 | 161 | |
174 | - def clean(self): | |
175 | - cleaned_data = super(UpdateSubjectForm, self).clean() | |
176 | - | |
177 | - return cleaned_data | |
178 | - | |
179 | 162 | def clean_name(self): |
180 | 163 | name = self.cleaned_data.get('name') |
181 | 164 | if self.instance.id: |
... | ... | @@ -189,10 +172,46 @@ class UpdateSubjectForm(forms.ModelForm): |
189 | 172 | |
190 | 173 | return name |
191 | 174 | |
192 | - | |
175 | + def clean_subscribe_begin(self): | |
176 | + subscribe_begin = self.cleaned_data['subscribe_begin'] | |
177 | + | |
178 | + print (self.instance.subscribe_begin, "################################") | |
179 | + if subscribe_begin < datetime.datetime.today().date() and subscribe_begin < self.instance.subscribe_begin: | |
180 | + self._errors['subscribe_begin'] = [_('This date must be today or after')] | |
181 | + return ValueError | |
182 | + | |
183 | + return subscribe_begin | |
184 | + | |
185 | + def clean_subscribe_end(self): | |
186 | + subscribe_end = self.cleaned_data['subscribe_end'] | |
187 | + subscribe_begin = self.cleaned_data['subscribe_begin'] | |
188 | + | |
189 | + if subscribe_begin is ValueError or subscribe_end < subscribe_begin: | |
190 | + self._errors['subscribe_end'] = [_('This date must be equal subscribe begin or after')] | |
191 | + return ValueError | |
192 | + return subscribe_end | |
193 | + | |
194 | + def clean_init_date(self): | |
195 | + init_date = self.cleaned_data['init_date'] | |
196 | + subscribe_end = self.cleaned_data['subscribe_end'] | |
197 | + | |
198 | + | |
199 | + if subscribe_end is ValueError or init_date <= subscribe_end: | |
200 | + self._errors['init_date'] = [_('This date must be after subscribe end')] | |
201 | + return ValueError | |
202 | + return init_date | |
203 | + | |
204 | + def clean_end_date(self): | |
205 | + end_date = self.cleaned_data['end_date'] | |
206 | + init_date = self.cleaned_data['init_date'] | |
207 | + | |
208 | + if init_date is ValueError or end_date < init_date: | |
209 | + self._errors['end_date'] = [_('This date must be equal init date or after')] | |
210 | + return ValueError | |
211 | + return end_date | |
212 | + | |
193 | 213 | |
194 | 214 | class CreateTagForm(forms.ModelForm): |
195 | 215 | class Meta: |
196 | 216 | model = Tag |
197 | 217 | fields = ('name',) |
198 | - | |
199 | 218 | \ No newline at end of file | ... | ... |