forms.py
2.68 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
from django import forms
from django.utils.translation import ugettext_lazy as _
import datetime
from django.forms.formsets import BaseFormSet
class BaseResourceAndTagFormset(BaseFormSet):
def clean(self):
"""
Adds validation to check that no two links have the same anchor or URL
and that all links have both an anchor and URL.
"""
print(self.errors)
if any(self.errors):
return
for form in self.forms:
pass
class ResourceAndTagForm(forms.Form):
resource = forms.ChoiceField(label=_("Kind Of Resource"), required=True)
tag = forms.ChoiceField(label=_('Tag'))
def __init__(self, *args, **kwargs):
super(ResourceAndTagForm, self).__init__(*args, **kwargs)
if kwargs.get('initial'):
initial = kwargs['initial']
self.fields['resource'].choices = [(classes.__name__.lower(), classes.__name__.lower()) for classes in initial['class_name']]
self.fields['tag'].choices = [(tag.id, tag.name) for tag in initial['tag']]
class CreateInteractionReportForm(forms.Form):
topic = forms.ChoiceField( label= _("Topics"), required=True)
init_date = forms.DateField(required=True, label= _("Initial Date"))
end_date = forms.DateField(required=True, label= _("Final Date"))
from_mural = forms.BooleanField(required=False, label=_("From Mural"))
from_messages = forms.BooleanField(required=False, label=_("Messages"))
class Meta:
fields = ('topic', 'init_date', 'end_date', 'from_mural' , 'from_messages')
def __init__(self, *args, **kwargs):
super(CreateInteractionReportForm, self).__init__(*args, **kwargs)
initial = kwargs['initial']
topics = list(initial['topic'])
self.subject = initial['subject'] #so we can check date cleaned data
self.fields['topic'].choices = [(topic.id, topic.name) for topic in topics]
self.fields['topic'].choices.append((_("All"), _("All")))
def clean(self):
cleaned_data = super(CreateInteractionReportForm, self).clean()
init_date = cleaned_data.get("init_date")
end_date = cleaned_data.get("end_date")
if init_date and end_date:
if init_date > end_date:
raise forms.ValidationError(_("The initial date can't be after the end one."))
def clean_init_date(self):
init_date = self.cleaned_data['init_date']
if init_date < self.subject.init_date:
self._errors['init_date'] = [_('This date should be right or after %s, which is when the subject started. ') % str(self.subject.init_date)]
return init_date
def clean_end_date(self):
end_date = self.cleaned_data['end_date']
if end_date > self.subject.end_date:
self._errors['end_date'] = [_('This date should be right or before %s, which is when the subject finishes. ') % str(self.subject.end_date)]
return end_date