<commit_before>"""Viewsets for the tracking app."""

import django_excel
from django.db import transaction
from django.db.models import Q
from django.http import HttpResponseBadRequest
from django.utils.translation import ugettext_lazy as _
from rest_condition import C
from rest_framework import exceptions, status
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from timed.permissions import (IsAuthenticated, IsDeleteOnly, IsOwner,
                               IsReadOnly, IsReviewer, IsSuperUser,
                               IsSupervisor, IsUnverified)
from timed.serializers import AggregateObject
from timed.tracking import filters, models, serializers


class ActivityViewSet(ModelViewSet):
    """Activity view set."""

    serializer_class = serializers.ActivitySerializer
    filter_class     = filters.ActivityFilterSet

    def get_queryset(self):
        """Filter the queryset by the user of the request.

        :return: The filtered activities
        :rtype:  QuerySet
        """
        return models.Activity.objects.prefetch_related(
            'blocks'
        ).select_related(
            'task',
            'user',
            'task__project',
            'task__project__customer'
        ).filter(
            user=self.request.user
        )


class ActivityBlockViewSet(ModelViewSet):
    """Activity view set."""

    serializer_class = serializers.ActivityBlockSerializer
    filter_class     = filters.ActivityBlockFilterSet

    def get_queryset(self):
        """Filter the queryset by the user of the request.

        :return: The filtered activity blocks
        :rtype:  QuerySet
        """
        return models.ActivityBlock.objects.select_related(
            'activity'
        ).filter(
            activity__user=self.request.user
        )


class AttendanceViewSet(ModelViewSet):
    """Attendance view set."""

    serializer_class = serializers.AttendanceSerializer
    filter_class     = filters.AttendanceFilterSet

    def get_queryset(self):
        """Filter the queryset by the user of the request.

        :return: The filtered attendances
        :rtype:  QuerySet
        """
        return models.Attendance.objects.select_related(
            'user'
        ).filter(
            user=self.request.user
        )


class ReportViewSet(ModelViewSet):
    """Report view set."""

    serializer_class = serializers.ReportSerializer
    filter_class     = filters.ReportFilterSet
    permission_classes = [
        # superuser may edit all reports but not delete
        C(IsSuperUser) & ~C(IsDeleteOnly) |
        # reviewer and supervisor may change unverified reports
        # but not delete them
        (C(IsReviewer) | C(IsSupervisor)) &
        C(IsUnverified) & ~C(IsDeleteOnly) |
        # owner may only change its own unverified reports
        C(IsAuthenticated) & C(IsOwner) & C(IsUnverified) |
        # all authenticated users may read all reports
        C(IsAuthenticated) & C(IsReadOnly)
    ]
    ordering = ('date', 'id')
    ordering_fields = (
        'date',
        'duration',
        'task__project__customer__name',
        'task__project__name',
        'task__name',
        'user__username',
        'comment',
        'verified_by__username',
        'review',
        'not_billable'
    )
<commit_msg>Add missing id filter<commit_after>"""Viewsets for the tracking app."""

import django_excel
from django.db import transaction
from django.db.models import Q
from django.http import HttpResponseBadRequest
from django.utils.translation import ugettext_lazy as _
from rest_condition import C
from rest_framework import exceptions, status
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet

from timed.permissions import (IsAuthenticated, IsDeleteOnly, IsOwner,
                               IsReadOnly, IsReviewer, IsSuperUser,
                               IsSupervisor, IsUnverified)
from timed.serializers import AggregateObject
from timed.tracking import filters, models, serializers


class ActivityViewSet(ModelViewSet):
    """Activity view set."""

    serializer_class = serializers.ActivitySerializer
    filter_class     = filters.ActivityFilterSet

    def get_queryset(self):
        """Filter the queryset by the user of the request.

        :return: The filtered activities
        :rtype:  QuerySet
        """
        return models.Activity.objects.prefetch_related(
            'blocks'
        ).select_related(
            'task',
            'user',
            'task__project',
            'task__project__customer'
        ).filter(
            user=self.request.user
        )


class ActivityBlockViewSet(ModelViewSet):
    """Activity view set."""

    serializer_class = serializers.ActivityBlockSerializer
    filter_class     = filters.ActivityBlockFilterSet

    def get_queryset(self):
        """Filter the queryset by the user of the request.

        :return: The filtered activity blocks
        :rtype:  QuerySet
        """
        return models.ActivityBlock.objects.select_related(
            'activity'
        ).filter(
            activity__user=self.request.user
        )


class AttendanceViewSet(ModelViewSet):
    """Attendance view set."""

    serializer_class = serializers.AttendanceSerializer
    filter_class     = filters.AttendanceFilterSet

    def get_queryset(self):
        """Filter the queryset by the user of the request.

        :return: The filtered attendances
        :rtype:  QuerySet
        """
        return models.Attendance.objects.select_related(
            'user'
        ).filter(
            user=self.request.user
        )


class ReportViewSet(ModelViewSet):
    """Report view set."""

    serializer_class = serializers.ReportSerializer
    filter_class     = filters.ReportFilterSet
    permission_classes = [
        # superuser may edit all reports but not delete
        C(IsSuperUser) & ~C(IsDeleteOnly) |
        # reviewer and supervisor may change unverified reports
        # but not delete them
        (C(IsReviewer) | C(IsSupervisor)) &
        C(IsUnverified) & ~C(IsDeleteOnly) |
        # owner may only change its own unverified reports
        C(IsAuthenticated) & C(IsOwner) & C(IsUnverified) |
        # all authenticated users may read all reports
        C(IsAuthenticated) & C(IsReadOnly)
    ]
    ordering = ('date', 'id')
    ordering_fields = (
        'id',
        'date',
        'duration',
        'task__project__customer__name',
        'task__project__name',
        'task__name',
        'user__username',
        'comment',
        'verified_by__username',
        'review',
        'not_billable'
    )

{'fraction_non_alphanumeric': 0.07939536248899325, 'fraction_numerical': 0.0, 'mean_word_length': 2.5439417576703067, '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': '777987', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1786, 'n_words': 472}