Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
itminedu
djnro
Commits
f1149817
Commit
f1149817
authored
Oct 23, 2012
by
Leonidas Poulopoulos
Browse files
Added explicit form cleaning to form fields according to Inst ertype
parent
731a947b
Changes
1
Hide whitespace changes
Inline
Side-by-side
edumanage/forms.py
View file @
f1149817
...
...
@@ -6,13 +6,36 @@ from django.conf import settings
from
django.contrib.contenttypes.generic
import
BaseGenericInlineFormSet
import
ipaddr
import
pprint
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}$)'
class
InstDetailsForm
(
forms
.
ModelForm
):
class
Meta
:
model
=
InstitutionDetails
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.'
)
class
InstServerForm
(
forms
.
ModelForm
):
...
...
@@ -22,7 +45,7 @@ class InstServerForm(forms.ModelForm):
def
clean_ertype
(
self
):
ertype
=
self
.
cleaned_data
[
'ertype'
]
institution
=
self
.
cleaned_data
[
'instid'
]
inst_type
=
institution
.
institutiondetails
.
ertype
inst_type
=
institution
.
ertype
type_list
=
[
inst_type
]
if
inst_type
==
3
:
type_list
=
[
1
,
2
,
3
]
...
...
@@ -32,6 +55,59 @@ class InstServerForm(forms.ModelForm):
return
self
.
cleaned_data
[
"ertype"
]
else
:
raise
forms
.
ValidationError
(
'This field is required.'
)
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"
]
class
ContactForm
(
forms
.
ModelForm
):
...
...
@@ -65,9 +141,9 @@ class NameFormSetFact(BaseGenericInlineFormSet):
emptyForms
=
False
langs
.
append
(
form
.
cleaned_data
.
get
(
'lang'
,
None
))
if
emptyForms
:
raise
forms
.
ValidationError
,
"Fill in at least one location name in English"
raise
forms
.
ValidationError
,
_
(
"Fill in at least one location name in English"
)
if
"en"
not
in
langs
:
raise
forms
.
ValidationError
,
"Fill in at least one location name in English"
raise
forms
.
ValidationError
,
_
(
"Fill in at least one location name in English"
)
class
UrlFormSetFact
(
BaseGenericInlineFormSet
):
...
...
@@ -92,6 +168,6 @@ class UrlFormSetFactInst(BaseGenericInlineFormSet):
emptyForms
=
False
url_types
.
append
(
form
.
cleaned_data
.
get
(
'urltype'
,
None
))
if
emptyForms
:
raise
forms
.
ValidationError
,
"Fill in at least the info url"
raise
forms
.
ValidationError
,
_
(
"Fill in at least the info url"
)
if
"info"
not
in
url_types
:
raise
forms
.
ValidationError
,
"Fill in at least the info url"
raise
forms
.
ValidationError
,
_
(
"Fill in at least the info url"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment