Commit 5b39ac1d authored by Matheus Miranda's avatar Matheus Miranda
Browse files

[Classroom Status] Update queryset for UserStats


Signed-off-by: default avatarMatheus Miranda <matheusmirandalacerda@gmail.com>
parent c209b763
...@@ -128,43 +128,7 @@ class UsersByClassSerializer(serializers.Serializer): ...@@ -128,43 +128,7 @@ class UsersByClassSerializer(serializers.Serializer):
return ', '.join([group.name for group in obj.user.groups.all()]) return ', '.join([group.name for group in obj.user.groups.all()])
class UserClassRoomStatsSerializer(serializers.ModelSerializer): class UserStatsSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField('get_full_name')
picture = serializers.SerializerMethodField('get_picture_url')
username = serializers.SerializerMethodField()
email = serializers.SerializerMethodField()
user_id = serializers.SerializerMethodField()
course_progress = serializers.SerializerMethodField('get_user_progress')
# forum_questions = serializers.SerializerMethodField('get_forum_questions')
# forum_answers = serializers.SerializerMethodField('get_forum_answers')
# lessons_stats = LessonUserStatsSerializer(many=True, allow_add_remove=False)
class Meta: class Meta:
model = User model = User
fields = ('name', 'username', 'email', 'user_id', 'course_progress', 'picture') fields = ('name', 'username', 'email', 'id', 'image', 'groups')
def get_full_name(self, obj):
return obj.get_full_name()
def get_picture_url(self, obj):
return obj.get_picture_thumb_url()
def get_username(self, obj):
return obj.username
def get_email(self, obj):
return obj.email
def get_user_id(self, obj):
return obj.id
def get_user_progress(self, obj):
return obj.percent_progress()
# def get_forum_questions(self, obj):
# return obj.forum_questions_by_lesson()
#
# def get_forum_answers(self, obj):
# return obj.forum_answers_by_lesson()
...@@ -23,9 +23,12 @@ from courses_learning_objects.models import LearningObject, Answer ...@@ -23,9 +23,12 @@ from courses_learning_objects.models import LearningObject, Answer
from courses.reports.serializers import ( from courses.reports.serializers import (
UserInDetailSerializer, UserInDetailSerializer,
UsersByClassSerializer, UsersByClassSerializer,
UserClassRoomStatsSerializer UserStatsSerializer
) )
from django.core.exceptions import ObjectDoesNotExist
User = get_user_model() User = get_user_model()
...@@ -309,12 +312,12 @@ class ReportPagination(PageNumberPagination): ...@@ -309,12 +312,12 @@ class ReportPagination(PageNumberPagination):
max_page_size = 100 max_page_size = 100
class UserClassRoomStats(viewsets.ReadOnlyModelViewSet): class UserStats(viewsets.ReadOnlyModelViewSet):
model = User model = User
queryset = User.objects.all().order_by('name') queryset = User.objects.all().order_by('name')
serializer_class = UserClassRoomStatsSerializer serializer_class = UserStatsSerializer
filter_fields = ('group',) filterset_fields = ['groups']
permission_classes = [IsAuthenticated] #permission_classes = [IsAuthenticated]
pagination_class = ReportPagination pagination_class = ReportPagination
filter_backends = [ filter_backends = [
filters.SearchFilter, filters.SearchFilter,
...@@ -329,26 +332,9 @@ class UserClassRoomStats(viewsets.ReadOnlyModelViewSet): ...@@ -329,26 +332,9 @@ class UserClassRoomStats(viewsets.ReadOnlyModelViewSet):
] ]
def get_queryset(self): def get_queryset(self):
## MANTER O FILTER ABAIXO? queryset = super().get_queryset()
queryset = super(User, self).get_queryset().filter(is_active=True) group_id = self.request.query_params.get('group')
user = self.request.user
classroom_id = self.request.query_params.get('classroom') queryset = queryset.filter(groups__id=group_id)
role = None
try:
role = self.request.user.teaching_courses.get(course__id=course_id).role
except ObjectDoesNotExist:
pass
classes_id = self.request.query_params.getlist('classes')
# class passed as get paremeter
classes = Class.objects.filter(course=course_id)
if classes_id:
classes = classes.filter(id__in=classes_id)
queryset = queryset.filter(user__classes__in=classes)
if not ((role and role == 'coordinator') or self.request.user.is_staff or self.request.user.is_superuser):
# if user is not coordinator or admin, only show his classes
classes = classes.filter(assistants=user)
queryset = queryset.filter(user__classes__in=classes)
return queryset.select_related('user', 'course')
return queryset
...@@ -24,7 +24,7 @@ from courses.reports.views import ( ...@@ -24,7 +24,7 @@ from courses.reports.views import (
SummaryViewSet, SummaryViewSet,
UsersByGroupViewSet, UsersByGroupViewSet,
UsersByClassViewSet, UsersByClassViewSet,
UserClassRoomStats UserStats
) )
from courses.classroom.views import ( from courses.classroom.views import (
BasicClassroomViewSet, BasicClassroomViewSet,
...@@ -55,7 +55,7 @@ router.register(r'class_basic', BasicClassViewSet) ...@@ -55,7 +55,7 @@ router.register(r'class_basic', BasicClassViewSet)
router.register(r'summary', SummaryViewSet, base_name='summary') 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-group', UsersByGroupViewSet, base_name='users-by-group')
router.register(r'users-by-class', UsersByClassViewSet, base_name='users-by-class') router.register(r'users-by-class', UsersByClassViewSet, base_name='users-by-class')
router.register(r'classroom-reports', UserClassRoomStats, base_name='classroom-reports') router.register(r'user-reports', UserStats, base_name='user-reports')
# router.register(r'course_carousel', views.CarouselCourseView, base_name='course_carousel') # 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