Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
csentry
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Harrisson
csentry
Commits
f57d9789
Commit
f57d9789
authored
7 years ago
by
Benjamin Bertrand
Browse files
Options
Downloads
Patches
Plain Diff
Create generic RegexpList field validator
Used to validate a list of strings against a regular expresssion.
parent
ba27ff55
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/inventory/forms.py
+3
-11
3 additions, 11 deletions
app/inventory/forms.py
app/validators.py
+21
-0
21 additions, 0 deletions
app/validators.py
with
24 additions
and
11 deletions
app/inventory/forms.py
+
3
−
11
View file @
f57d9789
...
...
@@ -9,20 +9,11 @@ This module defines the inventory blueprint forms.
:license: BSD 2-Clause, see LICENSE for more details.
"""
import
re
from
wtforms
import
SelectField
,
StringField
,
TextAreaField
,
validators
from
..helpers
import
CSEntryForm
from
..validators
import
Unique
from
..validators
import
Unique
,
RegexpList
,
MAC_ADDRESS_RE
from
..
import
utils
,
models
MAC_ADDRESS_RE
=
re
.
compile
(
'
^(?:[0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$
'
)
def
check_mac_addresses_list
(
form
,
field
):
for
address
in
field
.
data
.
split
():
if
MAC_ADDRESS_RE
.
match
(
address
)
is
None
:
raise
validators
.
ValidationError
(
'
Invalid Mac address
'
)
class
AttributeForm
(
CSEntryForm
):
name
=
StringField
(
'
name
'
,
validators
=
[
validators
.
DataRequired
()])
...
...
@@ -43,7 +34,8 @@ class ItemForm(CSEntryForm):
mac_addresses
=
StringField
(
'
MAC addresses
'
,
description
=
'
space separated list of MAC addresses
'
,
validators
=
[
validators
.
Optional
(),
check_mac_addresses_list
])
validators
=
[
validators
.
Optional
(),
RegexpList
(
MAC_ADDRESS_RE
,
message
=
'
Invalid MAC address
'
)])
def
__init__
(
self
,
*
args
,
**
kwargs
):
super
().
__init__
(
*
args
,
**
kwargs
)
...
...
This diff is collapsed.
Click to expand it.
app/validators.py
+
21
−
0
View file @
f57d9789
...
...
@@ -10,9 +10,12 @@ This module defines extra field validators
"""
import
ipaddress
import
re
import
sqlalchemy
as
sa
from
wtforms
import
ValidationError
MAC_ADDRESS_RE
=
re
.
compile
(
'
^(?:[0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}$
'
)
class
IPNetwork
:
"""
Validates an IP network.
...
...
@@ -59,3 +62,21 @@ class Unique(object):
raise
ValidationError
(
self
.
message
)
except
sa
.
orm
.
exc
.
NoResultFound
:
pass
class
RegexpList
:
"""
Validates a list of strings against a user provided regexp.
:param regex: the regular expression to use
:param message: the error message
"""
def
__init__
(
self
,
regex
,
message
=
None
):
self
.
regex
=
regex
if
message
is
None
:
message
=
'
Invalid input.
'
self
.
message
=
message
def
__call__
(
self
,
form
,
field
):
for
string
in
field
.
data
.
split
():
if
self
.
regex
.
match
(
string
)
is
None
:
raise
ValidationError
(
self
.
message
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
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!
Save comment
Cancel
Please
register
or
sign in
to comment