<commit_before># -*- coding: utf-8 -*-
#
# defivelo-intranet -- Outil métier pour la gestion du Défi Vélo
# Copyright (C) 2015, 2016 <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals

from dal_select2.views import Select2QuerySetView
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from rolepermissions.checkers import has_permission

from apps.challenge import MAX_MONO1_PER_QUALI

from ..models import FORMATION_KEYS, FORMATION_M2, USERSTATUS_DELETED
from .mixins import ProfileMixin
from .standard import UserProfileFilterSet


class PersonAutocomplete(ProfileMixin, Select2QuerySetView):
    model = get_user_model()
    required_permission = 'user_view_list'
    choices = None
    widget_attrs = {'data-widget-maximum-values': 1, }

    def get_result_label(self, choice):
        return choice.get_full_name()

    def get_queryset(self):
        if has_permission(self.request.user, self.required_permission):
            # Remove self.q to bypass Select2QuerySetView' wrong handling
            q = self.q if self.q else None
            del(self.q)
            qs = super(PersonAutocomplete, self).get_queryset()
            # Only non-deleted
            qs = qs.exclude(profile__status=USERSTATUS_DELETED)
            if q:
                upfs = UserProfileFilterSet()
                qs = upfs.filter_wide(qs, '', q)
            return qs
        else:
            raise PermissionDenied


class AllPersons(PersonAutocomplete):
    pass


class PersonsRelevantForSessions(PersonAutocomplete):
    def get_queryset(self):
        qs = super(PersonsRelevantForSessions, self).get_queryset()
        return qs.filter(
                Q(profile__formation__in=FORMATION_KEYS) |
                Q(profile__actor_for__isnull=False)
            )


class Helpers(PersonAutocomplete):
    widget_attrs = {'data-widget-maximum-values': MAX_MONO1_PER_QUALI, }

    def get_queryset(self):
        qs = super(Helpers, self).get_queryset()
        return qs.filter(profile__formation__in=FORMATION_KEYS)

    def get_result_label(self, choice):
        return "{name} {icon}".format(
            name=choice.get_full_name(),
            icon=choice.profile.formation_icon())


class Leaders(PersonAutocomplete):
    def get_queryset(self):
        qs = super(Leaders, self).get_queryset()
        return qs.filter(profile__formation=FORMATION_M2)


class Actors(PersonAutocomplete):
    def get_queryset(self):
        qs = super(Actors, self).get_queryset()
        return qs.exclude(
            profile__actor_for__isnull=True
        )<commit_msg>Garantit que les intervenants n'apparaîssent qu'une fois<commit_after># -*- coding: utf-8 -*-
#
# defivelo-intranet -- Outil métier pour la gestion du Défi Vélo
# Copyright (C) 2015, 2016 <NAME> <<EMAIL>>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals

from dal_select2.views import Select2QuerySetView
from django.contrib.auth import get_user_model
from django.core.exceptions import PermissionDenied
from django.db.models import Q
from rolepermissions.checkers import has_permission

from apps.challenge import MAX_MONO1_PER_QUALI

from ..models import FORMATION_KEYS, FORMATION_M2, USERSTATUS_DELETED
from .mixins import ProfileMixin
from .standard import UserProfileFilterSet


class PersonAutocomplete(ProfileMixin, Select2QuerySetView):
    model = get_user_model()
    required_permission = 'user_view_list'
    choices = None
    widget_attrs = {'data-widget-maximum-values': 1, }

    def get_result_label(self, choice):
        return choice.get_full_name()

    def get_queryset(self):
        if has_permission(self.request.user, self.required_permission):
            # Remove self.q to bypass Select2QuerySetView' wrong handling
            q = self.q if self.q else None
            del(self.q)
            qs = super(PersonAutocomplete, self).get_queryset()
            # Only non-deleted
            qs = qs.exclude(profile__status=USERSTATUS_DELETED)
            if q:
                upfs = UserProfileFilterSet()
                qs = upfs.filter_wide(qs, '', q)
            return qs
        else:
            raise PermissionDenied


class AllPersons(PersonAutocomplete):
    pass


class PersonsRelevantForSessions(PersonAutocomplete):
    def get_queryset(self):
        qs = super(PersonsRelevantForSessions, self).get_queryset()
        return qs.filter(
                Q(profile__formation__in=FORMATION_KEYS) |
                Q(profile__actor_for__isnull=False)
            ).distinct()


class Helpers(PersonAutocomplete):
    widget_attrs = {'data-widget-maximum-values': MAX_MONO1_PER_QUALI, }

    def get_queryset(self):
        qs = super(Helpers, self).get_queryset()
        return qs.filter(profile__formation__in=FORMATION_KEYS)

    def get_result_label(self, choice):
        return "{name} {icon}".format(
            name=choice.get_full_name(),
            icon=choice.profile.formation_icon())


class Leaders(PersonAutocomplete):
    def get_queryset(self):
        qs = super(Leaders, self).get_queryset()
        return qs.filter(profile__formation=FORMATION_M2)


class Actors(PersonAutocomplete):
    def get_queryset(self):
        qs = super(Actors, self).get_queryset()
        return qs.exclude(
            profile__actor_for__isnull=True
        ).distinct()
{'fraction_non_alphanumeric': 0.07984391415278404, 'fraction_numerical': 0.0057031367251988594, 'mean_word_length': 3.302130406714009, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5978096', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1902, 'n_words': 548}