taggedItem.py 993 Bytes
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2 3

from backend_app.fields import JSONField
4
from backend_app.models.abstract.module import Module, ModuleSerializer, ModuleViewSet
Florent Chehab's avatar
Florent Chehab committed
5
from backend_app.models.tag import Tag
6
from backend_app.validators.tags import tagged_item_validation
Florent Chehab's avatar
Florent Chehab committed
7 8


9
class TaggedItem(Module):
Florent Chehab's avatar
Florent Chehab committed
10 11 12 13
    """
    Abstract model to represent a tagged item
    """

14
    tag = models.ForeignKey(Tag, related_name="+", on_delete=models.PROTECT)
15
    custom_content = JSONField(default=dict)
Florent Chehab's avatar
Florent Chehab committed
16 17 18 19 20

    class Meta:
        abstract = True


21
class TaggedItemSerializer(ModuleSerializer):
Florent Chehab's avatar
Florent Chehab committed
22 23 24 25
    """
    Serializer for tagged items
    """

26 27 28
    def validate(self, attrs):
        attrs = super().validate(attrs)

29 30
        tagged_item_validation(attrs)
        return attrs
Florent Chehab's avatar
Florent Chehab committed
31 32


33
class TaggedItemViewSet(ModuleViewSet):
Florent Chehab's avatar
Florent Chehab committed
34 35 36 37
    """
    Tagged item viewset
    """

38
    def get_queryset(self):
Florent Chehab's avatar
Florent Chehab committed
39 40 41
        """
        Extend the queryset for a bit of optimization
        """
42
        return super().get_queryset().prefetch_related("tag")