Skip to content
GitLab
Menu
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
7c38befd
Commit
7c38befd
authored
Sep 01, 2018
by
Florent Chehab
Browse files
Centralized tag definition
parent
4553b543
Changes
8
Hide whitespace changes
Inline
Side-by-side
backend/load_data/assets/tags.json
View file @
7c38befd
...
...
@@ -3,42 +3,7 @@
"name"
:
"photos"
,
"config"
:
{
"photos"
:
{
"type"
:
"array"
,
"content"
:
{
"url"
:
{
"type"
:
"url"
,
"required"
:
true
,
"validators"
:
{
"extension"
:
[
"jpg"
,
"jpeg"
,
"png"
,
"svg"
]
}
},
"title"
:
{
"type"
:
"text"
,
"required"
:
true
,
"validators"
:
{
"max_length"
:
200
}
},
"licence"
:
{
"type"
:
"text"
,
"required"
:
false
,
"validators"
:
{
"max_length"
:
200
}
},
"description"
:
{
"type"
:
"text"
,
"required"
:
false
,
"validators"
:
{
"max_length"
:
500
}
}
},
"type"
:
"photos"
,
"required"
:
true
}
}
...
...
backend/migrations/0002_auto_20180901_1000.py
0 → 100644
View file @
7c38befd
# Generated by Django 2.0.3 on 2018-09-01 08:00
import
django.contrib.postgres.fields.jsonb
from
django.db
import
migrations
class
Migration
(
migrations
.
Migration
):
dependencies
=
[
(
'backend'
,
'0001_initial'
),
]
operations
=
[
migrations
.
AddField
(
model_name
=
'campustaggeditem'
,
name
=
'custom_content'
,
field
=
django
.
contrib
.
postgres
.
fields
.
jsonb
.
JSONField
(
default
=
dict
),
),
migrations
.
AddField
(
model_name
=
'citytaggeditem'
,
name
=
'custom_content'
,
field
=
django
.
contrib
.
postgres
.
fields
.
jsonb
.
JSONField
(
default
=
dict
),
),
migrations
.
AddField
(
model_name
=
'countrytaggeditem'
,
name
=
'custom_content'
,
field
=
django
.
contrib
.
postgres
.
fields
.
jsonb
.
JSONField
(
default
=
dict
),
),
migrations
.
AddField
(
model_name
=
'universitytaggeditem'
,
name
=
'custom_content'
,
field
=
django
.
contrib
.
postgres
.
fields
.
jsonb
.
JSONField
(
default
=
dict
),
),
]
backend/models/module/basicModule.py
View file @
7c38befd
...
...
@@ -2,27 +2,7 @@ from django.db import models
from
backend.models.my_model
import
MyModelVersionned
,
MyModelVersionnedSerializer
,
MyModelVersionnedViewSet
from
django.contrib.postgres.fields
import
JSONField
from
backend.models.tag.tagged_item_validation
import
validate_content_against_config
USEFULL_LINKS_CONFIG
=
{
"usefull_links"
:
{
"type"
:
"array"
,
"content"
:
{
"url"
:
{
"type"
:
"url"
,
"required"
:
True
,
"validators"
:
{}
},
"description"
:
{
"type"
:
"text"
,
"required"
:
True
,
"validators"
:
{
"max_length"
:
500
}
}
},
"required"
:
True
}
}
from
backend.models.tag.tags_config
import
USEFULL_LINKS_CONFIG
class
BasicModule
(
MyModelVersionned
):
...
...
@@ -38,7 +18,7 @@ class BasicModuleSerializer(MyModelVersionnedSerializer):
def
validate
(
self
,
attrs
):
content
=
{
'usefull_links'
:
attrs
[
'usefull_links'
]}
config
=
USEFULL_LINKS_CONFIG
config
=
{
'usefull_links'
:
USEFULL_LINKS_CONFIG
}
validate_content_against_config
(
config
,
content
)
return
attrs
...
...
backend/models/tag/taggedItem.py
View file @
7c38befd
...
...
@@ -2,10 +2,12 @@ from django.db import models
from
backend.models.my_model
import
MyModelVersionned
,
MyModelVersionnedSerializer
,
MyModelVersionnedViewSet
from
.tag
import
Tag
from
.tagged_item_validation
import
tagged_item_validation
from
django.contrib.postgres.fields
import
JSONField
class
TaggedItem
(
MyModelVersionned
):
tag
=
models
.
ForeignKey
(
Tag
,
related_name
=
'+'
,
on_delete
=
models
.
PROTECT
)
custom_content
=
JSONField
(
default
=
dict
)
class
Meta
:
abstract
=
True
...
...
backend/models/tag/tagged_item_validation.py
View file @
7c38befd
from
.checks
import
check_required
from
.validators
import
validate_url
,
validate_text
from
rest_framework.validators
import
ValidationError
from
.tags_config
import
PHOTOS_TAG_GONFIG
from
.tags_config
import
USEFULL_LINKS_CONFIG
def
tagged_item_validation
(
attrs
):
...
...
@@ -19,15 +21,22 @@ def validate_content_against_config(config, content):
field_submitted
=
content
[
field
]
field_config
=
config
[
field
]
field_type
=
field_config
[
'type'
]
if
field_type
==
'url'
:
validate_url
(
field_config
,
field_submitted
)
elif
field_type
==
'text'
:
validate_text
(
field_config
,
field_submitted
)
elif
field_type
==
'photos'
:
validate_content_against_config
({
"photos"
:
PHOTOS_TAG_GONFIG
},
{
"photos"
:
field_submitted
})
elif
field_type
==
'usefull_links'
:
validate_content_against_config
({
"ul"
:
USEFULL_LINKS_CONFIG
},
{
"ul"
:
field_submitted
})
elif
field_type
==
'array'
:
if
type
(
field_submitted
)
is
not
list
:
raise
ValidationError
(
"A array is required here !"
)
raise
ValidationError
(
"A
n
array is required here !"
)
for
item
in
field_submitted
:
validate_content_against_config
(
field_config
[
'content'
],
item
)
else
:
...
...
backend/models/tag/tags_config/__init__.py
0 → 100644
View file @
7c38befd
from
.photos
import
PHOTOS_TAG_GONFIG
# noqa: F401
from
.usefull_links
import
USEFULL_LINKS_CONFIG
# noqa: F401
backend/models/tag/tags_config/photos.py
0 → 100644
View file @
7c38befd
PHOTOS_TAG_GONFIG
=
{
"type"
:
"array"
,
"required"
:
True
,
"content"
:
{
"url"
:
{
"type"
:
"url"
,
"required"
:
True
,
"validators"
:
{
"extension"
:
[
"jpg"
,
"jpeg"
,
"png"
,
"svg"
]
}
},
"title"
:
{
"type"
:
"text"
,
"required"
:
True
,
"validators"
:
{
"max_length"
:
200
}
},
"licence"
:
{
"type"
:
"text"
,
"required"
:
False
,
"validators"
:
{
"max_length"
:
200
}
},
"description"
:
{
"type"
:
"text"
,
"required"
:
False
,
"validators"
:
{
"max_length"
:
500
}
}
}
}
backend/models/tag/tags_config/usefull_links.py
0 → 100644
View file @
7c38befd
USEFULL_LINKS_CONFIG
=
{
"type"
:
"array"
,
"required"
:
True
,
"content"
:
{
"url"
:
{
"type"
:
"url"
,
"required"
:
True
,
"validators"
:
{}
},
"description"
:
{
"type"
:
"text"
,
"required"
:
True
,
"validators"
:
{
"max_length"
:
500
}
}
}
}
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