forms.py 5.73 KB
Newer Older
1
2
3
4
from django import forms
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
from edumanage.models import *
5
from accounts.models import *
6
7
from django.conf import settings

8
from django.contrib.contenttypes.generic import BaseGenericInlineFormSet
9

10
import ipaddr
11

12
import pprint
13
14
15
16
import re

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])$)'
#FQDN_RE = r'(^[a-z0-9.-]{1,255}$)'
17

18
19
20
21
22
class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile

23
24
25
class InstDetailsForm(forms.ModelForm):

    class Meta:
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
        model = InstitutionDetails
    
    def clean_oper_name(self):
        oper_name = self.cleaned_data['oper_name']
        institution = self.cleaned_data['institution']
        if institution.ertype in [2,3]:
            if oper_name:
                return self.cleaned_data["oper_name"]
            else:
                raise forms.ValidationError('This field is required.')
            
    def clean_number_user(self):
        number_user = self.cleaned_data['number_user']
        institution = self.cleaned_data['institution']
        if institution.ertype in [1,3]:
            if number_user:
                return self.cleaned_data["number_user"]
            else:
                raise forms.ValidationError('This field is required.')            
45
46
47
48
49

class InstServerForm(forms.ModelForm):

    class Meta:
        model = InstServer
50
51
52
53
    
    def clean_ertype(self):
        ertype = self.cleaned_data['ertype']
        institution = self.cleaned_data['instid']
54
        inst_type = institution.ertype
55
56
57
58
59
60
61
62
63
        type_list = [inst_type]
        if inst_type == 3:
            type_list = [1, 2, 3]
        if ertype:
            if ertype not in type_list:
                raise forms.ValidationError('Server type cannot be different than institution type (%s)' %dict(self.fields['ertype'].choices)[inst_type])
            return self.cleaned_data["ertype"]
        else:
            raise forms.ValidationError('This field is required.')
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
    
    def clean_port(self):
        port = self.cleaned_data['port']
        institution = self.cleaned_data['instid']
        if institution.ertype in [1,3]:
            if port:
                return self.cleaned_data["port"]
            else:
                raise forms.ValidationError(_('This field is required.'))
    
    def clean_acct_port(self):
        acct_port = self.cleaned_data['acct_port']
        institution = self.cleaned_data['instid']
        if institution.ertype in [1,3]:
            if acct_port:
                return self.cleaned_data["acct_port"]
            else:
                raise forms.ValidationError(_('This field is required.'))

    def clean_timeout(self):
        timeout = self.cleaned_data['timeout']
        institution = self.cleaned_data['instid']
        if institution.ertype in [1,3]:
            if timeout:
                return self.cleaned_data["timeout"]
            else:
                raise forms.ValidationError(_('This field is required.'))

    def clean_retry(self):
        retry = self.cleaned_data['retry']
        institution = self.cleaned_data['instid']
        if institution.ertype in [1,3]:
            if retry:
                return self.cleaned_data["retry"]
            else:
                raise forms.ValidationError(_('This field is required.'))

    
    def clean_host(self):
        host = self.cleaned_data['host']
        if host:
            match = re.match(FQDN_RE, host)
            if not match:
                print "not match"
                try:
                    address = ipaddr.IPNetwork(host)
                except Exception:
                        error_text = _('Invalid network address/hostname format')
                        raise forms.ValidationError(error_text)
        else:
            raise forms.ValidationError(_('This field is required.'))
        return self.cleaned_data["host"]
    
117

118
119
120
121
122
class ContactForm(forms.ModelForm):

    class Meta:
        model = Contact

123
124
125
126
127
class InstRealmForm(forms.ModelForm):

    class Meta:
        model = InstRealm

128
129
130
class ServiceLocForm(forms.ModelForm):

    class Meta:
131
132
        model = ServiceLoc

133
134
135
136
class ContactForm(forms.ModelForm):

    class Meta:
        model = Contact
137
138
139

class NameFormSetFact(BaseGenericInlineFormSet):
    def clean(self):
140
141
142
143
144
145
146
147
148
149
        if any(self.errors):
            return
        langs = []
        emptyForms = True
        for i in range(0, self.total_form_count()):
            form = self.forms[i]
            if len(form.cleaned_data) != 0:
                emptyForms = False
            langs.append(form.cleaned_data.get('lang', None))
        if emptyForms:        
150
            raise forms.ValidationError, _("Fill in at least one location name in English")
151
        if "en" not in langs:
152
            raise forms.ValidationError, _("Fill in at least one location name in English")
153
154
155
156
157
158
159
160
161
162
163
164


class UrlFormSetFact(BaseGenericInlineFormSet):
    def clean(self):
        if any(self.errors):
            return
        for i in range(0, self.total_form_count()):
            form = self.forms[i]
            if len(form.cleaned_data) == 0:
                pass
        return
                
165
166
167
168
169
170
171
172
173
174
175
176
class UrlFormSetFactInst(BaseGenericInlineFormSet):
    def clean(self):
        if any(self.errors):
            return
        url_types = []
        emptyForms = True
        for i in range(0, self.total_form_count()):
            form = self.forms[i]
            if len(form.cleaned_data) != 0:
                emptyForms = False
            url_types.append(form.cleaned_data.get('urltype',None))
        if emptyForms:        
177
            raise forms.ValidationError, _("Fill in at least the info url")
178
        if "info" not in url_types:
179
            raise forms.ValidationError, _("Fill in at least the info url")