<commit_before># -*- coding: utf-8 -*-

import uuid

from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404

from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.decorators import detail_route
from rest_framework import viewsets
from rest_framework import status

from djmail.template_mail import MagicMailBuilder

from taiga.base import filters
from taiga.base import exceptions as exc
from taiga.base.permissions import has_project_perm
from taiga.base.api import ModelCrudViewSet, ModelListViewSet, RetrieveModelMixin
from taiga.base.domains import get_active_domain
from taiga.base.users.models import Role
from taiga.base.notifications.api import NotificationSenderMixin
from taiga.projects.aggregates.tags import get_all_tags

from . import serializers
from . import models
from . import permissions
from . import services

from .aggregates import stats
from .aggregates import filters as filters_aggr


class ProjectAdminViewSet(ModelCrudViewSet):
    model = models.Project
    serializer_class = serializers.ProjectDetailSerializer
    list_serializer_class = serializers.ProjectSerializer
    permission_classes = (IsAuthenticated, permissions.ProjectAdminPermission)

    def get_queryset(self):
        domain = get_active_domain()
        return domain.projects.all()

    def pre_save(self, obj):
        obj.owner = self.request.user

        # FIXME

        # Assign domain only if it current
        # value is None
        if not obj.domain:
            obj.domain = self.request.domain

        super().pre_save(obj)


class ProjectViewSet(ModelCrudViewSet):
    model = models.Project
    serializer_class = serializers.ProjectDetailSerializer
    list_serializer_class = serializers.ProjectSerializer
    permission_classes = (IsAuthenticated, permissions.ProjectPermission)

    @detail_route(methods=['get'])
    def stats(self, request, pk=None):
        project = self.get_object()
        return Response(stats.get_stats_for_project(project))

    @detail_route(methods=['get'])
    def issues_stats(self, request, pk=None):
        project = self.get_object()
        return Response(stats.get_stats_for_project_issues(project))

    @detail_route(methods=['get'])
    def issue_filters_data(self, request, pk=None):
        project = self.get_object()
        return Response(filters_aggr.get_issues_filters_data(project))

    @detail_route(methods=['get'])
    def tags(self, request, pk=None):
        project = self.get_object()
        return Response(get_all_tags(project))

    def get_queryset(self):
        qs = super().get_queryset()
        qs = qs.filter(Q(owner=self.request.user) |
                       Q(members=self.request.user))
        return qs.distinct()

    def pre_save(self, obj):
        obj.owner = self.request.user

        # FIXME

        # Assign domain only if it current<commit_msg>Filtering the projects by active domain<commit_after># -*- coding: utf-8 -*-

import uuid

from django.db.models import Q
from django.utils.translation import ugettext_lazy as _
from django.shortcuts import get_object_or_404

from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.decorators import detail_route
from rest_framework import viewsets
from rest_framework import status

from djmail.template_mail import MagicMailBuilder

from taiga.base import filters
from taiga.base import exceptions as exc
from taiga.base.permissions import has_project_perm
from taiga.base.api import ModelCrudViewSet, ModelListViewSet, RetrieveModelMixin
from taiga.base.domains import get_active_domain
from taiga.base.users.models import Role
from taiga.base.notifications.api import NotificationSenderMixin
from taiga.projects.aggregates.tags import get_all_tags

from . import serializers
from . import models
from . import permissions
from . import services

from .aggregates import stats
from .aggregates import filters as filters_aggr


class ProjectAdminViewSet(ModelCrudViewSet):
    model = models.Project
    serializer_class = serializers.ProjectDetailSerializer
    list_serializer_class = serializers.ProjectSerializer
    permission_classes = (IsAuthenticated, permissions.ProjectAdminPermission)

    def get_queryset(self):
        domain = get_active_domain()
        return domain.projects.all()

    def pre_save(self, obj):
        obj.owner = self.request.user

        # FIXME

        # Assign domain only if it current
        # value is None
        if not obj.domain:
            obj.domain = self.request.domain

        super().pre_save(obj)


class ProjectViewSet(ModelCrudViewSet):
    model = models.Project
    serializer_class = serializers.ProjectDetailSerializer
    list_serializer_class = serializers.ProjectSerializer
    permission_classes = (IsAuthenticated, permissions.ProjectPermission)

    @detail_route(methods=['get'])
    def stats(self, request, pk=None):
        project = self.get_object()
        return Response(stats.get_stats_for_project(project))

    @detail_route(methods=['get'])
    def issues_stats(self, request, pk=None):
        project = self.get_object()
        return Response(stats.get_stats_for_project_issues(project))

    @detail_route(methods=['get'])
    def issue_filters_data(self, request, pk=None):
        project = self.get_object()
        return Response(filters_aggr.get_issues_filters_data(project))

    @detail_route(methods=['get'])
    def tags(self, request, pk=None):
        project = self.get_object()
        return Response(get_all_tags(project))

    def get_queryset(self):
        qs = super().get_queryset()
        qs = qs.filter(Q(owner=self.request.user) |
                       Q(members=self.request.user)).filter(domain=get_active_domain())
        return qs.distinct()

    def pre_save(self, obj):
        obj.owner = self.request.user

        # FIXME

        # Assign domain only if it current
{'fraction_non_alphanumeric': 0.07750932382033404, 'fraction_numerical': 0.0012972271769093563, 'mean_word_length': 3.875889328063241, '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': '2092680', 'n_tokens_mistral': 1761, 'n_tokens_neox': 1712, 'n_words': 426}