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

Merge branch 'create_classroom_reports' into 'develop'

Create classroom reports

See merge request !17
parents 08b818ab 214e5427
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.models import User
from rest_framework import serializers
from django.utils import timezone
from datetime import timedelta
from courses.models import CourseStudent
......@@ -126,3 +128,21 @@ class UsersByClassSerializer(serializers.Serializer):
def get_groups(self, obj):
return ', '.join([group.name for group in obj.user.groups.all()])
class UserStatsSerializer(serializers.ModelSerializer):
last_access = serializers.SerializerMethodField()
accesses_count = serializers.SerializerMethodField()
class Meta:
model = User
fields = ('name', 'username', 'email', 'id', 'image', 'groups',
'last_access', 'accesses_count')
def get_last_access(self, obj):
count = obj.actor_actions.count()
return obj.actor_actions.last().timestamp if count else None
def get_accesses_count(self, obj):
time_delta = timezone.now() - timedelta(days=7)
return obj.actor_actions.filter(timestamp__gte=time_delta).count()
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.db.models import Count
from rest_framework import viewsets
from rest_framework import viewsets, filters
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
from django_filters.rest_framework import DjangoFilterBackend
from rest_pandas import PandasViewSet
from rest_pandas.renderers import PandasCSVRenderer, PandasJSONRenderer
......@@ -19,8 +23,12 @@ from courses_learning_objects.models import LearningObject, Answer
from courses.reports.serializers import (
UserInDetailSerializer,
UsersByClassSerializer,
UserStatsSerializer
)
from django.core.exceptions import ObjectDoesNotExist
User = get_user_model()
......@@ -296,3 +304,37 @@ class UsersByClassViewSet(PandasViewSet):
def get_pandas_filename(self, request, format):
return 'Relatório de progresso dos usuários'
class ReportPagination(PageNumberPagination):
page_size = 50
page_size_query_param = 'page_size'
max_page_size = 100
class UserStats(viewsets.ReadOnlyModelViewSet):
model = User
queryset = User.objects.all().order_by('name')
serializer_class = UserStatsSerializer
filterset_fields = ['groups']
permission_classes = [IsAuthenticated]
pagination_class = ReportPagination
filter_backends = [
filters.SearchFilter,
DjangoFilterBackend,
]
search_fields = [
'username',
'name',
'first_name',
'last_name',
'email'
]
def get_queryset(self):
queryset = super().get_queryset()
group_id = self.request.query_params.get('group')
queryset = queryset.filter(groups__id=group_id)
return queryset
......@@ -24,6 +24,7 @@ from courses.reports.views import (
SummaryViewSet,
UsersByGroupViewSet,
UsersByClassViewSet,
UserStats
)
from courses.classroom.views import (
BasicClassroomViewSet,
......@@ -54,6 +55,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'course_carousel', views.CarouselCourseView, base_name='course_carousel')
......
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