24
24
25
25
26
26
class UserProfile (Form ):
27
- def validate_general (field : StringField , infomsg : str , url_prefix : str ):
27
+ def validate_general (value : str , infomsg : str , url_prefix : str ):
28
28
"""Validate basic user inputs, i.e. check for leading/trailing
29
29
whitespaces and leading URL prefix, like http://scholar.google.com/
30
30
31
31
Parameters
32
32
----------
33
- field : wtforms.StringField
34
- The WTform user input field .
33
+ value : str
34
+ The WTform user input string .
35
35
infomsg : str
36
36
An error message to inform the user how to extract the correct
37
37
value.
@@ -48,8 +48,7 @@ def validate_general(field: StringField, infomsg: str, url_prefix: str):
48
48
a) input has leading or trailing whitespaces
49
49
b) input starts with the given url_prefix
50
50
"""
51
- value = field .data
52
- if value == "" :
51
+ if (value is None ) or (value == "" ):
53
52
# nothing to complain, as input is empty
54
53
return None
55
54
@@ -58,11 +57,12 @@ def validate_general(field: StringField, infomsg: str, url_prefix: str):
58
57
'Please remove all leading and trailing whitespaces from your '
59
58
'input.<br/>%s' % infomsg )
60
59
61
- isPrefix = re .search ("^%s" % url_prefix , value )
62
- if isPrefix is not None :
63
- raise ValidationError (
64
- 'Please remove the "%s" part from your input.<br/>%s' % (
65
- isPrefix [0 ], infomsg ))
60
+ if len (url_prefix ) > 0 :
61
+ isPrefix = re .search ("^%s" % url_prefix , value )
62
+ if isPrefix is not None :
63
+ raise ValidationError (
64
+ 'Please remove the "%s" part from your input.<br/>%s' % (
65
+ isPrefix [0 ], infomsg ))
66
66
67
67
# if there is still no error raised, we return the actual value of the
68
68
# user input
@@ -90,7 +90,7 @@ def validator_orcid_id(form: Form, field: StringField):
90
90
' every four digits are separated with a dash "-". An '
91
91
'example is: 0000-0002-0975-9019' )
92
92
value = UserProfile .validate_general (
93
- field , infomsg , 'https://orcid.org' )
93
+ field . data , infomsg , 'https://orcid.org' )
94
94
if value is None :
95
95
return True
96
96
@@ -129,7 +129,7 @@ def validator_gscholar_id(form, field):
129
129
# we need a regex here, since we don't know the TLD the user is
130
130
# presenting to us
131
131
value = UserProfile .validate_general (
132
- field , infomsg , r'https://scholar.google.\w{1,3}/citations\?' )
132
+ field . data , infomsg , r'https://scholar.google.\w{1,3}/citations\?' )
133
133
if value is None :
134
134
return True
135
135
@@ -171,7 +171,7 @@ def validator_rgate_id(form, field):
171
171
'Your ID is the part right of the last "/", in the example:'
172
172
' "Rob-Knight"' )
173
173
value = UserProfile .validate_general (
174
- field , infomsg , 'https://www.researchgate.net/profile/' )
174
+ field . data , infomsg , 'https://www.researchgate.net/profile/' )
175
175
if value is None :
176
176
return True
177
177
0 commit comments