exchangeFeedback.py 3.56 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
from backend_app.utils import CustomPagination
16 17 18


class ExchangeFeedback(EssentialModule):
19
    moderation_level = ModerationLevels.DEPENDING_ON_SITE_SETTINGS
20 21 22 23 24 25 26 27 28 29
    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(
30
        default=0, validators=[MinValueValidator(-5), MaxValueValidator(5)]
31
    )
32
    foreign_student_welcome = models.IntegerField(
33
        default=0, validators=[MinValueValidator(-5), MaxValueValidator(5)]
34 35
    )
    cultural_interest = models.IntegerField(
36
        default=0, validators=[MinValueValidator(-5), MaxValueValidator(5)]
37 38
    )

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

42 43 44
    # Field to tell that the instance hasn't been edited as of now
    untouched = models.BooleanField(default=True, null=False)

45 46

class ExchangeFeedbackSerializer(EssentialModuleSerializer):
47 48
    exchange = ExchangeSerializer(read_only=True)

49 50 51 52
    def update(self, instance, validated_data):
        instance.untouched = False
        return super().update(instance, validated_data)

53 54
    class Meta:
        model = ExchangeFeedback
55 56 57 58 59 60 61
        fields = EssentialModuleSerializer.Meta.fields + (
            "university",
            "exchange",
            "general_comment",
            "academical_level_appreciation",
            "foreign_student_welcome",
            "cultural_interest",
62
            "untouched",
63
        )
64
        read_only_fields = ("university", "exchange", "untouched")
65 66 67 68 69 70 71 72 73


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
74 75 76


class ExchangeFeedbackViewSet(EssentialModuleViewSet):
77 78 79 80
    permission_classes = (
        NoDelete & NoPost & (ReadOnly | IsStaff | ExchangePermission),
    )
    queryset = (
81 82 83 84
        ExchangeFeedback.objects.filter(
            exchange__unlinked=False
        )  # only display linked instances
        .order_by("-exchange__year", "exchange__semester")
85 86 87 88 89 90 91 92
        .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",
        )
    )
93 94
    serializer_class = ExchangeFeedbackSerializer
    end_point_route = "exchangeFeedbacks"
95 96 97 98 99
    filterset_fields = (
        "university",
        "exchange__student_major",
        "exchange__student_minor",
    )
100
    required_filterset_fields = ("university",)
101
    pagination_class = CustomPagination