serializers.py 2.24 KB
Newer Older
1 2
from rest_framework import serializers

3 4 5 6 7 8 9 10
from backend_app.models.abstract.base import BaseModelSerializer
from backend_app.models.abstract.essentialModule import EssentialModuleSerializer
from backend_app.models.course import Course
from backend_app.models.courseFeedback import CourseFeedback
from backend_app.models.exchange import Exchange


class CourseFeedbackSerializer(EssentialModuleSerializer):
11 12 13 14 15
    course_code = serializers.SerializerMethodField()  # needed for the front

    def get_course_code(self, obj):
        return obj.course.code

16 17 18 19
    def update(self, instance, validated_data):
        instance.untouched = False
        return super().update(instance, validated_data)

20 21 22
    class Meta:
        model = CourseFeedback
        fields = EssentialModuleSerializer.Meta.fields + (
23
            "course_code",
Segolene Brisemeur's avatar
Segolene Brisemeur committed
24
            "language",
25 26
            "comment",
            "adequation",
27
            "would_recommend",
28
            "working_dose",
29
            "following_ease",
30
            "untouched",
31 32 33
        )


34 35 36 37 38
COURSE_FIELDS = BaseModelSerializer.Meta.fields + (
    "course_feedback",
    "code",
    "title",
    "link",
39
    "ects",
40 41 42 43 44
    "category",
    "profile",
    "tsh_profile",
)

45

46
class CourseSerializerSimple(BaseModelSerializer):
47 48
    class Meta:
        model = Course
49 50
        fields = COURSE_FIELDS
        read_only_fields = COURSE_FIELDS
51 52


53 54 55 56 57 58 59 60
class CourseSerializer(CourseSerializerSimple):
    course_feedback = CourseFeedbackSerializer(many=False, read_only=True)


EXCHANGE_FIELDS = BaseModelSerializer.Meta.fields + (
    "year",
    "semester",
    "duration",
61
    "double_degree",
62
    "master_obtained",
63
    "student_major_and_semester",
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    "student_major",
    "student_minor",
    "student_option",
    "exchange_courses",
    "university",
    "student",
)


class ExchangeSerializerSimple(BaseModelSerializer):
    exchange_courses = CourseSerializerSimple(many=True, read_only=True)
    student = serializers.SerializerMethodField()

    def get_student(self, obj):
        return self.get_user_related_field(obj.student)
79 80 81

    class Meta:
        model = Exchange
82 83 84 85 86 87
        fields = EXCHANGE_FIELDS
        read_only_fields = EXCHANGE_FIELDS


class ExchangeSerializer(ExchangeSerializerSimple):
    exchange_courses = CourseSerializer(many=True, read_only=True)