basicModule.py 1.44 KB
Newer Older
1
from django.db import models
2
from backend.models.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
3
from django.contrib.postgres.fields import JSONField
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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
    }
}
26 27


28
class BasicModule(MyModelVersionned):
29

30
    comment = models.TextField(blank=True)
31
    usefull_links = JSONField(blank=True, default=list)
32

33 34
    class Meta:
        abstract = True
Florent Chehab's avatar
Florent Chehab committed
35 36


37
class BasicModuleSerializer(MyModelVersionnedSerializer):
Florent Chehab's avatar
Florent Chehab committed
38

39 40 41 42 43 44
    def validate(self, attrs):
        content = {'usefull_links': attrs['usefull_links']}
        config = USEFULL_LINKS_CONFIG
        validate_content_against_config(config, content)
        return attrs

Florent Chehab's avatar
Florent Chehab committed
45 46
    class Meta:
        model = BasicModule
47
        fields = '__all__'
48 49 50 51


class BasicModuleViewSet(MyModelVersionnedViewSet):
    serializer_class = BasicModuleSerializer