Commit 7c38befd authored by Florent Chehab's avatar Florent Chehab

Centralized tag definition

parent 4553b543
Pipeline #26844 passed with stages
......@@ -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
}
}
......
# 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),
),
]
......@@ -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
......
......@@ -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
......
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("An array is required here !")
for item in field_submitted:
validate_content_against_config(field_config['content'], item)
else:
......
from .photos import PHOTOS_TAG_GONFIG # noqa: F401
from .usefull_links import USEFULL_LINKS_CONFIG # noqa: F401
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
}
}
}
}
USEFULL_LINKS_CONFIG = {
"type": "array",
"required": True,
"content": {
"url": {
"type": "url",
"required": True,
"validators": {}
},
"description": {
"type": "text",
"required": True,
"validators": {
"max_length": 500
}
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment