basicModule.py 1.15 KB
Newer Older
1
from django.db import models
2 3
from backend.models.abstract.my_model import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet
from backend.fields import JSONField
4 5
from backend.validators.tag import validate_content_against_config
from backend.validators.tag.tags_config import USEFULL_LINKS_CONFIG
6

7 8 9 10 11 12
IMPORTANCE_LEVEL = (
    ('-', 'normal'),
    ('+', 'important'),
    ('++', 'IMPORTANT')
)

13

14
class BasicModule(MyModelVersionned):
15

16
    comment = models.TextField(blank=True, default="")
Florent Chehab's avatar
Florent Chehab committed
17
    useful_links = JSONField(default=list)
18 19 20 21 22
    importance_level = models.CharField(
        max_length=2,
        choices=IMPORTANCE_LEVEL,
        default='-',
    )
23

24 25
    class Meta:
        abstract = True
Florent Chehab's avatar
Florent Chehab committed
26 27


28
class BasicModuleSerializer(MyModelVersionnedSerializer):
Florent Chehab's avatar
Florent Chehab committed
29

Florent Chehab's avatar
Florent Chehab committed
30
    def my_validate(self, attrs):
Florent Chehab's avatar
Florent Chehab committed
31 32
        content = {'useful_links': attrs['useful_links']}
        config = {'useful_links': USEFULL_LINKS_CONFIG}
33 34 35
        validate_content_against_config(config, content)
        return attrs

Florent Chehab's avatar
Florent Chehab committed
36 37
    class Meta:
        model = BasicModule
38
        fields = '__all__'
39 40 41 42


class BasicModuleViewSet(MyModelVersionnedViewSet):
    serializer_class = BasicModuleSerializer