Statistics
| Branch: | Tag: | Revision:

root / edumanage / forms.py @ f1149817

History | View | Annotate | Download (5.6 kB)

1
from django import forms
2
from django.utils.translation import ugettext as _
3
from django.utils.translation import ugettext_lazy
4
from edumanage.models import *
5
from django.conf import settings
6

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

    
9
import ipaddr
10

    
11
import pprint
12
import re
13

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

    
17
class InstDetailsForm(forms.ModelForm):
18

    
19
    class Meta:
20
        model = InstitutionDetails
21
    
22
    def clean_oper_name(self):
23
        oper_name = self.cleaned_data['oper_name']
24
        institution = self.cleaned_data['institution']
25
        if institution.ertype in [2,3]:
26
            if oper_name:
27
                return self.cleaned_data["oper_name"]
28
            else:
29
                raise forms.ValidationError('This field is required.')
30
            
31
    def clean_number_user(self):
32
        number_user = self.cleaned_data['number_user']
33
        institution = self.cleaned_data['institution']
34
        if institution.ertype in [1,3]:
35
            if number_user:
36
                return self.cleaned_data["number_user"]
37
            else:
38
                raise forms.ValidationError('This field is required.')            
39

    
40
class InstServerForm(forms.ModelForm):
41

    
42
    class Meta:
43
        model = InstServer
44
    
45
    def clean_ertype(self):
46
        ertype = self.cleaned_data['ertype']
47
        institution = self.cleaned_data['instid']
48
        inst_type = institution.ertype
49
        type_list = [inst_type]
50
        if inst_type == 3:
51
            type_list = [1, 2, 3]
52
        if ertype:
53
            if ertype not in type_list:
54
                raise forms.ValidationError('Server type cannot be different than institution type (%s)' %dict(self.fields['ertype'].choices)[inst_type])
55
            return self.cleaned_data["ertype"]
56
        else:
57
            raise forms.ValidationError('This field is required.')
58
    
59
    def clean_port(self):
60
        port = self.cleaned_data['port']
61
        institution = self.cleaned_data['instid']
62
        if institution.ertype in [1,3]:
63
            if port:
64
                return self.cleaned_data["port"]
65
            else:
66
                raise forms.ValidationError(_('This field is required.'))
67
    
68
    def clean_acct_port(self):
69
        acct_port = self.cleaned_data['acct_port']
70
        institution = self.cleaned_data['instid']
71
        if institution.ertype in [1,3]:
72
            if acct_port:
73
                return self.cleaned_data["acct_port"]
74
            else:
75
                raise forms.ValidationError(_('This field is required.'))
76

    
77
    def clean_timeout(self):
78
        timeout = self.cleaned_data['timeout']
79
        institution = self.cleaned_data['instid']
80
        if institution.ertype in [1,3]:
81
            if timeout:
82
                return self.cleaned_data["timeout"]
83
            else:
84
                raise forms.ValidationError(_('This field is required.'))
85

    
86
    def clean_retry(self):
87
        retry = self.cleaned_data['retry']
88
        institution = self.cleaned_data['instid']
89
        if institution.ertype in [1,3]:
90
            if retry:
91
                return self.cleaned_data["retry"]
92
            else:
93
                raise forms.ValidationError(_('This field is required.'))
94

    
95
    
96
    def clean_host(self):
97
        host = self.cleaned_data['host']
98
        if host:
99
            match = re.match(FQDN_RE, host)
100
            if not match:
101
                print "not match"
102
                try:
103
                    address = ipaddr.IPNetwork(host)
104
                except Exception:
105
                        error_text = _('Invalid network address/hostname format')
106
                        raise forms.ValidationError(error_text)
107
        else:
108
            raise forms.ValidationError(_('This field is required.'))
109
        return self.cleaned_data["host"]
110
    
111

    
112
class ContactForm(forms.ModelForm):
113

    
114
    class Meta:
115
        model = Contact
116

    
117
class InstRealmForm(forms.ModelForm):
118

    
119
    class Meta:
120
        model = InstRealm
121

    
122
class ServiceLocForm(forms.ModelForm):
123

    
124
    class Meta:
125
        model = ServiceLoc
126

    
127
class ContactForm(forms.ModelForm):
128

    
129
    class Meta:
130
        model = Contact
131

    
132
class NameFormSetFact(BaseGenericInlineFormSet):
133
    def clean(self):
134
        if any(self.errors):
135
            return
136
        langs = []
137
        emptyForms = True
138
        for i in range(0, self.total_form_count()):
139
            form = self.forms[i]
140
            if len(form.cleaned_data) != 0:
141
                emptyForms = False
142
            langs.append(form.cleaned_data.get('lang', None))
143
        if emptyForms:        
144
            raise forms.ValidationError, _("Fill in at least one location name in English")
145
        if "en" not in langs:
146
            raise forms.ValidationError, _("Fill in at least one location name in English")
147

    
148

    
149
class UrlFormSetFact(BaseGenericInlineFormSet):
150
    def clean(self):
151
        if any(self.errors):
152
            return
153
        for i in range(0, self.total_form_count()):
154
            form = self.forms[i]
155
            if len(form.cleaned_data) == 0:
156
                pass
157
        return
158
                
159
class UrlFormSetFactInst(BaseGenericInlineFormSet):
160
    def clean(self):
161
        if any(self.errors):
162
            return
163
        url_types = []
164
        emptyForms = True
165
        for i in range(0, self.total_form_count()):
166
            form = self.forms[i]
167
            if len(form.cleaned_data) != 0:
168
                emptyForms = False
169
            url_types.append(form.cleaned_data.get('urltype',None))
170
        if emptyForms:        
171
            raise forms.ValidationError, _("Fill in at least the info url")
172
        if "info" not in url_types:
173
            raise forms.ValidationError, _("Fill in at least the info url")