Commit e7310329 authored by Florent Chehab's avatar Florent Chehab

Began work on models, Basic module working with usefull_links validation.

parent ef93ef29
Pipeline #26281 passed with stages
in 1 minute and 23 seconds
......@@ -4,9 +4,11 @@ from reversion_compare.admin import CompareVersionAdmin
from import University, MainCampus
from rex.models.location import Country, City
from rex.models.module import BasicModule, CompareVersionAdmin), CompareVersionAdmin)
# Generated by Django 2.0.3 on 2018-08-18 08:44
# Generated by Django 2.0.3 on 2018-08-19 11:49
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import rex.utils.friendly_path
......@@ -14,6 +15,14 @@ class Migration(migrations.Migration):
operations = [
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('comment', models.TextField()),
from .module import Module # noqa: F401
from .basicModule import BasicModule # noqa: F401
from django.db import models
from import UsefullLinksField
class BasicModule(models.Model):
comment = models.TextField()
usefull_links = UsefullLinksField()
# class Meta:
# abstract = True
from .DictModeViewSet import DictModeViewSet # noqa: F401
from .usefullLinksField import UsefullLinksField # noqa: F401
from .validateWithRestFramework import validate_with_rest_framework # noqa: F401
from django.contrib.postgres.fields import JSONField
from django.core.exceptions import ValidationError
from rest_framework import serializers
from 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)
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")
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