serializers.py 4.7 KB
Newer Older
1 2 3 4
from django.contrib.auth import get_user_model
from rest_framework import serializers

from .videos.serializers import VideoSerializer
5 6 7
from .models import (
    Course,
    CourseProfessor,
8 9
    ProfessorMessage,
    ProfessorMessageRead,
10
)
11 12 13 14


User = get_user_model()

15

16 17 18 19 20
class SimpleUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = User
        # The model must have image, biography and name fields
21 22 23
        # FIXME remove email from here, couse this way, one user can
        # all users email.
        fields = ('id', 'image', 'name', 'biography', 'username', 'email')
24 25 26 27 28 29 30 31 32 33 34 35 36 37


class CourseSerializer(serializers.ModelSerializer):

    intro_video = VideoSerializer(required=False)
    home_thumbnail_url = serializers.SerializerMethodField()
    professors = SimpleUserSerializer(read_only=True, many=True)
    is_user_assistant = serializers.SerializerMethodField()
    is_user_coordinator = serializers.SerializerMethodField()
    is_assistant_or_coordinator = serializers.SerializerMethodField()

    class Meta:
        model = Course
        fields = ("id", "slug", "name", "intro_video", "application", "requirement",
38
                  "abstract", "structure", "workload", "status", "is_public",
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
                  "thumbnail_url", "home_thumbnail_url", "home_position",
                  "start_date", "home_published", "authors_names", "has_started",
                  "min_percent_to_complete", "is_user_assistant", "is_user_coordinator",
                  "is_assistant_or_coordinator", 'professors', )

    @staticmethod
    def get_home_thumbnail_url(obj):
        if obj.home_thumbnail:
            return obj.home_thumbnail.url
        return ''

    def get_is_user_assistant(self, obj):
        return obj.is_course_assistant(self.context['request'].user)

    def get_is_user_coordinator(self, obj):
        return obj.is_course_coordinator(self.context['request'].user)

    def get_is_assistant_or_coordinator(self, obj):
        return obj.is_assistant_or_coordinator(self.context['request'].user)

    def update(self, instance, validated_data):
        intro_video_data = validated_data.pop('intro_video', None)

        course = super(CourseSerializer, self).update(instance, validated_data)

        if intro_video_data:
            intro_video_ser = VideoSerializer(course.intro_video, data=intro_video_data)
            if intro_video_ser.is_valid():
                intro_video = intro_video_ser.save()
            course.intro_video = intro_video
            course.save()

        return course
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97


class BasicCourseSerializer(serializers.ModelSerializer):
    home_thumbnail_url = serializers.SerializerMethodField()

    class Meta:
        model = Course
        fields = (
            "id",
            "slug",
            "name",
            "status",
            "home_thumbnail_url",
            "start_date",
            "home_published",
            "has_started",
            "min_percent_to_complete",
            "is_public",
        )

    @staticmethod
    def get_home_thumbnail_url(obj):
        if obj.home_thumbnail:
            return obj.home_thumbnail.url
        return ''

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

class BasicCourseProfessorSerializer(serializers.ModelSerializer):

    user = SimpleUserSerializer(read_only=True)

    class Meta:
        fields = (
            'id',
            'course',
            'user',
            'role',
        )
        model = CourseProfessor


113 114 115 116 117 118 119 120 121 122 123 124 125
class ProfessorMessageReadSerializer(serializers.ModelSerializer):

    class Meta:
        model = ProfessorMessageRead
        fields = ('id', 'message', 'is_read')


class ProfessorMessageSerializer(serializers.ModelSerializer):

    professor = SimpleUserSerializer(read_only=True)
    course_slug = serializers.SerializerMethodField(read_only=True)
    course_name = serializers.SerializerMethodField(read_only=True)
    is_read = serializers.SerializerMethodField(read_only=True)
126
    
127 128 129
    class Meta:
        model = ProfessorMessage
        fields = ('id', 'course', 'course_name', 'course_slug', 'professor',
Bruno Martin's avatar
Bruno Martin committed
130 131 132
                  'users', 'subject', 'message', 'date', 'is_read', 'groups', 'classes',
                  'classrooms',
        )
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

    def get_course_slug(self, obj):
        try:
            return obj.course.slug
        except AttributeError as e:
            return ''  # no course is associated with this message

    def get_course_name(self, obj):
        try:
            return obj.course.name
        except AttributeError as e:
            return ''  # no course is associated with this message

    def get_is_read(self, obj):
        try:
            read_state = ProfessorMessageRead.objects.get(user=self.context['request'].user, message=obj)
            return read_state.is_read
        except ProfessorMessageRead.DoesNotExist as e:
            return False