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

from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from rest_framework_json_api.views import PreloadIncludesMixin

from timed.permissions import IsAuthenticated, IsReadOnly, IsReviewer, IsSuperUser
from timed.projects import filters, models, serializers


class CustomerViewSet(ReadOnlyModelViewSet):
    """Customer view set."""

    serializer_class = serializers.CustomerSerializer
    filterset_class = filters.CustomerFilterSet
    ordering = "name"

    def get_queryset(self):
        """Prefetch related data.

        :return: The customers
        :rtype:  QuerySet
        """
        return models.Customer.objects.prefetch_related("projects")


class BillingTypeViewSet(ReadOnlyModelViewSet):
    serializer_class = serializers.BillingTypeSerializer
    ordering = "name"

    def get_queryset(self):
        return models.BillingType.objects.all()


class CostCenterViewSet(ReadOnlyModelViewSet):
    serializer_class = serializers.CostCenterSerializer
    ordering = "name"

    def get_queryset(self):
        return models.CostCenter.objects.all()


class ProjectViewSet(PreloadIncludesMixin, ReadOnlyModelViewSet):
    """Project view set."""

    serializer_class = serializers.ProjectSerializer
    filterset_class = filters.ProjectFilterSet
    ordering_fields = ("customer__name", "name")
    ordering = "name"
    queryset = models.Project.objects.all()

    prefetch_for_includes = {
        "__all__": ["reviewers"],
        "reviewers": ["reviewers__supervisors"],
    }

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.select_related("customer", "billing_type", "cost_center")


class TaskViewSet(ModelViewSet):
    """Task view set."""

    serializer_class = serializers.TaskSerializer
    filterset_class = filters.TaskFilterSet<commit_msg>feat: get assigned only objects for external<commit_after>"""Viewsets for the projects app."""

from datetime import date

from rest_framework.viewsets import ModelViewSet, ReadOnlyModelViewSet
from rest_framework_json_api.views import PreloadIncludesMixin

from timed.permissions import IsAuthenticated, IsReadOnly, IsReviewer, IsSuperUser
from timed.projects import filters, models, serializers


class CustomerViewSet(ReadOnlyModelViewSet):
    """Customer view set."""

    serializer_class = serializers.CustomerSerializer
    filterset_class = filters.CustomerFilterSet
    ordering = "name"

    def get_queryset(self):
        """Prefetch related data.

        If an employee is external, get only assigned customers.

        :return: The customers
        :rtype:  QuerySet
        """
        all_user_employments = self.request.user.employments.all()
        current_date = date.today()

        for employment in all_user_employments:
            if not employment.is_external:
                return models.Customer.objects.prefetch_related("projects")
            if not employment.end_date:
                return models.Customer.objects.filter(assignees=self.request.user)
            elif (
                employment.start_date <= current_date
                and employment.end_date >= current_date
            ):
                return models.Customer.objects.filter(assignees=self.request.user)


class BillingTypeViewSet(ReadOnlyModelViewSet):
    serializer_class = serializers.BillingTypeSerializer
    ordering = "name"

    def get_queryset(self):
        return models.BillingType.objects.all()


class CostCenterViewSet(ReadOnlyModelViewSet):
    serializer_class = serializers.CostCenterSerializer
    ordering = "name"

    def get_queryset(self):
        return models.CostCenter.objects.all()


class ProjectViewSet(PreloadIncludesMixin, ReadOnlyModelViewSet):
    """Project view set."""

    serializer_class = serializers.ProjectSerializer
    filterset_class = filters.ProjectFilterSet
    ordering_fields = ("customer__name", "name")
    ordering = "name"
    queryset = models.Project.objects.all()

    prefetch_for_includes = {
        "__all__": ["reviewers"],
        "reviewers": ["reviewers__supervisors"],
    }

    def get_queryset(self):
        """Get only assigned projects, if an employee is external."""
        all_user_employments = self.request.user.employments.all()
        current_date = date.today()

        for employment in all_user_employments:
            if not employment.is_external:
                queryset = super().get_queryset()
                return queryset.select_related(
                    "customer", "billing_type", "cost_center"
                )
            if not employment.end_date:
                queryset = models.Project.objects.filter(assignees=self.request.user)
                return queryset.select_related("customer")
            elif (
                employment.start_date <= current_date
                and employment.end_date >= current_date
            ):
                queryset = models.Project.objects.filter(assignees=self.request.user)
                return queryset.select_related("customer")


class TaskViewSet(ModelViewSet):
    """Task view set."""

    serializer_class = serializers.TaskSerializer
    filterset_class = filters.TaskFilterSet
{'fraction_non_alphanumeric': 0.08669969512195122, 'fraction_numerical': 0.0, 'mean_word_length': 3.2228479485116655, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, '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': '3272762', 'n_tokens_mistral': 1431, 'n_tokens_neox': 1370, 'n_words': 298}