Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
django-cas-server
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Valentin Samir
django-cas-server
Commits
14a459b1
Commit
14a459b1
authored
Aug 21, 2016
by
Valentin Samir
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a validator to models CharField that should be regular expressions
parent
282e3a83
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
31 additions
and
3 deletions
+31
-3
cas_server/models.py
cas_server/models.py
+6
-3
cas_server/tests/test_utils.py
cas_server/tests/test_utils.py
+6
-0
cas_server/utils.py
cas_server/utils.py
+19
-0
No files found.
cas_server/models.py
View file @
14a459b1
...
...
@@ -466,7 +466,8 @@ class ServicePattern(models.Model):
"A regular expression matching services. "
"Will usually looks like '^https://some
\\
.server
\\
.com/path/.*$'."
"As it is a regular expression, special character must be escaped with a '
\\
'."
)
),
validators
=
[
utils
.
regexpr_validator
]
)
#: Name of the attribute to transmit as username, if empty the user login is used
user_field
=
models
.
CharField
(
...
...
@@ -660,7 +661,8 @@ class FilterAttributValue(models.Model):
pattern
=
models
.
CharField
(
max_length
=
255
,
verbose_name
=
_
(
u
"pattern"
),
help_text
=
_
(
u
"a regular expression"
)
help_text
=
_
(
u
"a regular expression"
),
validators
=
[
utils
.
regexpr_validator
]
)
#: ForeignKey to a :class:`ServicePattern`. :class:`FilterAttributValue` instances for a
#: :class:`ServicePattern` are accessible thought its :attr:`ServicePattern.filters`
...
...
@@ -689,7 +691,8 @@ class ReplaceAttributValue(models.Model):
pattern
=
models
.
CharField
(
max_length
=
255
,
verbose_name
=
_
(
u
"pattern"
),
help_text
=
_
(
u
"An regular expression maching whats need to be replaced"
)
help_text
=
_
(
u
"An regular expression maching whats need to be replaced"
),
validators
=
[
utils
.
regexpr_validator
]
)
#: The replacement to what is mached by :attr:`pattern`. groups are capture by \\1, \\2 …
replace
=
models
.
CharField
(
...
...
cas_server/tests/test_utils.py
View file @
14a459b1
...
...
@@ -255,3 +255,9 @@ class UtilsTestCase(TestCase):
self
.
assertIsInstance
(
result
,
dict
)
self
.
assertIn
(
'applied'
,
result
)
self
.
assertIsInstance
(
result
[
'applied'
],
datetime
.
datetime
)
def
test_regexpr_validator
(
self
):
"""test the function regexpr_validator"""
utils
.
regexpr_validator
(
"^a$"
)
with
self
.
assertRaises
(
utils
.
ValidationError
):
utils
.
regexpr_validator
(
"["
)
cas_server/utils.py
View file @
14a459b1
...
...
@@ -18,7 +18,10 @@ from django.contrib import messages
from
django.contrib.messages
import
constants
as
DEFAULT_MESSAGE_LEVELS
from
django.core.serializers.json
import
DjangoJSONEncoder
from
django.utils
import
timezone
from
django.core.exceptions
import
ValidationError
from
django.utils.translation
import
ugettext_lazy
as
_
import
re
import
random
import
string
import
json
...
...
@@ -700,3 +703,19 @@ def logout_request(ticket):
'datetime'
:
timezone
.
now
().
isoformat
(),
'ticket'
:
ticket
}
def
regexpr_validator
(
value
):
"""
Test that ``value`` is a valid regular expression
:param unicode value: A regular expression to test
:raises ValidationError: if ``value`` is not a valid regular expression
"""
try
:
re
.
compile
(
value
)
except
re
.
error
:
raise
ValidationError
(
_
(
'"%(value)s" is not a valid regular expression'
),
params
=
{
'value'
:
value
}
)
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