Commit 214e5427 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

[Classroom Status] Add access info to UserStats serialization

parent 5b39ac1d
......@@ -2,6 +2,8 @@ from django.contrib.auth import get_user_model
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
......@@ -129,6 +131,18 @@ class UsersByClassSerializer(serializers.Serializer):
class UserStatsSerializer(serializers.ModelSerializer):
last_access = serializers.SerializerMethodField()
accesses_count = serializers.SerializerMethodField()
class Meta:
model = User
fields = ('name', 'username', 'email', 'id', 'image', 'groups')
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()
......@@ -317,7 +317,7 @@ class UserStats(viewsets.ReadOnlyModelViewSet):
queryset = User.objects.all().order_by('name')
serializer_class = UserStatsSerializer
filterset_fields = ['groups']
#permission_classes = [IsAuthenticated]
permission_classes = [IsAuthenticated]
pagination_class = ReportPagination
filter_backends = [
filters.SearchFilter,
......
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