serializers.py 2.67 KB
Newer Older
1 2 3
"""
Django REST Framework serializers for the User API application
"""
4 5 6
from django.contrib.auth.models import User
from rest_framework import serializers
from student.models import UserProfile
7 8

from .models import UserPreference
9 10 11


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

    def get_name(self, user):
19 20 21
        """
        Return the name attribute from the user profile object
        """
22 23 24
        profile = UserProfile.objects.get(user=user)
        return profile.name

25
    def get_preferences(self, user):
26 27 28
        """
        Returns the set of preferences as a dict for the specified user
        """
29 30
        return dict([(pref.key, pref.value) for pref in user.preferences.all()])

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


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

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


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

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


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', '')
76 77 78 79 80 81 82 83

    @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()))