views.py 1.54 KB
Newer Older
1 2
from django.contrib.auth.models import User
from rest_framework.viewsets import ReadOnlyModelViewSet
3 4
from rest_framework.response import Response
from rest_framework import pagination
5 6 7

from notification_prefs import NOTIFICATION_PREF_KEY
from notifier_api.serializers import NotifierUserSerializer
8
from openedx.core.lib.api.permissions import ApiKeyHeaderPermission
9 10


11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
class NotifierPaginator(pagination.PageNumberPagination):
    """
    Paginator for the notifier API.
    """
    page_size = 10
    page_size_query_param = "page_size"

    def get_paginated_response(self, data):
        """
        Construct a response with pagination information.
        """
        return Response({
            'next': self.get_next_link(),
            'previous': self.get_previous_link(),
            'count': self.page.paginator.count,
            'results': data
        })


30 31 32 33 34 35 36 37
class NotifierUsersViewSet(ReadOnlyModelViewSet):
    """
    An endpoint that the notifier can use to retrieve users who have enabled
    daily forum digests, including all information that the notifier needs about
    such users.
    """
    permission_classes = (ApiKeyHeaderPermission,)
    serializer_class = NotifierUserSerializer
38
    pagination_class = NotifierPaginator
39 40 41 42 43 44 45 46 47 48 49 50

    # See NotifierUserSerializer for notes about related tables
    queryset = User.objects.filter(
        preferences__key=NOTIFICATION_PREF_KEY
    ).select_related(
        "profile"
    ).prefetch_related(
        "preferences",
        "courseenrollment_set",
        "course_groups",
        "roles__permissions"
    )