campusTaggedItem.py 1.15 KB
Newer Older
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
from backend_app.models.campus import Campus
3
4
5
6
7
from backend_app.models.abstract.taggedItem import (
    TaggedItem,
    TaggedItemSerializer,
    TaggedItemViewSet,
)
Florent Chehab's avatar
Florent Chehab committed
8
from backend_app.utils import get_model_config, get_viewset_permissions
9
10


Florent Chehab's avatar
Florent Chehab committed
11
class CampusTaggedItem(TaggedItem):
Florent Chehab's avatar
Florent Chehab committed
12
    model_config = get_model_config("CampusTaggedItem")
13
    campus = models.ForeignKey(
14
15
        Campus, on_delete=models.PROTECT, related_name="campus_tagged_items"
    )
16
17
18
19
20
21

    @classmethod
    def get_serializer(cls):
        return CampusTaggedItemSerializer

    class Meta:
22
        unique_together = ("campus", "tag", "importance_level")
23
24


Florent Chehab's avatar
Florent Chehab committed
25
class CampusTaggedItemSerializer(TaggedItemSerializer):
26
27
    class Meta:
        model = CampusTaggedItem
28
        fields = "__all__"
29
30


Florent Chehab's avatar
Florent Chehab committed
31
class CampusTaggedItemViewSet(TaggedItemViewSet):
32
    permission_classes = get_viewset_permissions("CampusTaggedItemViewSet")
33
34
    queryset = CampusTaggedItem.objects.all()  # pylint: disable=E1101
    serializer_class = CampusTaggedItemSerializer
35

36
    def get_queryset(self):
37
        campus_id = self.kwargs["campus_id"]
38
        return super().get_queryset().filter(campus=campus_id).distinct()