module.py 1.73 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
from backend_app.models.abstract.versionedEssentialModule import (
    VersionedEssentialModule,
    VersionedEssentialModuleSerializer,
    VersionedEssentialModuleViewSet,
8
)
9 10
from backend_app.validators.tags import validate_content_against_config
from backend_app.validators.tags_config.useful_links import USEFULL_LINKS_CONFIG
11

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

14

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

    Those field will be inherited.

22
    All Basic modules are also "versioned" modules
Florent Chehab's avatar
Florent Chehab committed
23 24
    """

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 ModuleSerializer(VersionedEssentialModuleSerializer):
Florent Chehab's avatar
Florent Chehab committed
37 38 39 40
    """
    Custom serializer that performs checks on the Basic module filed
    """

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

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

51 52
        return attrs

Florent Chehab's avatar
Florent Chehab committed
53
    class Meta:
54
        model = Module
55
        fields = "__all__"
56 57


58
class ModuleViewSet(VersionedEssentialModuleViewSet):
Florent Chehab's avatar
Florent Chehab committed
59 60 61 62
    """
    Viewset for the Basic Module
    """

63
    serializer_class = ModuleSerializer