Commit 2a94dcd9 authored by Matheus Miranda's avatar Matheus Miranda
Browse files

create UserAccessView

parent 0982085f
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.db.models import Count
from django.utils import timezone
from datetime import timedelta
from rest_framework import viewsets, filters
from rest_framework import viewsets, filters, views, permissions, status
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.pagination import PageNumberPagination
......@@ -14,6 +16,7 @@ from rest_pandas.renderers import PandasCSVRenderer, PandasJSONRenderer
import pandas as pd
from discussion.models import Comment, CommentLike, Topic, TopicLike
from baquara.users.models import UserLogin
from courses.models import Course, CourseStudent, StudentProgress
from courses.classes.models import Class
......@@ -315,6 +318,7 @@ class ReportPagination(PageNumberPagination):
class UserStatsViewSet(viewsets.ReadOnlyModelViewSet):
model = User
queryset = User.objects.all().order_by('name')
group_id = ''
serializer_class = UserStatsSerializer
filterset_fields = ['groups']
permission_classes = [IsAuthenticated]
......@@ -331,6 +335,11 @@ class UserStatsViewSet(viewsets.ReadOnlyModelViewSet):
'email'
]
def get_serializer_context(self):
return {
'group': self.request.query_params.get('group')
}
def get_queryset(self):
queryset = super().get_queryset()
group_id = self.request.query_params.get('group')
......@@ -338,3 +347,42 @@ class UserStatsViewSet(viewsets.ReadOnlyModelViewSet):
queryset = queryset.filter(groups__id=group_id)
return queryset
class UserAccessView(views.APIView):
permission_classes = [permissions.IsAuthenticated]
def get(self, request, format=None):
user_id = request.GET['user']
user = User.objects.get(id=user_id)
actions = user.actor_actions.all()
last_login = ''
last_access = ''
accesses_7_days = 0
accesses_30_days = 0
if actions:
last_access_str = actions.filter(verb='access').last().timestamp.strftime('%d-%m-%Y %H:%M:%S')
date, hour = last_access_str.split()
last_access = date + ' às ' + hour
last_login_str = user.last_login.strftime('%d-%m-%Y %H:%M:%S')
date, hour = last_login_str.split()
last_login = date + ' às ' + hour
time_delta = timezone.now() - timedelta(days=7)
accesses_7_days = actions.filter(verb='access', timestamp__gte=time_delta).count()
time_delta = timezone.now() - timedelta(days=30)
accesses_30_days = actions.filter(verb='access', timestamp__gte=time_delta).count()
access_data = {
'last_login': last_login,
'last_access': last_access,
'accesses_7_days': accesses_7_days,
'accesses_30_days': accesses_30_days
}
return Response(data=access_data, status=status.HTTP_200_OK)
......@@ -24,7 +24,8 @@ from courses.reports.views import (
SummaryViewSet,
UsersByGroupViewSet,
UsersByClassViewSet,
UserStatsViewSet
UserStatsViewSet,
UserAccessView
)
from courses.classroom.views import (
BasicClassroomViewSet,
......@@ -82,4 +83,6 @@ urlpatterns = [
url(r'^course/(?P<course_id>[1-9][0-9]*)/export/$', ExportCourseView.as_view(), name="course_export"),
url(r'^course/import/$', ImportCourseView.as_view(), name="course_import"),
url(r'^', include(router.urls)),
url(r'^user-access', UserAccessView.as_view(), name='user-access')
]
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