exchangeFeedback.py 2.93 KB
Newer Older
1
from django.core.validators import MaxValueValidator, MinValueValidator
2 3
from django.db import models
from rest_framework.permissions import BasePermission
4 5 6 7 8 9 10 11

from backend_app.models.abstract.essentialModule import (
    EssentialModule,
    EssentialModuleSerializer,
    EssentialModuleViewSet,
)
from backend_app.models.exchange import Exchange
from backend_app.models.university import University
12 13 14
from backend_app.permissions.app_permissions import ReadOnly, IsStaff, NoDelete, NoPost
from backend_app.permissions.moderation import ModerationLevels
from backend_app.serializers import ExchangeSerializer
15 16 17


class ExchangeFeedback(EssentialModule):
18
    moderation_level = ModerationLevels.DEPENDING_ON_SITE_SETTINGS
19 20 21 22 23 24 25 26 27 28
    exchange = models.OneToOneField(
        Exchange,
        on_delete=models.CASCADE,
        related_name="feedbacks",
        primary_key=True,
        null=False,
        default=0,
    )
    general_comment = models.TextField(null=True, max_length=1500)
    academical_level_appreciation = models.IntegerField(
29
        validators=[MinValueValidator(-5), MaxValueValidator(5)]
30
    )
31 32 33 34 35
    foreign_student_welcome = models.IntegerField(
        validators=[MinValueValidator(-5), MaxValueValidator(5)]
    )
    cultural_interest = models.IntegerField(
        validators=[MinValueValidator(-5), MaxValueValidator(5)]
36 37
    )

38 39 40
    # A bit of denormalization (managed by signals)
    university = models.ForeignKey(University, on_delete=models.PROTECT, null=True)

41 42

class ExchangeFeedbackSerializer(EssentialModuleSerializer):
43 44
    exchange = ExchangeSerializer(read_only=True)

45 46
    class Meta:
        model = ExchangeFeedback
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        fields = EssentialModuleSerializer.Meta.fields + (
            "university",
            "exchange",
            "general_comment",
            "academical_level_appreciation",
            "foreign_student_welcome",
            "cultural_interest",
        )
        read_only_fields = ("university", "exchange")


class ExchangePermission(BasePermission):
    """
    Permission that checks that the requester is the student concern by the exchange.
    """

    def has_object_permission(self, request, view, obj):
        return request.user.pk == obj.exchange.student.pk
65 66 67


class ExchangeFeedbackViewSet(EssentialModuleViewSet):
68 69 70 71 72 73 74 75 76 77 78 79 80
    permission_classes = (
        NoDelete & NoPost & (ReadOnly | IsStaff | ExchangePermission),
    )
    queryset = (
        ExchangeFeedback.objects.all()
        .select_related("exchange", "updated_by", "moderated_by", "exchange__student")
        .prefetch_related(
            "exchange__exchange_courses",
            "exchange__exchange_courses__course_feedback",
            "exchange__exchange_courses__course_feedback__updated_by",
            "exchange__exchange_courses__course_feedback__moderated_by",
        )
    )
81 82
    serializer_class = ExchangeFeedbackSerializer
    end_point_route = "exchangeFeedbacks"
83
    filterset_fields = ("university",)
84
    required_filterset_fields = ("university",)