serializers.py 3.56 KB
Newer Older
1 2 3
"""
Django REST Framework serializers for the User API application
"""
4
from django.contrib.auth.models import User
5
from django.utils.timezone import now
6
from rest_framework import serializers
7

8
from lms.djangoapps.verify_student.models import SoftwareSecurePhotoVerification
9
from student.models import UserProfile
10 11

from .models import UserPreference
12 13 14


class UserSerializer(serializers.HyperlinkedModelSerializer):
15 16 17
    """
    Serializer that generates a representation of a User entity containing a subset of fields
    """
18 19
    name = serializers.SerializerMethodField()
    preferences = serializers.SerializerMethodField()
20 21

    def get_name(self, user):
22
        """
23
        Return the name attribute from the user profile object if profile exists else none
24
        """
25
        return user.profile.name
26

27
    def get_preferences(self, user):
28 29 30
        """
        Returns the set of preferences as a dict for the specified user
        """
31
        return UserPreference.get_all_preferences(user)
32

33
    class Meta(object):
34 35
        model = User
        # This list is the minimal set required by the notification service
36
        fields = ("id", "url", "email", "name", "username", "preferences")
37
        read_only_fields = ("id", "email", "username")
38 39 40


class UserPreferenceSerializer(serializers.HyperlinkedModelSerializer):
41 42 43
    """
    Serializer that generates a represenation of a UserPreference entity
    """
44 45
    user = UserSerializer()

46
    class Meta(object):
47 48
        model = UserPreference
        depth = 1
49 50 51 52


class RawUserPreferenceSerializer(serializers.ModelSerializer):
    """
53 54 55
    Serializer that generates a raw representation of a user preference.
    """
    user = serializers.PrimaryKeyRelatedField(queryset=User.objects.all())
56

57
    class Meta(object):
58 59
        model = UserPreference
        depth = 1
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


class ReadOnlyFieldsSerializerMixin(object):
    """
    Mixin for use with Serializers that provides a method
    `get_read_only_fields`, which returns a tuple of all read-only
    fields on the Serializer.
    """
    @classmethod
    def get_read_only_fields(cls):
        """
        Return all fields on this Serializer class which are read-only.
        Expects sub-classes implement Meta.explicit_read_only_fields,
        which is a tuple declaring read-only fields which were declared
        explicitly and thus could not be added to the usual
        cls.Meta.read_only_fields tuple.
        """
        return getattr(cls.Meta, 'read_only_fields', '') + getattr(cls.Meta, 'explicit_read_only_fields', '')
78 79 80 81 82 83 84 85

    @classmethod
    def get_writeable_fields(cls):
        """
        Return all fields on this serializer that are writeable.
        """
        all_fields = getattr(cls.Meta, 'fields', tuple())
        return tuple(set(all_fields) - set(cls.get_read_only_fields()))
86 87 88 89 90 91


class CountryTimeZoneSerializer(serializers.Serializer):  # pylint: disable=abstract-method
    """
    Serializer that generates a list of common time zones for a country
    """
92 93
    time_zone = serializers.CharField()
    description = serializers.CharField()
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110


class SoftwareSecurePhotoVerificationSerializer(serializers.ModelSerializer):
    """
    Serializer that generates a representation of a user's photo verification status.
    """
    is_verified = serializers.SerializerMethodField()

    def get_is_verified(self, obj):
        """
        Return a boolean indicating if a the user is verified.
        """
        return obj.status == 'approved' and obj.expiration_datetime > now()

    class Meta(object):
        fields = ('status', 'expiration_datetime', 'is_verified')
        model = SoftwareSecurePhotoVerification