root / edumanage / forms.py @ f1149817
History | View | Annotate | Download (5.6 kB)
1 | fa8b2f7d | Leonidas Poulopoulos | from django import forms |
---|---|---|---|
2 | fa8b2f7d | Leonidas Poulopoulos | from django.utils.translation import ugettext as _ |
3 | fa8b2f7d | Leonidas Poulopoulos | from django.utils.translation import ugettext_lazy |
4 | fa8b2f7d | Leonidas Poulopoulos | from edumanage.models import * |
5 | fa8b2f7d | Leonidas Poulopoulos | from django.conf import settings |
6 | fa8b2f7d | Leonidas Poulopoulos | |
7 | 02f2ae43 | Leonidas Poulopoulos | from django.contrib.contenttypes.generic import BaseGenericInlineFormSet |
8 | fa8b2f7d | Leonidas Poulopoulos | |
9 | f1149817 | Leonidas Poulopoulos | import ipaddr |
10 | fa8b2f7d | Leonidas Poulopoulos | |
11 | af9d484b | Leonidas Poulopoulos | import pprint |
12 | f1149817 | Leonidas Poulopoulos | import re |
13 | f1149817 | Leonidas Poulopoulos | |
14 | f1149817 | Leonidas Poulopoulos | FQDN_RE = r'(^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$)'
|
15 | f1149817 | Leonidas Poulopoulos | #FQDN_RE = r'(^[a-z0-9.-]{1,255}$)'
|
16 | af9d484b | Leonidas Poulopoulos | |
17 | fa8b2f7d | Leonidas Poulopoulos | class InstDetailsForm(forms.ModelForm): |
18 | fa8b2f7d | Leonidas Poulopoulos | |
19 | fa8b2f7d | Leonidas Poulopoulos | class Meta: |
20 | f1149817 | Leonidas Poulopoulos | model = InstitutionDetails |
21 | f1149817 | Leonidas Poulopoulos | |
22 | f1149817 | Leonidas Poulopoulos | def clean_oper_name(self): |
23 | f1149817 | Leonidas Poulopoulos | oper_name = self.cleaned_data['oper_name'] |
24 | f1149817 | Leonidas Poulopoulos | institution = self.cleaned_data['institution'] |
25 | f1149817 | Leonidas Poulopoulos | if institution.ertype in [2,3]: |
26 | f1149817 | Leonidas Poulopoulos | if oper_name:
|
27 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["oper_name"] |
28 | f1149817 | Leonidas Poulopoulos | else:
|
29 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError('This field is required.') |
30 | f1149817 | Leonidas Poulopoulos | |
31 | f1149817 | Leonidas Poulopoulos | def clean_number_user(self): |
32 | f1149817 | Leonidas Poulopoulos | number_user = self.cleaned_data['number_user'] |
33 | f1149817 | Leonidas Poulopoulos | institution = self.cleaned_data['institution'] |
34 | f1149817 | Leonidas Poulopoulos | if institution.ertype in [1,3]: |
35 | f1149817 | Leonidas Poulopoulos | if number_user:
|
36 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["number_user"] |
37 | f1149817 | Leonidas Poulopoulos | else:
|
38 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError('This field is required.') |
39 | edac002f | Leonidas Poulopoulos | |
40 | edac002f | Leonidas Poulopoulos | class InstServerForm(forms.ModelForm): |
41 | edac002f | Leonidas Poulopoulos | |
42 | edac002f | Leonidas Poulopoulos | class Meta: |
43 | edac002f | Leonidas Poulopoulos | model = InstServer |
44 | bd92cd64 | Leonidas Poulopoulos | |
45 | bd92cd64 | Leonidas Poulopoulos | def clean_ertype(self): |
46 | bd92cd64 | Leonidas Poulopoulos | ertype = self.cleaned_data['ertype'] |
47 | bd92cd64 | Leonidas Poulopoulos | institution = self.cleaned_data['instid'] |
48 | f1149817 | Leonidas Poulopoulos | inst_type = institution.ertype |
49 | bd92cd64 | Leonidas Poulopoulos | type_list = [inst_type] |
50 | bd92cd64 | Leonidas Poulopoulos | if inst_type == 3: |
51 | bd92cd64 | Leonidas Poulopoulos | type_list = [1, 2, 3] |
52 | bd92cd64 | Leonidas Poulopoulos | if ertype:
|
53 | bd92cd64 | Leonidas Poulopoulos | if ertype not in type_list: |
54 | bd92cd64 | Leonidas Poulopoulos | raise forms.ValidationError('Server type cannot be different than institution type (%s)' %dict(self.fields['ertype'].choices)[inst_type]) |
55 | bd92cd64 | Leonidas Poulopoulos | return self.cleaned_data["ertype"] |
56 | bd92cd64 | Leonidas Poulopoulos | else:
|
57 | bd92cd64 | Leonidas Poulopoulos | raise forms.ValidationError('This field is required.') |
58 | f1149817 | Leonidas Poulopoulos | |
59 | f1149817 | Leonidas Poulopoulos | def clean_port(self): |
60 | f1149817 | Leonidas Poulopoulos | port = self.cleaned_data['port'] |
61 | f1149817 | Leonidas Poulopoulos | institution = self.cleaned_data['instid'] |
62 | f1149817 | Leonidas Poulopoulos | if institution.ertype in [1,3]: |
63 | f1149817 | Leonidas Poulopoulos | if port:
|
64 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["port"] |
65 | f1149817 | Leonidas Poulopoulos | else:
|
66 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError(_('This field is required.')) |
67 | f1149817 | Leonidas Poulopoulos | |
68 | f1149817 | Leonidas Poulopoulos | def clean_acct_port(self): |
69 | f1149817 | Leonidas Poulopoulos | acct_port = self.cleaned_data['acct_port'] |
70 | f1149817 | Leonidas Poulopoulos | institution = self.cleaned_data['instid'] |
71 | f1149817 | Leonidas Poulopoulos | if institution.ertype in [1,3]: |
72 | f1149817 | Leonidas Poulopoulos | if acct_port:
|
73 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["acct_port"] |
74 | f1149817 | Leonidas Poulopoulos | else:
|
75 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError(_('This field is required.')) |
76 | f1149817 | Leonidas Poulopoulos | |
77 | f1149817 | Leonidas Poulopoulos | def clean_timeout(self): |
78 | f1149817 | Leonidas Poulopoulos | timeout = self.cleaned_data['timeout'] |
79 | f1149817 | Leonidas Poulopoulos | institution = self.cleaned_data['instid'] |
80 | f1149817 | Leonidas Poulopoulos | if institution.ertype in [1,3]: |
81 | f1149817 | Leonidas Poulopoulos | if timeout:
|
82 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["timeout"] |
83 | f1149817 | Leonidas Poulopoulos | else:
|
84 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError(_('This field is required.')) |
85 | f1149817 | Leonidas Poulopoulos | |
86 | f1149817 | Leonidas Poulopoulos | def clean_retry(self): |
87 | f1149817 | Leonidas Poulopoulos | retry = self.cleaned_data['retry'] |
88 | f1149817 | Leonidas Poulopoulos | institution = self.cleaned_data['instid'] |
89 | f1149817 | Leonidas Poulopoulos | if institution.ertype in [1,3]: |
90 | f1149817 | Leonidas Poulopoulos | if retry:
|
91 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["retry"] |
92 | f1149817 | Leonidas Poulopoulos | else:
|
93 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError(_('This field is required.')) |
94 | f1149817 | Leonidas Poulopoulos | |
95 | f1149817 | Leonidas Poulopoulos | |
96 | f1149817 | Leonidas Poulopoulos | def clean_host(self): |
97 | f1149817 | Leonidas Poulopoulos | host = self.cleaned_data['host'] |
98 | f1149817 | Leonidas Poulopoulos | if host:
|
99 | f1149817 | Leonidas Poulopoulos | match = re.match(FQDN_RE, host) |
100 | f1149817 | Leonidas Poulopoulos | if not match: |
101 | f1149817 | Leonidas Poulopoulos | print "not match" |
102 | f1149817 | Leonidas Poulopoulos | try:
|
103 | f1149817 | Leonidas Poulopoulos | address = ipaddr.IPNetwork(host) |
104 | f1149817 | Leonidas Poulopoulos | except Exception: |
105 | f1149817 | Leonidas Poulopoulos | error_text = _('Invalid network address/hostname format')
|
106 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError(error_text)
|
107 | f1149817 | Leonidas Poulopoulos | else:
|
108 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError(_('This field is required.')) |
109 | f1149817 | Leonidas Poulopoulos | return self.cleaned_data["host"] |
110 | f1149817 | Leonidas Poulopoulos | |
111 | edac002f | Leonidas Poulopoulos | |
112 | c2b3014a | Leonidas Poulopoulos | class ContactForm(forms.ModelForm): |
113 | c2b3014a | Leonidas Poulopoulos | |
114 | c2b3014a | Leonidas Poulopoulos | class Meta: |
115 | c2b3014a | Leonidas Poulopoulos | model = Contact |
116 | c2b3014a | Leonidas Poulopoulos | |
117 | 04df82f7 | Leonidas Poulopoulos | class InstRealmForm(forms.ModelForm): |
118 | 04df82f7 | Leonidas Poulopoulos | |
119 | 04df82f7 | Leonidas Poulopoulos | class Meta: |
120 | 04df82f7 | Leonidas Poulopoulos | model = InstRealm |
121 | 04df82f7 | Leonidas Poulopoulos | |
122 | f60df981 | Leonidas Poulopoulos | class ServiceLocForm(forms.ModelForm): |
123 | f60df981 | Leonidas Poulopoulos | |
124 | f60df981 | Leonidas Poulopoulos | class Meta: |
125 | 02f2ae43 | Leonidas Poulopoulos | model = ServiceLoc |
126 | 02f2ae43 | Leonidas Poulopoulos | |
127 | af9d484b | Leonidas Poulopoulos | class ContactForm(forms.ModelForm): |
128 | af9d484b | Leonidas Poulopoulos | |
129 | af9d484b | Leonidas Poulopoulos | class Meta: |
130 | af9d484b | Leonidas Poulopoulos | model = Contact |
131 | 02f2ae43 | Leonidas Poulopoulos | |
132 | 02f2ae43 | Leonidas Poulopoulos | class NameFormSetFact(BaseGenericInlineFormSet): |
133 | 02f2ae43 | Leonidas Poulopoulos | def clean(self): |
134 | af9d484b | Leonidas Poulopoulos | if any(self.errors): |
135 | af9d484b | Leonidas Poulopoulos | return
|
136 | af9d484b | Leonidas Poulopoulos | langs = [] |
137 | af9d484b | Leonidas Poulopoulos | emptyForms = True
|
138 | af9d484b | Leonidas Poulopoulos | for i in range(0, self.total_form_count()): |
139 | af9d484b | Leonidas Poulopoulos | form = self.forms[i]
|
140 | af9d484b | Leonidas Poulopoulos | if len(form.cleaned_data) != 0: |
141 | af9d484b | Leonidas Poulopoulos | emptyForms = False
|
142 | af9d484b | Leonidas Poulopoulos | langs.append(form.cleaned_data.get('lang', None)) |
143 | af9d484b | Leonidas Poulopoulos | if emptyForms:
|
144 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError, _("Fill in at least one location name in English") |
145 | af9d484b | Leonidas Poulopoulos | if "en" not in langs: |
146 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError, _("Fill in at least one location name in English") |
147 | af9d484b | Leonidas Poulopoulos | |
148 | af9d484b | Leonidas Poulopoulos | |
149 | af9d484b | Leonidas Poulopoulos | class UrlFormSetFact(BaseGenericInlineFormSet): |
150 | af9d484b | Leonidas Poulopoulos | def clean(self): |
151 | af9d484b | Leonidas Poulopoulos | if any(self.errors): |
152 | af9d484b | Leonidas Poulopoulos | return
|
153 | af9d484b | Leonidas Poulopoulos | for i in range(0, self.total_form_count()): |
154 | af9d484b | Leonidas Poulopoulos | form = self.forms[i]
|
155 | af9d484b | Leonidas Poulopoulos | if len(form.cleaned_data) == 0: |
156 | af9d484b | Leonidas Poulopoulos | pass
|
157 | af9d484b | Leonidas Poulopoulos | return
|
158 | af9d484b | Leonidas Poulopoulos | |
159 | b9e65644 | Leonidas Poulopoulos | class UrlFormSetFactInst(BaseGenericInlineFormSet): |
160 | b9e65644 | Leonidas Poulopoulos | def clean(self): |
161 | b9e65644 | Leonidas Poulopoulos | if any(self.errors): |
162 | b9e65644 | Leonidas Poulopoulos | return
|
163 | b9e65644 | Leonidas Poulopoulos | url_types = [] |
164 | b9e65644 | Leonidas Poulopoulos | emptyForms = True
|
165 | b9e65644 | Leonidas Poulopoulos | for i in range(0, self.total_form_count()): |
166 | b9e65644 | Leonidas Poulopoulos | form = self.forms[i]
|
167 | b9e65644 | Leonidas Poulopoulos | if len(form.cleaned_data) != 0: |
168 | b9e65644 | Leonidas Poulopoulos | emptyForms = False
|
169 | b9e65644 | Leonidas Poulopoulos | url_types.append(form.cleaned_data.get('urltype',None)) |
170 | b9e65644 | Leonidas Poulopoulos | if emptyForms:
|
171 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError, _("Fill in at least the info url") |
172 | b9e65644 | Leonidas Poulopoulos | if "info" not in url_types: |
173 | f1149817 | Leonidas Poulopoulos | raise forms.ValidationError, _("Fill in at least the info url") |