<commit_before>from datetime import date

from django.utils.translation import ugettext_lazy as _
from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup, \
    modeladmin_register

from involvement.models import Team, Role, Position, Application
from involvement.rules import is_admin
from utils.permissions import RulesPermissionHelper


class TeamAdmin(ModelAdmin):
    model = Team
    menu_icon = 'group'
    menu_order = 100
    list_display = ('name_en', 'name_sv', 'email')
    search_fields = ('name_en', 'name_sv', 'description_en', 'description_sv',
                     'leader_en', 'leader_sv', 'email')
    permission_helper_class = RulesPermissionHelper

    def get_queryset(self, request):
        if is_admin(request.user):
            return super(TeamAdmin, self).get_queryset(request)
        else:
            # TODO : Is this efficient?
            applications = Application.objects.filter(
                applicant=request.user,
                status='appointed',
                position__term_from__lte=date.today(),
                position__term_to__gte=date.today(),
                position__role__official=True,
                position__role__team_id__isnull=False,
            ).select_related('position__role__team')
            teams = []
            for i in applications:
                teams.append(i.position.role.team.id)
            qs = Team.objects.filter(id__in=teams)
            ordering = self.get_ordering(request)
            if ordering:
                qs = qs.order_by(*ordering)
            return qs


class RoleAdmin(ModelAdmin):
    model = Role
    menu_icon = 'user'
    menu_order = 200
    list_display = ('team', 'name_en', 'name_sv', 'archived')
    search_fields = ('name_en', 'name_sv', 'description_en',
                     'description_sv')
    permission_helper_class = RulesPermissionHelper

    def get_queryset(self, request):
        if is_admin(request.user):
            return super(RoleAdmin, self).get_queryset(request)
        else:
            # TODO : Is this efficient?
            applications = Application.objects.filter(
                applicant=request.user,
                status='appointed',
                position__term_from__lte=date.today(),
                position__term_to__gte=date.today(),
                position__role__official=True,
                position__role__team_id__isnull=False,
            ).select_related('position__role__team')
            teams = []
            for i in applications:
                teams.append(i.position.role.team)
            qs = Role.objects.filter(team__in=teams)
            ordering = self.get_ordering(request)
            if ordering:
                qs = qs.order_by(*ordering)
            return qs


class PositionAdmin(ModelAdmin):
    model = Position
    menu_icon = 'search'
    menu_order = 300
    list_display = ('role', 'appointments', 'term_from', 'term_to')
    search_fields = ('comments_en', 'comments_sv')
    permission_helper_class = RulesPermissionHelper

    def get_queryset(self, request):
        if is_admin(request.user):
            return super(PositionAdmin, self).get_queryset(request)
        else:
            # TODO : Is this efficient?
            applications = Application.objects.filter(
                applicant=request.user,
                status='appointed',
                position__term_from__lte=date.today(),
                position__term_to__gte=date.today(),
                position__role__official=True,
                position__role__team_id__isnull=False,
            ).select_related('position__role__team')
            teams = []
            for i in applications:
                teams.append(i.position.role.team)
            qs = Position.objects.filter(role__team__in=teams)
            ordering = self.get_ordering(request)
            if ordering:
                qs = qs.order_by(*ordering)
            return qs


class ApplicationAdmin(ModelAdmin):
    model = Application
    menu_icon = 'mail'
    menu_order = 400
    list_display = ('position', 'applicant', 'status')
    permission_helper_class = RulesPermissionHelper


class InvolvementAdminGroup(ModelAdminGroup):
    menu_label = _('Involvement')
    menu_icon = 'group'<commit_msg>Add filters to the the involvement administration<commit_after>from datetime import date

from django.contrib import admin
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from wagtail.contrib.modeladmin.options import ModelAdmin, ModelAdminGroup, \
    modeladmin_register

from involvement.models import Team, Role, Position, Application
from involvement.rules import is_admin
from utils.permissions import RulesPermissionHelper


class TeamAdmin(ModelAdmin):
    model = Team
    menu_icon = 'group'
    menu_order = 100
    list_display = ('name_en', 'name_sv', 'email')
    search_fields = ('name_en', 'name_sv', 'description_en', 'description_sv',
                     'leader_en', 'leader_sv', 'email')
    permission_helper_class = RulesPermissionHelper

    def get_queryset(self, request):
        if is_admin(request.user):
            return super(TeamAdmin, self).get_queryset(request)
        else:
            # TODO : Is this efficient?
            applications = Application.objects.filter(
                applicant=request.user,
                status='appointed',
                position__term_from__lte=date.today(),
                position__term_to__gte=date.today(),
                position__role__official=True,
                position__role__team_id__isnull=False,
            ).select_related('position__role__team')
            teams = []
            for i in applications:
                teams.append(i.position.role.team.id)
            qs = Team.objects.filter(id__in=teams)
            ordering = self.get_ordering(request)
            if ordering:
                qs = qs.order_by(*ordering)
            return qs


class RoleAdmin(ModelAdmin):
    model = Role
    menu_icon = 'user'
    menu_order = 200
    list_display = ('team', 'name_en', 'name_sv', 'archived')
    search_fields = ('name_en', 'name_sv', 'description_en',
                     'description_sv')
    # TODO: Default to archived==False, might be in
    # https://code.djangoproject.com/ticket/8851#no1
    list_filter = ('team', 'archived')
    permission_helper_class = RulesPermissionHelper

    def get_queryset(self, request):
        if is_admin(request.user):
            return super(RoleAdmin, self).get_queryset(request)
        else:
            # TODO : Is this efficient?
            applications = Application.objects.filter(
                applicant=request.user,
                status='appointed',
                position__term_from__lte=date.today(),
                position__term_to__gte=date.today(),
                position__role__official=True,
                position__role__team_id__isnull=False,
            ).select_related('position__role__team')
            teams = []
            for i in applications:
                teams.append(i.position.role.team)
            qs = Role.objects.filter(team__in=teams)
            ordering = self.get_ordering(request)
            if ordering:
                qs = qs.order_by(*ordering)
            return qs


class PositionYearFilter(admin.SimpleListFilter):
    title = _('year')

    parameter_name = 'year'
    parameter = 'year'

    def lookups(self, request, model_admin):
        return (
            ('this', _('This Year')),
            ('last', _('Last Year')),
            ('before', _('Before')),
        )

    def queryset(self, request, queryset):
        now = timezone.now()
        # When time zone support is enabled, convert "now" to the user's time
        # zone so Django's definition of "Today" matches what the user expects.
        if timezone.is_aware(now):
            now = timezone.localtime(now)

        this_year = now.replace(
            month=1, day=1, hour=0, minute=0, second=0, microsecond=0
        )
        next_year = date(year=this_year.year + 1, month=1, day=1)
        last_year = date(year=this_year.year - 1, month=1, day=1)

        if self.value() == 'this':
            return queryset.filter(
                term_from__gte=this_year,
                term_from__lt=next_year
            )
        elif self.value() == 'last':
            return queryset.filter(
                term_from__gte=last_year,
                term_from__lt=this_year
            )
        elif self.value() == 'before':
            return queryset.filter(term_from__lt=last_year)


class PositionAdmin(ModelAdmin):
    model = Position
    menu_icon = 'search'
    menu_order = 300
    list_display = ('role', 'appointments', 'term_from', 'term_to')
    search_fields = ('comments_en', 'comments_sv')
    list_filter = ('role__team', PositionYearFilter)
    permission_helper_class = RulesPermissionHelper

    def get_queryset(self, request):
        if is_admin(request.user):
            return super(PositionAdmin, self).get_queryset(request)
        else:
            # TODO : Is this efficient?
            applications = Application.objects.filter(
                applicant=request.user,
                status='appointed',
                position__term_from__lte=date.today(),
                position__term_to__gte=date.today(),
                position__role__official=True,
                position__role__team_id__isnull=False,
            ).select_related('position__role__team')
            teams = []
            for i in applications:
                teams.append(i.position.role.team)
            qs = Position.objects.filter(role__team__in=teams)
            ordering = self.get_ordering(request)
            if ordering:
                qs = qs.order_by(*ordering)
            return qs


class ApplicationAdmin(ModelAdmin):
    model = Application
    menu_icon = 'mail'
    menu_order = 400
    list_display = ('position', 'applicant', 'status')
    list_filter = ('position__role__team', 'status')
    permission_helper_class = RulesPermissionHelper


class InvolvementAdminGroup(ModelAdminGroup):
    menu_label = _('Involvement')
    menu_icon = 'group'
{'fraction_non_alphanumeric': 0.09424904477319486, 'fraction_numerical': 0.0040168511805623594, 'mean_word_length': 2.2929032258064517, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '7133531', 'n_tokens_mistral': 2906, 'n_tokens_neox': 2766, 'n_words': 569}