from django.db import models from backend_app.fields import JSONField from backend_app.models.abstract.versionedEssentialModule import ( VersionedEssentialModule, VersionedEssentialModuleSerializer, VersionedEssentialModuleViewSet, ) from backend_app.validators.tags import validate_content_against_config from backend_app.validators.tags_config.useful_links import USEFULL_LINKS_CONFIG IMPORTANCE_LEVEL = (("-", "normal"), ("+", "important"), ("++", "IMPORTANT")) class Module(VersionedEssentialModule): """ Abstract module that provides defaults fields: Title, comment, useful_links and importance_level Those field will be inherited. All Basic modules are also "versioned" modules """ title = models.CharField(default="", blank=True, max_length=150) comment = models.CharField(default="", blank=True, max_length=5000) useful_links = JSONField(default=list) importance_level = models.CharField( max_length=2, choices=IMPORTANCE_LEVEL, default="-" ) class Meta: abstract = True class ModuleSerializer(VersionedEssentialModuleSerializer): """ Custom serializer that performs checks on the Basic module filed """ def validate(self, attrs): """ Checks that the useful_links have been filled properly """ attrs = super().validate(attrs) content = {"useful_links": attrs["useful_links"]} config = {"useful_links": USEFULL_LINKS_CONFIG} validate_content_against_config(config, content) return attrs class Meta: model = Module fields = VersionedEssentialModuleSerializer.Meta.fields + ( "title", "comment", "useful_links", "importance_level", ) class ModuleViewSet(VersionedEssentialModuleViewSet): """ Viewset for the Basic Module """ serializer_class = ModuleSerializer