serializers.py 2.6 KB
Newer Older
1
from django.contrib.auth.models import User
2
from django.http import Http404
3 4
from rest_framework import serializers

5
from openedx.core.djangoapps.course_groups.cohorts import is_course_cohorted
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
from notification_prefs import NOTIFICATION_PREF_KEY
from lang_pref import LANGUAGE_KEY


class NotifierUserSerializer(serializers.ModelSerializer):
    """
    A serializer containing all information about a user needed by the notifier
    (namely the user's name, email address, notification and language
    preferences, and course enrollment and cohort information).

    Because these pieces of information reside in different tables, this is
    designed to work well with prefetch_related and select_related, which
    require the use of all() instead of get() or filter(). The following fields
    should be prefetched on the user objects being serialized:
     * profile
     * preferences
     * courseenrollment_set
     * course_groups
     * roles__permissions
    """
    name = serializers.SerializerMethodField("get_name")
    preferences = serializers.SerializerMethodField("get_preferences")
    course_info = serializers.SerializerMethodField("get_course_info")

    def get_name(self, user):
        return user.profile.name

    def get_preferences(self, user):
        return {
            pref.key: pref.value
            for pref
            in user.preferences.all()
            if pref.key in [LANGUAGE_KEY, NOTIFICATION_PREF_KEY]
        }

    def get_course_info(self, user):
        cohort_id_map = {
            cohort.course_id: cohort.id
            for cohort in user.course_groups.all()
        }
        see_all_cohorts_set = {
            role.course_id
            for role in user.roles.all()
            for perm in role.permissions.all() if perm.name == "see_all_cohorts"
        }
51 52 53 54 55 56 57 58 59 60 61 62 63 64
        ret = {}
        for enrollment in user.courseenrollment_set.all():
            if enrollment.is_active:
                try:
                    ret[unicode(enrollment.course_id)] = {
                        "cohort_id": cohort_id_map.get(enrollment.course_id),
                        "see_all_cohorts": (
                            enrollment.course_id in see_all_cohorts_set or
                            not is_course_cohorted(enrollment.course_id)
                        ),
                    }
                except Http404:  # is_course_cohorted raises this if course does not exist
                    pass
        return ret
65

stv committed
66
    class Meta(object):  # pylint: disable=missing-docstring
67 68 69
        model = User
        fields = ("id", "email", "name", "preferences", "course_info")
        read_only_fields = ("id", "email")