recommendation.py 1.46 KB
Newer Older
Florent Chehab's avatar
Florent Chehab committed
1
2
3
from django.db import models
from django.core.validators import MaxValueValidator

Florent Chehab's avatar
Florent Chehab committed
4
from backend_app.models.university import University
5
6
7
8
9
from backend_app.models.user import (
    UserRestrictedModule,
    UserRestrictedModuleSerializer,
    UserRestrictedModuleViewSet,
)
Florent Chehab's avatar
Florent Chehab committed
10
from backend_app.models.other_core import Specialty
Florent Chehab's avatar
Florent Chehab committed
11

Florent Chehab's avatar
Florent Chehab committed
12
13
from backend_app.models.user import RecommendationList
from backend_app.utils import get_model_config, get_viewset_permissions
Florent Chehab's avatar
Florent Chehab committed
14
15
16


class Recommendation(UserRestrictedModule):
Florent Chehab's avatar
Florent Chehab committed
17
    model_config = get_model_config("Recommendation")
Florent Chehab's avatar
Florent Chehab committed
18
19
    university = models.ForeignKey(University, on_delete=models.PROTECT)
    parent_list = models.ForeignKey(
20
21
        RecommendationList, on_delete=models.PROTECT, related_name="recommendations"
    )
Florent Chehab's avatar
Florent Chehab committed
22
23
24
    specialty = models.ForeignKey(Specialty, on_delete=models.PROTECT)
    order_in_list = models.PositiveIntegerField()

25
26
    comment = models.CharField(default="", blank=True, max_length=5000)
    grade = models.PositiveIntegerField(null=True, validators=[MaxValueValidator(20)])
Florent Chehab's avatar
Florent Chehab committed
27
28

    class Meta:
29
        unique_together = ("order_in_list", "parent_list")
Florent Chehab's avatar
Florent Chehab committed
30
31
32
33
34


class RecommendationSerializer(UserRestrictedModuleSerializer):
    class Meta:
        model = Recommendation
35
        fields = "__all__"
Florent Chehab's avatar
Florent Chehab committed
36
37


38
class RecommendationViewSet(UserRestrictedModuleViewSet):
39
    permission_classes = get_viewset_permissions("RecommendationViewSet")
Florent Chehab's avatar
Florent Chehab committed
40
41
    queryset = Recommendation.objects.all()  # pylint: disable=E1101
    serializer_class = RecommendationSerializer