Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Julien Jerphanion
Rex Dri
Commits
e7310329
Commit
e7310329
authored
Aug 19, 2018
by
Florent Chehab
Browse files
Began work on models, Basic module working with usefull_links validation.
parent
ef93ef29
Changes
7
Hide whitespace changes
Inline
Side-by-side
rex/admin.py
View file @
e7310329
...
...
@@ -4,9 +4,11 @@ from reversion_compare.admin import CompareVersionAdmin
from
rex.models.university
import
University
,
MainCampus
from
rex.models.location
import
Country
,
City
from
rex.models.module
import
BasicModule
admin
.
site
.
register
(
Country
)
admin
.
site
.
register
(
City
)
admin
.
site
.
register
(
BasicModule
)
admin
.
site
.
register
(
University
,
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
:4
4
# Generated by Django 2.0.3 on 2018-08-1
9 11
:4
9
import
django.core.validators
from
django.db
import
migrations
,
models
import
django.db.models.deletion
import
rex.models.tools.usefullLinksField
import
rex.utils.friendly_path
...
...
@@ -14,6 +15,14 @@ class Migration(migrations.Migration):
]
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
(
name
=
'City'
,
fields
=
[
...
...
rex/models/module/__init__.py
View file @
e7310329
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
.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
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