<commit_before># -*- coding: utf-8 -*-
from rest_framework import viewsets
from rest_framework.response import Response
from braces.views import LoginRequiredMixin
from core.models import Course, CourseStudent, Class
from django.core.exceptions import ObjectDoesNotExist
from reports.serializer import UserCourseStatsSerializer, CourseStats, LessonUserStats


class UserCourseStats(LoginRequiredMixin, viewsets.ReadOnlyModelViewSet):
    model = CourseStudent
    queryset = CourseStudent.objects.all()
    serializer_class = UserCourseStatsSerializer
    filter_fields = ('course',)

    def get_queryset(self):
        queryset = super(UserCourseStats, self).get_queryset()
        user = self.request.user
        course_id = self.request.query_params.get('course')
        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 (role and role == 'coordinator') or self.request.user.is_staff or self.request.user.is_superuser:
            return queryset
        else:
            # if user is not coordinator or admin, only show his classes
            classes = classes.filter(assistant=user)
            return queryset.filter(user__classes__in=classes)


class UserCourseLessonsStats(LoginRequiredMixin, viewsets.ReadOnlyModelViewSet):
    model = CourseStudent
    queryset = CourseStudent.objects.all()
    serializer_class = LessonUserStats
    filter_fields = ('course', 'user',)
    lookup_field = 'course'


class CourseStatsByLessonViewSet(LoginRequiredMixin, viewsets.ReadOnlyModelViewSet):
    model = Course
    queryset = Course.objects.all()
    serializer_class = CourseStats

    def retrieve(self, request, *args, **kwargs):
        self.object = self.get_object()

        course_id = self.kwargs.get('pk')
        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)
        # if user is not coordinator or admin, only show his classes
        if not (role and (role == 'coordinator') and self.request.user.is_staff and self.request.user.is_superuser):
            classes = classes.filter(assistant=self.request.user)

        self.object.classes = classes<commit_msg>Fixed issue that allowed only admin and coordinators to see classes reports<commit_after># -*- coding: utf-8 -*-
from rest_framework import viewsets
from rest_framework.response import Response
from braces.views import LoginRequiredMixin
from core.models import Course, CourseStudent, Class
from django.core.exceptions import ObjectDoesNotExist
from reports.serializer import UserCourseStatsSerializer, CourseStats, LessonUserStats


class UserCourseStats(LoginRequiredMixin, viewsets.ReadOnlyModelViewSet):
    model = CourseStudent
    queryset = CourseStudent.objects.all()
    serializer_class = UserCourseStatsSerializer
    filter_fields = ('course',)

    def get_queryset(self):
        queryset = super(UserCourseStats, self).get_queryset()
        user = self.request.user
        course_id = self.request.query_params.get('course')
        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 (role and role == 'coordinator') or self.request.user.is_staff or self.request.user.is_superuser:
            return queryset
        else:
            # if user is not coordinator or admin, only show his classes
            classes = classes.filter(assistants=user)
            return queryset.filter(user__classes__in=classes)


class UserCourseLessonsStats(LoginRequiredMixin, viewsets.ReadOnlyModelViewSet):
    model = CourseStudent
    queryset = CourseStudent.objects.all()
    serializer_class = LessonUserStats
    filter_fields = ('course', 'user',)
    lookup_field = 'course'


class CourseStatsByLessonViewSet(LoginRequiredMixin, viewsets.ReadOnlyModelViewSet):
    model = Course
    queryset = Course.objects.all()
    serializer_class = CourseStats

    def retrieve(self, request, *args, **kwargs):
        self.object = self.get_object()

        course_id = self.kwargs.get('pk')
        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)
        # if user is not coordinator or admin, only show his classes
        if not (role and (role == 'coordinator') and self.request.user.is_staff and self.request.user.is_superuser):
            classes = classes.filter(assistants=self.request.user)

        self.object.classes = classes
{'fraction_non_alphanumeric': 0.08298898071625345, 'fraction_numerical': 0.0003443526170798898, 'mean_word_length': 3.155221745350501, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5140647', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1577, 'n_words': 368}