views.py 3.62 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
from cards.models import Card
16 17
from courses.models import Unit

18 19 20

from courses_learning_objects.models import LearningObject

21 22 23 24 25
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model

User = get_user_model()

26 27 28 29 30 31 32 33

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,
34
            'Classroom': Classroom,
35 36
            'Topic': Topic,
            'Forum': Forum,
37 38 39
            'AccessibleArea': AccessibleArea,
            'LearningObject': LearningObject,
            'Card': Card,
40
            'Unit': Unit,
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        }

        # 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)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79


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)

80 81 82 83 84
        if 'target_type' in data and data['target_type'] == 'Classroom':
            target = Classroom.objects.get(id=data['target_id'])
        else:
            target = None

85
        # Save the action
86
        action.send(request.user, verb='access', action_object=action_object, target=target)
87 88

        return Response(None, status=status.HTTP_201_CREATED)
89 90 91


class ActivitiesPagination(PageNumberPagination):
92
    page_size = 30
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    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