countryScholarship.py 1.08 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
from django.db import models
Florent Chehab's avatar
Florent Chehab committed
2
from backend_app.models.country import Country
3
4
5
6
7
from backend_app.models.abstract.scholarship import (
    Scholarship,
    ScholarshipSerializer,
    ScholarshipViewSet,
)
Florent Chehab's avatar
Florent Chehab committed
8
from backend_app.utils import get_model_config, get_viewset_permissions
Florent Chehab's avatar
Florent Chehab committed
9
10
11


class CountryScholarship(Scholarship):
Florent Chehab's avatar
Florent Chehab committed
12
    model_config = get_model_config("CountryScholarship")
13
    countries = models.ManyToManyField(Country, related_name="country_scholarships")
Florent Chehab's avatar
Florent Chehab committed
14

15
16
17
18
    @classmethod
    def get_serializer(cls):
        return CountryScholarshipSerializer

Florent Chehab's avatar
Florent Chehab committed
19

20
class CountryScholarshipSerializer(ScholarshipSerializer):
Florent Chehab's avatar
Florent Chehab committed
21
22
    class Meta:
        model = CountryScholarship
23
        fields = "__all__"
Florent Chehab's avatar
Florent Chehab committed
24
25


26
class CountryScholarshipViewSet(ScholarshipViewSet):
27
    permission_classes = get_viewset_permissions("CountryScholarshipViewSet")
Florent Chehab's avatar
Florent Chehab committed
28
29
    queryset = CountryScholarship.objects.all()  # pylint: disable=E1101
    serializer_class = CountryScholarshipSerializer
30

31
    def get_queryset(self):
32
        country_id = self.kwargs["country_id"]
33
        return super().get_queryset().filter(countries__pk=country_id).distinct()