universityScholarship.py 1.16 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.university import University
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 UniversityScholarship(Scholarship):
Florent Chehab's avatar
Florent Chehab committed
12
    model_config = get_model_config("UniversityScholarship")
13
    universities = models.ManyToManyField(
14 15
        University, related_name="university_scholarships"
    )
Florent Chehab's avatar
Florent Chehab committed
16

17 18 19 20
    @classmethod
    def get_serializer(cls):
        return UniversityScholarshipSerializer

Florent Chehab's avatar
Florent Chehab committed
21

22
class UniversityScholarshipSerializer(ScholarshipSerializer):
23
    FORCE_FULL_DISPLAY = True
Florent Chehab's avatar
Florent Chehab committed
24 25 26

    class Meta:
        model = UniversityScholarship
27
        fields = "__all__"
Florent Chehab's avatar
Florent Chehab committed
28 29


30
class UniversityScholarshipViewSet(ScholarshipViewSet):
31
    permission_classes = get_viewset_permissions("UniversityScholarshipViewSet")
Florent Chehab's avatar
Florent Chehab committed
32 33
    queryset = UniversityScholarship.objects.all()  # pylint: disable=E1101
    serializer_class = UniversityScholarshipSerializer
34 35

    def extend_queryset(self):
36
        univ_id = self.kwargs["univ_id"]
37
        return self.my_model_queryset.filter(universities__pk=univ_id).distinct()