views.py 3.37 KB
Newer Older
1
from actstream import action
2 3
from actstream.models import user_stream, Action, actor_stream

4 5 6
from rest_framework import views, viewsets, mixins, permissions, status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
7
from rest_framework.pagination import PageNumberPagination
8 9

from discussion.models import Forum, Topic
10
from .serializers import UserActionsSerializer, UserAccessSerializer, ActionSerializer
11
from .models import AccessibleArea
12
from ..models import Course
13
from ..classroom.models import Classroom
14

15 16 17 18
from cards.models import Card

from courses_learning_objects.models import LearningObject

19 20 21 22 23
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model

User = get_user_model()

24 25 26 27 28 29 30 31

class UserActionsView(views.APIView):
    permission_classes = [permissions.IsAuthenticated]

    def post(self, request, format=None):
        # Only models from this list are registered to be used on actions
        allowed_models = {
            'Course': Course,
32
            'Classroom': Classroom,
33 34
            'Topic': Topic,
            'Forum': Forum,
35 36 37
            'AccessibleArea': AccessibleArea,
            'LearningObject': LearningObject,
            'Card': Card,
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        }

        # Validate incoming data
        serializer = UserActionsSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        data = serializer.data

        # Get the actual model objects for action and target
        action_object = None
        target = None
        if 'action_object_type' in data:
            action_object = allowed_models[data['action_object_type']].objects.get(id=data['action_object_id'])
        if 'target_type' in data:
            target = allowed_models[data['target_type']].objects.get(id=data['target_id'])

        # Save the action
        action.send(request.user, verb=data['verb'], action_object=action_object, target=target)

        return Response(None, status=status.HTTP_201_CREATED)
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77


class UserAccessView(views.APIView):
    permission_classes = [permissions.IsAuthenticated]

    def post(self, request, format=None):

        # Validate incoming data
        serializer = UserAccessSerializer(data=request.data)
        if not serializer.is_valid():
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
        data = serializer.data

        # Get the actual model object for action
        try:
            action_object = AccessibleArea.objects.get(slug=data['area'])
        except AccessibleArea.DoesNotExist:
            return Response({'error': 'Specified area not found'}, status=status.HTTP_404_NOT_FOUND)

        # Save the action
78
        action.send(request.user, verb='access', action_object=action_object)
79 80

        return Response(None, status=status.HTTP_201_CREATED)
81 82 83


class ActivitiesPagination(PageNumberPagination):
84
    page_size = 30
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
    page_size_query_param = 'page_size'
    max_page_size = 100


class UserActivitiesViewSet(viewsets.ReadOnlyModelViewSet):
    serializer_class = ActionSerializer
    permission_classes = [permissions.IsAuthenticated]
    pagination_class = ActivitiesPagination
    model = Action

    def get_queryset(self):
        user_id = self.request.GET.get('user')
        user = User.objects.get(id=user_id)

        queryset = actor_stream(user)

        return queryset