Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Debian New Member Process
nm.debian.org
Commits
9e2822cc
Commit
9e2822cc
authored
Aug 29, 2015
by
Enrico Zini
Browse files
Share FingerprintField.clean_fingerprint method
parent
153743b4
Changes
2
Hide whitespace changes
Inline
Side-by-side
backend/models.py
View file @
9e2822cc
...
...
@@ -84,15 +84,16 @@ class FingerprintField(models.CharField):
re_spaces
=
re
.
compile
(
r
"\s+"
)
re_invalid
=
re
.
compile
(
r
"[^0-9A-Fa-f]+"
)
def
_clean_fingerprint
(
self
,
value
):
@
classmethod
def
clean_fingerprint
(
cls
,
value
):
# Refuse all non-strings
if
not
isinstance
(
value
,
basestring
):
return
None
# Remove spaces
value
=
self
.
re_spaces
.
sub
(
""
,
value
)
value
=
cls
.
re_spaces
.
sub
(
""
,
value
)
# Convert empty strings to None
if
not
value
:
return
None
# Refuse strings with non-hex characters
if
self
.
re_invalid
.
search
(
value
):
return
None
if
cls
.
re_invalid
.
search
(
value
):
return
None
# Refuse hex strings whose length does not match a fingerprint
if
len
(
value
)
!=
32
and
len
(
value
)
!=
40
:
return
None
# Uppercase the result
...
...
@@ -104,14 +105,14 @@ class FingerprintField(models.CharField):
Converts a value from the database to a python object
"""
value
=
super
(
FingerprintField
,
self
).
to_python
(
value
)
return
self
.
_
clean_fingerprint
(
value
)
return
self
.
clean_fingerprint
(
value
)
def
get_prep_value
(
self
,
value
):
"""
Converts a value from python to the DB
"""
value
=
super
(
FingerprintField
,
self
).
get_prep_value
(
value
)
return
self
.
_
clean_fingerprint
(
value
)
return
self
.
clean_fingerprint
(
value
)
def
formfield
(
self
,
**
kwargs
):
# bypass our parent to fix "maxlength" attribute in widget: fingerprint
...
...
public/views.py
View file @
9e2822cc
...
...
@@ -798,10 +798,7 @@ class NewPersonForm(forms.ModelForm):
self
.
fields
[
"fpr"
].
required
=
True
def
clean_fpr
(
self
):
fpr
=
self
.
cleaned_data
[
'fpr'
]
if
fpr
is
not
None
:
return
fpr
.
replace
(
' '
,
''
)
return
fpr
return
bmodels
.
FingerprintField
.
clean_fingerprint
(
self
.
cleaned_data
[
'fpr'
])
def
clean_sc_ok
(
self
):
data
=
self
.
cleaned_data
[
'sc_ok'
]
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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