Commit a2019407 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

[Classroom Status] Add class to serialize User Actions

parent ae75cad6
from rest_framework import serializers
from actstream.models import Action
from courses.reports.serializers import UserStatsSerializer
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
User = get_user_model()
from courses.classroom.models import Classroom
from courses.classroom.serializers import BasicClassroomSerializer
class UserActionsSerializer(serializers.Serializer):
......@@ -11,3 +20,23 @@ class UserActionsSerializer(serializers.Serializer):
class UserAccessSerializer(serializers.Serializer):
area = serializers.CharField(required=True, max_length=100)
class GenericRelatedField(serializers.Field):
def to_representation(self, value):
if isinstance(value, User):
return UserStatsSerializer(value).data
if isinstance(value, Classroom):
return BasicClassroomSerializer(value).data
return str(value)
class ActionSerializer(serializers.ModelSerializer):
actor = GenericRelatedField(read_only=True)
target = GenericRelatedField(read_only=True)
action_object = GenericRelatedField(read_only=True)
verb = GenericRelatedField(read_only=True)
class Meta:
model = Action
fields = ('actor', 'target', 'action_object', 'verb')
from actstream import action
from actstream.models import user_stream, Action, actor_stream
from rest_framework import views, viewsets, mixins, permissions, status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from discussion.models import Forum, Topic
from .serializers import UserActionsSerializer, UserAccessSerializer
from .serializers import UserActionsSerializer, UserAccessSerializer, ActionSerializer
from .models import AccessibleArea
from ..models import Course
from ..classroom.models import Classroom
from django.contrib.auth.models import User
from django.contrib.auth import get_user_model
User = get_user_model()
class UserActionsView(views.APIView):
permission_classes = [permissions.IsAuthenticated]
......@@ -63,3 +71,24 @@ class UserAccessView(views.APIView):
action.send(request.user, verb='access', action_object=action_object)
return Response(None, status=status.HTTP_201_CREATED)
class ActivitiesPagination(PageNumberPagination):
page_size = 50
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment