<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

import operator
from collections import OrderedDict
from functools import reduce

from django.contrib.auth import get_user_model
from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import HttpResponseRedirect
from django.template.defaultfilters import date, time
from django.utils.translation import ugettext as u, ugettext_lazy as _
from django.views.generic.dates import YearArchiveView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from rolepermissions.mixins import HasPermissionsMixin
from rolepermissions.verifications import has_permission
from tablib import Dataset

from apps.common.views import ExportMixin
from apps.user.models import FORMATION_M2
from apps.user.views import ActorsList, HelpersList
from defivelo.roles import user_cantons
from defivelo.views import MenuView

from .. import AVAILABILITY_FIELDKEY, MAX_MONO1_PER_QUALI, STAFF_FIELDKEY
from ..forms import SeasonAvailabilityForm, SeasonForm, SeasonNewHelperAvailabilityForm, SeasonStaffChoiceForm
from ..models import HelperSessionAvailability, Season
from ..models.qualification import CATEGORY_CHOICE_A, CATEGORY_CHOICE_B, CATEGORY_CHOICE_C
from .mixins import CantonSeasonFormMixin

EXPORT_NAMETEL = u('{name} - {tel}')


class SeasonMixin(CantonSeasonFormMixin, MenuView):
    required_permission = 'challenge_season_crud'
    model = Season
    context_object_name = 'season'
    form_class = SeasonForm
    raise_without_cantons = True

    def get_context_data(self, **kwargs):
        context = super(SeasonMixin, self).get_context_data(**kwargs)
        # Add our menu_category context
        context['menu_category'] = 'season'
        context['season'] = self.season
        return context

    def get_queryset(self):
        if self.model == Season:
            return self.request.user.profile.get_seasons(
                self.raise_without_cantons
            )

        qs = super(SeasonMixin, self).get_queryset()<commit_msg>Ne permet l'édition et la création de saisons que dans les cantons permis<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

import operator
from collections import OrderedDict
from functools import reduce

from django.contrib.auth import get_user_model
from django.contrib.messages.views import SuccessMessageMixin
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse_lazy
from django.db.models import Q
from django.http import HttpResponseRedirect
from django.template.defaultfilters import date, time
from django.utils.translation import ugettext as u, ugettext_lazy as _
from django.views.generic.dates import YearArchiveView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView, DeleteView, UpdateView
from rolepermissions.mixins import HasPermissionsMixin
from rolepermissions.verifications import has_permission
from tablib import Dataset

from apps.common import DV_STATES
from apps.common.views import ExportMixin
from apps.user.models import FORMATION_M2
from apps.user.views import ActorsList, HelpersList
from defivelo.roles import user_cantons
from defivelo.views import MenuView

from .. import AVAILABILITY_FIELDKEY, MAX_MONO1_PER_QUALI, STAFF_FIELDKEY
from ..forms import SeasonAvailabilityForm, SeasonForm, SeasonNewHelperAvailabilityForm, SeasonStaffChoiceForm
from ..models import HelperSessionAvailability, Season
from ..models.qualification import CATEGORY_CHOICE_A, CATEGORY_CHOICE_B, CATEGORY_CHOICE_C
from .mixins import CantonSeasonFormMixin

EXPORT_NAMETEL = u('{name} - {tel}')


class SeasonMixin(CantonSeasonFormMixin, MenuView):
    required_permission = 'challenge_season_crud'
    model = Season
    context_object_name = 'season'
    form_class = SeasonForm
    raise_without_cantons = True

    def get_context_data(self, **kwargs):
        context = super(SeasonMixin, self).get_context_data(**kwargs)
        # Add our menu_category context
        context['menu_category'] = 'season'
        context['season'] = self.season
        return context

    def get_form_kwargs(self):
        form_kwargs = \
            super(SeasonMixin, self).get_form_kwargs()
        if has_permission(self.request.user, 'cantons_all'):
            form_kwargs['cantons'] = DV_STATES
        else:
            # Ne permet que l'édition et la création de saisons pour les cantons gérés
            form_kwargs['cantons'] = self.request.user.managedstates.all().values_list('canton', flat=True)
        return form_kwargs

    def get_queryset(self):
        if self.model == Season:
            return self.request.user.profile.get_seasons(
                self.raise_without_cantons
            )

        qs = super(SeasonMixin, self).get_queryset()
{'fraction_non_alphanumeric': 0.06630086877000457, 'fraction_numerical': 0.003657978966620942, 'mean_word_length': 4.414191419141914, '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': '1530086', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1817, 'n_words': 620}