basicModule.py 1.66 KB
Newer Older
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
3

from backend_app.fields import JSONField
4
5
6
7
8
from backend_app.models.abstract.my_model import (
    MyModelVersionned,
    MyModelVersionnedSerializer,
    MyModelVersionnedViewSet,
)
Florent Chehab's avatar
Florent Chehab committed
9
10
from backend_app.validators.tag import validate_content_against_config
from backend_app.validators.tag.tags_config import USEFULL_LINKS_CONFIG
11

12
IMPORTANCE_LEVEL = (("-", "normal"), ("+", "important"), ("++", "IMPORTANT"))
13

14

15
class BasicModule(MyModelVersionned):
Florent Chehab's avatar
Florent Chehab committed
16
17
18
19
20
21
22
23
24
    """
    Abstract module that provides defaults fields:
    Title, comment, useful_links and importance_level

    Those field will be inherited.

    All Basic modules are also "versionned" modules
    """

25
26
    title = models.CharField(default="", blank=True, max_length=150)
    comment = models.CharField(default="", blank=True, max_length=5000)
Florent Chehab's avatar
Florent Chehab committed
27
    useful_links = JSONField(default=list)
28
    importance_level = models.CharField(
29
        max_length=2, choices=IMPORTANCE_LEVEL, default="-"
30
    )
31

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


36
class BasicModuleSerializer(MyModelVersionnedSerializer):
Florent Chehab's avatar
Florent Chehab committed
37
38
39
40
    """
    Custom serializer that performs checks on the Basic module filed
    """

Florent Chehab's avatar
Florent Chehab committed
41
    def my_validate(self, attrs):
Florent Chehab's avatar
Florent Chehab committed
42
43
44
45
        """
        Checks that the useful_links have been filled properly
        """

46
47
        content = {"useful_links": attrs["useful_links"]}
        config = {"useful_links": USEFULL_LINKS_CONFIG}
48
49
50
        validate_content_against_config(config, content)
        return attrs

Florent Chehab's avatar
Florent Chehab committed
51
52
    class Meta:
        model = BasicModule
53
        fields = "__all__"
54
55
56


class BasicModuleViewSet(MyModelVersionnedViewSet):
Florent Chehab's avatar
Florent Chehab committed
57
58
59
60
    """
    Viewset for the Basic Module
    """

61
    serializer_class = BasicModuleSerializer