Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Rex Dri
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
32
Issues
32
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Rex Dri
Rex Dri
Commits
e7310329
Commit
e7310329
authored
Aug 19, 2018
by
Florent Chehab
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Began work on models, Basic module working with usefull_links validation.
parent
ef93ef29
Pipeline
#26281
passed with stages
in 1 minute and 23 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
81 additions
and
1 deletion
+81
-1
rex/admin.py
rex/admin.py
+2
-0
rex/migrations/0001_initial.py
rex/migrations/0001_initial.py
+10
-1
rex/models/module/__init__.py
rex/models/module/__init__.py
+1
-0
rex/models/module/basicModule.py
rex/models/module/basicModule.py
+11
-0
rex/models/tools/__init__.py
rex/models/tools/__init__.py
+2
-0
rex/models/tools/usefullLinksField.py
rex/models/tools/usefullLinksField.py
+35
-0
rex/models/tools/validateWithRestFramework.py
rex/models/tools/validateWithRestFramework.py
+20
-0
No files found.
rex/admin.py
View file @
e7310329
...
@@ -4,9 +4,11 @@ from reversion_compare.admin import CompareVersionAdmin
...
@@ -4,9 +4,11 @@ from reversion_compare.admin import CompareVersionAdmin
from
rex.models.university
import
University
,
MainCampus
from
rex.models.university
import
University
,
MainCampus
from
rex.models.location
import
Country
,
City
from
rex.models.location
import
Country
,
City
from
rex.models.module
import
BasicModule
admin
.
site
.
register
(
Country
)
admin
.
site
.
register
(
Country
)
admin
.
site
.
register
(
City
)
admin
.
site
.
register
(
City
)
admin
.
site
.
register
(
BasicModule
)
admin
.
site
.
register
(
University
,
CompareVersionAdmin
)
admin
.
site
.
register
(
University
,
CompareVersionAdmin
)
admin
.
site
.
register
(
MainCampus
,
CompareVersionAdmin
)
admin
.
site
.
register
(
MainCampus
,
CompareVersionAdmin
)
rex/migrations/0001_initial.py
View file @
e7310329
# Generated by Django 2.0.3 on 2018-08-1
8 08:44
# Generated by Django 2.0.3 on 2018-08-1
9 11:49
import
django.core.validators
import
django.core.validators
from
django.db
import
migrations
,
models
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
django.db.models.deletion
import
rex.models.tools.usefullLinksField
import
rex.utils.friendly_path
import
rex.utils.friendly_path
...
@@ -14,6 +15,14 @@ class Migration(migrations.Migration):
...
@@ -14,6 +15,14 @@ class Migration(migrations.Migration):
]
]
operations
=
[
operations
=
[
migrations
.
CreateModel
(
name
=
'BasicModule'
,
fields
=
[
(
'id'
,
models
.
AutoField
(
auto_created
=
True
,
primary_key
=
True
,
serialize
=
False
,
verbose_name
=
'ID'
)),
(
'comment'
,
models
.
TextField
()),
(
'usefull_links'
,
rex
.
models
.
tools
.
usefullLinksField
.
UsefullLinksField
(
validators
=
[
rex
.
models
.
tools
.
usefullLinksField
.
validate_usefull_links
])),
],
),
migrations
.
CreateModel
(
migrations
.
CreateModel
(
name
=
'City'
,
name
=
'City'
,
fields
=
[
fields
=
[
...
...
rex/models/module/__init__.py
View file @
e7310329
from
.module
import
Module
# noqa: F401
from
.module
import
Module
# noqa: F401
from
.basicModule
import
BasicModule
# noqa: F401
rex/models/module/basicModule.py
0 → 100644
View file @
e7310329
from
django.db
import
models
from
rex.models.tools
import
UsefullLinksField
class
BasicModule
(
models
.
Model
):
comment
=
models
.
TextField
()
usefull_links
=
UsefullLinksField
()
# class Meta:
# abstract = True
rex/models/tools/__init__.py
View file @
e7310329
from
.DictModeViewSet
import
DictModeViewSet
# noqa: F401
from
.DictModeViewSet
import
DictModeViewSet
# noqa: F401
from
.usefullLinksField
import
UsefullLinksField
# noqa: F401
from
.validateWithRestFramework
import
validate_with_rest_framework
# noqa: F401
rex/models/tools/usefullLinksField.py
0 → 100644
View file @
e7310329
from
django.contrib.postgres.fields
import
JSONField
from
django.core.exceptions
import
ValidationError
from
rest_framework
import
serializers
from
rex.models.tools.validateWithRestFramework
import
validate_with_rest_framework
class
UrlAndDescriptionSerializer
(
serializers
.
Serializer
):
"""
Simple serialize used to validate each usefull links objs
"""
url
=
serializers
.
URLField
(
required
=
True
)
description
=
serializers
.
CharField
(
required
=
False
,
allow_null
=
True
,
allow_blank
=
True
)
def
validate_usefull_links
(
value
):
"""
Function validate the data that should be stored
in a usefull_links field
"""
if
type
(
value
)
is
not
list
:
raise
ValidationError
(
"Usefull links must be a JSON array !"
)
for
obj
in
value
:
validate_with_rest_framework
(
UrlAndDescriptionSerializer
,
obj
)
class
UsefullLinksField
(
JSONField
):
description
=
"A field to store a URL and a description as single JSON data"
def
__init__
(
self
,
*
args
,
**
kwargs
):
kwargs
[
'validators'
]
=
[
validate_usefull_links
]
super
(
UsefullLinksField
,
self
).
__init__
(
*
args
,
**
kwargs
)
rex/models/tools/validateWithRestFramework.py
0 → 100644
View file @
e7310329
from
django.core.exceptions
import
ValidationError
def
validate_with_rest_framework
(
serializer
,
value
):
"""
Function to validate some data (comming from JSON)
against a serializer.
TODO add test for this...
"""
valid_ser
=
serializer
(
data
=
value
)
if
not
valid_ser
.
is_valid
():
raise
ValidationError
(
str
(
valid_ser
.
errors
))
# Also checks that no extra fields were added
if
type
(
value
)
is
not
list
:
allowed_keys
=
list
(
valid_ser
.
get_fields
())
for
key
in
value
.
keys
():
if
key
not
in
allowed_keys
:
raise
ValidationError
(
"Expected JSON schema not respected"
)
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