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


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

    @classmethod
    def get_serializer(cls):
        return UniversityTaggedItemSerializer

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


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


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

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