forms.py 6.25 KB
Newer Older
1 2
from django import forms
from django.utils.translation import ugettext as _
3 4 5 6 7 8 9 10 11 12
from edumanage.models import (
    MonLocalAuthnParam,
    InstRealmMon,
    InstitutionDetails,
    InstServer,
    Contact,
    InstRealm,
    ServiceLoc
)
from accounts.models import UserProfile
13
from edumanage.fields import MultipleEmailsField
14
from django.contrib.contenttypes.generic import BaseGenericInlineFormSet
15

16 17 18 19
import ipaddr
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])$)'
20 21
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])$)'

22

23
class MonLocalAuthnParamForm(forms.ModelForm):
24

25 26 27
    class Meta:
        model = MonLocalAuthnParam

28

29
class InstRealmMonForm(forms.ModelForm):
30

31 32 33
    class Meta:
        model = InstRealmMon

34

35
class UserProfileForm(forms.ModelForm):
36

37
    email = MultipleEmailsField(required=True)
38

39 40 41
    class Meta:
        model = UserProfile

42

43 44 45
class InstDetailsForm(forms.ModelForm):

    class Meta:
46
        model = InstitutionDetails
47

48 49 50
    def clean_oper_name(self):
        oper_name = self.cleaned_data['oper_name']
        institution = self.cleaned_data['institution']
51
        if institution.ertype in [2, 3]:
52
            if oper_name:
53
                match = re.match(FQDN_RE, oper_name)
54 55
                if not match:
                    raise forms.ValidationError('Invalid domain name format.')
56 57 58
                return self.cleaned_data["oper_name"]
            else:
                raise forms.ValidationError('This field is required.')
59

60

61 62 63 64
class InstServerForm(forms.ModelForm):

    class Meta:
        model = InstServer
65

66 67 68
    def clean_ertype(self):
        ertype = self.cleaned_data['ertype']
        institution = self.cleaned_data['instid']
69
        inst_type = institution.ertype
70 71 72 73 74 75 76 77 78
        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.')
79

80 81
    def clean_auth_port(self):
        auth_port = self.cleaned_data['auth_port']
82 83
        institution = self.cleaned_data['instid']
        if institution.ertype in [1,3]:
84 85
            if auth_port:
                return self.cleaned_data["auth_port"]
86 87
            else:
                raise forms.ValidationError(_('This field is required.'))
88

89 90 91 92 93 94 95 96
    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.'))
97

98 99 100 101 102 103 104 105
    def clean_rad_pkt_type(self):
        rad_pkt_type = self.cleaned_data['rad_pkt_type']
        institution = self.cleaned_data['instid']
        if institution.ertype in [1,3]:
            if rad_pkt_type:
                return self.cleaned_data["rad_pkt_type"]
            else:
                raise forms.ValidationError(_('This field is required.'))
106

107 108
    def clean_host(self):
        host = self.cleaned_data['host']
109
        addr_type = self.cleaned_data['addr_type']
110 111 112 113
        if host:
            match = re.match(FQDN_RE, host)
            if not match:
                try:
114 115 116 117 118 119
                    if addr_type == 'any':
                        address = ipaddr.IPAddress(host)
                    if addr_type == 'ipv4':
                        address = ipaddr.IPv4Address(host)
                    if addr_type == 'ipv6':
                        address = ipaddr.IPv6Address(host)
120 121 122 123 124 125
                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"]
126

127

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

    class Meta:
        model = Contact

133

134 135 136 137
class InstRealmForm(forms.ModelForm):

    class Meta:
        model = InstRealm
138

139 140 141 142 143 144 145 146 147 148
    def clean_proxyto(self):
        proxied_servers = self.cleaned_data['proxyto']
        if proxied_servers:
            for server in proxied_servers:
                if server.ertype not in [1,3]:
                    error_text = _('Only IdP and IdP/SP server types are allowed')
                    raise forms.ValidationError(error_text)
        else:
            raise forms.ValidationError(_('This field is required.'))
        return self.cleaned_data["proxyto"]
149

150

151 152 153
class ServiceLocForm(forms.ModelForm):

    class Meta:
154 155 156 157 158
        model = ServiceLoc


class NameFormSetFact(BaseGenericInlineFormSet):
    def clean(self):
159 160 161
        if any(self.errors):
            return
        langs = []
162
        empty_forms = True
163 164 165
        for i in range(0, self.total_form_count()):
            form = self.forms[i]
            if len(form.cleaned_data) != 0:
166
                empty_forms = False
167
            langs.append(form.cleaned_data.get('lang', None))
168
        if empty_forms:
169
            raise forms.ValidationError, _("Fill in at least one location name in English")
170
        if "en" not in langs:
171
            raise forms.ValidationError, _("Fill in at least one location name in English")
172 173 174 175 176 177 178 179 180 181 182


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
183 184


185 186 187 188 189
class UrlFormSetFactInst(BaseGenericInlineFormSet):
    def clean(self):
        if any(self.errors):
            return
        url_types = []
190
        empty_forms = True
191 192 193
        for i in range(0, self.total_form_count()):
            form = self.forms[i]
            if len(form.cleaned_data) != 0:
194 195 196
                empty_forms = False
            url_types.append(form.cleaned_data.get('urltype', None))
        if empty_forms:
197
            raise forms.ValidationError, _("Fill in at least the info url")
198
        if "info" not in url_types:
199
            raise forms.ValidationError, _("Fill in at least the info url")