cityTaggedItem.py 1.12 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.city import City
from backend_app.utils import get_model_config, get_viewset_permissions
9
10


Florent Chehab's avatar
Florent Chehab committed
11
class CityTaggedItem(TaggedItem):
Florent Chehab's avatar
Florent Chehab committed
12
    model_config = get_model_config("CityTaggedItem")
13

14
    city = models.ForeignKey(
15
16
        City, on_delete=models.PROTECT, related_name="city_tagged_items"
    )
17
18
19
20
21
22

    @classmethod
    def get_serializer(cls):
        return CityTaggedItemSerializer

    class Meta:
23
        unique_together = ("city", "tag", "importance_level")
24
25


Florent Chehab's avatar
Florent Chehab committed
26
class CityTaggedItemSerializer(TaggedItemSerializer):
27
28
    class Meta:
        model = CityTaggedItem
29
        fields = "__all__"
30
31


Florent Chehab's avatar
Florent Chehab committed
32
class CityTaggedItemViewSet(TaggedItemViewSet):
33
    permission_classes = get_viewset_permissions("CityTaggedItemViewSet")
34
35
    queryset = CityTaggedItem.objects.all()  # pylint: disable=E1101
    serializer_class = CityTaggedItemSerializer
36
37

    def extend_queryset(self):
38
        city_id = self.kwargs["city_id"]
39
        return self.my_model_queryset.filter(city=city_id).distinct()