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

Merge branch 'create_actions_reports' into 'develop'

Create Actions Reports

See merge request !18
parents 18f6ad5e 2bbcb929
......@@ -312,7 +312,7 @@ class ReportPagination(PageNumberPagination):
max_page_size = 100
class UserStats(viewsets.ReadOnlyModelViewSet):
class UserStatsViewSet(viewsets.ReadOnlyModelViewSet):
model = User
queryset = User.objects.all().order_by('name')
serializer_class = UserStatsSerializer
......
from rest_framework import serializers
from actstream.models import Action
from courses.reports.serializers import UserStatsSerializer
from courses.serializers import SimpleUserSerializer
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
from discussion.models import Comment, Topic
from datetime import datetime
class UserActionsSerializer(serializers.Serializer):
verb = serializers.CharField(required=True, max_length=100)
......@@ -11,3 +24,59 @@ 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 SimpleUserSerializer(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)
action_phrase = serializers.SerializerMethodField()
timestamp = serializers.SerializerMethodField()
class Meta:
model = Action
fields = ('actor', 'target', 'action_object', 'verb', 'action_phrase', 'timestamp')
def get_timestamp(self, obj):
timestamp_str = obj.timestamp.strftime('%d-%m-%Y %H:%M:%S')
date, hour = timestamp_str.split()
return date + ' às ' + hour
def get_object_type(self, obj):
if isinstance(obj, Classroom):
return ' na sala '
if isinstance(obj, Comment):
return ' no comentário '
if isinstance(obj, Topic):
return ' no tópico '
return ''
def get_action_phrase(self, obj):
verbs = {
'access': 'acessou',
'answered': 'respondeu',
'created': 'criou',
'reacted': 'reagiu'
}
action_phrase = ''
action_phrase += obj.actor.name
action_phrase += (' ' + verbs.get(obj.verb))
if obj.target:
action_phrase += (self.get_object_type(obj.target) + str(obj.target))
if obj.action_object:
action_phrase += (self.get_object_type(obj.action_object) + str(obj.action_object))
return action_phrase
from django.conf.urls import url, include
from courses.stats.views import (
UserActionsView,
UserAccessView
UserAccessView,
)
......
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
......@@ -24,7 +24,7 @@ from courses.reports.views import (
SummaryViewSet,
UsersByGroupViewSet,
UsersByClassViewSet,
UserStats
UserStatsViewSet
)
from courses.classroom.views import (
BasicClassroomViewSet,
......@@ -33,6 +33,7 @@ from courses.classroom.views import (
)
from courses.stats.views import (
UserActionsView,
UserActivitiesViewSet
)
......@@ -55,7 +56,7 @@ router.register(r'class_basic', BasicClassViewSet)
router.register(r'summary', SummaryViewSet, base_name='summary')
router.register(r'users-by-group', UsersByGroupViewSet, base_name='users-by-group')
router.register(r'users-by-class', UsersByClassViewSet, base_name='users-by-class')
router.register(r'user-reports', UserStats, base_name='user-reports')
router.register(r'user-reports', UserStatsViewSet, base_name='user-reports')
# router.register(r'course_carousel', views.CarouselCourseView, base_name='course_carousel')
......@@ -70,6 +71,9 @@ router.register(r'classrooms', BasicClassroomViewSet, base_name='classrooms')
router.register(r'classroom', ClassroomViewSet, base_name='classroom')
router.register(r'event', EventViewSet, base_name='event')
# Stats
router.register(r'user-activities', UserActivitiesViewSet, base_name='user-activities')
app_name = 'courses'
urlpatterns = [
# Stats (Activity Stream related functionality)
......
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