forms.py 5.78 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
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])$)'
16 17
DN_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])$)'

18

19 20 21 22 23 24 25 26 27 28
class MonLocalAuthnParamForm(forms.ModelForm):
    
    class Meta:
        model = MonLocalAuthnParam

class InstRealmMonForm(forms.ModelForm):
    
    class Meta:
        model = InstRealmMon

29 30 31 32 33
class UserProfileForm(forms.ModelForm):

    class Meta:
        model = UserProfile

34 35 36
class InstDetailsForm(forms.ModelForm):

    class Meta:
37 38 39 40 41 42 43
        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:
44 45 46
                match = re.match(DN_RE, oper_name)
                if not match:
                    raise forms.ValidationError('Invalid domain name format.')
47 48 49
                return self.cleaned_data["oper_name"]
            else:
                raise forms.ValidationError('This field is required.')
50 51 52 53 54

class InstServerForm(forms.ModelForm):

    class Meta:
        model = InstServer
55 56 57 58
    
    def clean_ertype(self):
        ertype = self.cleaned_data['ertype']
        institution = self.cleaned_data['instid']
59
        inst_type = institution.ertype
60 61 62 63 64 65 66 67 68
        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.')
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 117 118 119 120 121
    
    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"]
    
122

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

    class Meta:
        model = Contact

128 129 130 131 132
class InstRealmForm(forms.ModelForm):

    class Meta:
        model = InstRealm

133 134 135
class ServiceLocForm(forms.ModelForm):

    class Meta:
136 137
        model = ServiceLoc

138 139 140 141
class ContactForm(forms.ModelForm):

    class Meta:
        model = Contact
142 143 144

class NameFormSetFact(BaseGenericInlineFormSet):
    def clean(self):
145 146 147 148 149 150 151 152 153 154
        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:        
155
            raise forms.ValidationError, _("Fill in at least one location name in English")
156
        if "en" not in langs:
157
            raise forms.ValidationError, _("Fill in at least one location name in English")
158 159 160 161 162 163 164 165 166 167 168 169


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
                
170 171 172 173 174 175 176 177 178 179 180 181
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:        
182
            raise forms.ValidationError, _("Fill in at least the info url")
183
        if "info" not in url_types:
184
            raise forms.ValidationError, _("Fill in at least the info url")
185 186