universityScholarship.py 1.13 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):
Florent Chehab's avatar
Florent Chehab committed
23 24
    class Meta:
        model = UniversityScholarship
25
        fields = "__all__"
Florent Chehab's avatar
Florent Chehab committed
26 27


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

33
    def get_queryset(self):
34
        univ_id = self.kwargs["univ_id"]
35
        return super().get_queryset().filter(universities__pk=univ_id).distinct()