<commit_before>from django.contrib import messages
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseRedirect
from django.utils.translation import ugettext as _
from django.views.generic import DetailView, View
from django_filters.views import FilterView

from TWLight.applications.models import Application
from TWLight.graphs.helpers import (get_median,
                                    get_application_status_data,
                                    get_data_count_by_month,
                                    get_users_by_partner_by_month)
from TWLight.view_mixins import CoordinatorsOnly

from .models import Partner, Stream


class PartnersFilterView(FilterView):
    model = Partner

    def get_queryset(self):
        # The ordering here is useful primarily to people familiar with the
        # English alphabet. :/
        if self.request.user.is_staff:
            messages.add_message(self.request, messages.INFO,
                # Translators: Staff members can see partners on the Browse page (https://wikipedialibrary.wmflabs.org/partners/) which are hidden from other users.
                _('Because you are a staff member, this page may include '
                    'Partners who are not yet available to all users.'))
            return Partner.even_not_available.order_by('company_name')
        else:
            return Partner.objects.order_by('company_name')



class PartnersDetailView(DetailView):
    model = Partner

    def get_context_data(self, **kwargs):
        context = super(PartnersDetailView, self).get_context_data(**kwargs)

        partner = self.get_object()

        if partner.status == Partner.NOT_AVAILABLE:
            # This should be guaranteed by get_queryset and the manager
            # definitions.
            assert self.request.user.is_staff
            # Translators: Staff members can view partner pages which are hidden from other users. This message appears on those specific partner resource pages.
            messages.add_message(self.request, messages.WARNING,
                _("This Partner is not available. You can see it because you "
                    "are a staff member, but it is not visible to non-staff "
                    "users."))

        context['total_apps'] = Application.objects.filter(
            partner=partner).count()

        context['total_apps_approved'] = Application.objects.filter(
            partner=partner, status=Application.APPROVED).count()

        context['total_apps_sent'] = Application.objects.filter(
            partner=partner, status=Application.SENT).count()

        context['total_apps_approved_or_sent'] = context['total_apps_approved'] + context['total_apps_sent']

        context['unique_users'] = User.objects.filter(
            editor__applications__partner=partner).distinct().count()

        context['unique_users_approved'] = User.objects.filter(
            editor__applications__partner=partner,
            editor__applications__status=Application.APPROVED).distinct().count()

        context['unique_users_sent'] = User.objects.filter(
            editor__applications__partner=partner,
            editor__applications__status=Application.SENT).distinct().count()

        context['unique_users_approved_or_sent'] = context['unique_users_approved'] + context['unique_users_sent']

        application_end_states = [Application.APPROVED, Application.NOT_APPROVED, Application.SENT]
        partner_app_time = Application.objects.filter(
            partner=partner, status__in=application_end_states).exclude(imported=True).values_list('days_open', flat=True)

        if len(partner_app_time) > 0:
            context['median_days'] = get_median(list(partner_app_time))
        else:
            context['median_days'] = None

        context['app_distribution_data'] = get_application_status_data(
                Application.objects.filter(partner=partner)
            )

        context['signups_time_data'] = get_data_count_by_month(
                Application.objects.filter(partner=partner)
            )

        context['approved_or_sent_signups_time_data'] = get_data_count_by_month(
                Application.objects.filter(
                    partner=partner,
                    status__in=(Application.APPROVED, Application.SENT)
                )
            )

        context['users_time_data'] = get_users_by_partner_by_month(partner)

        # Find out if current user has applications and change the Apply
        # button behaviour accordingly
        if self.request.user.is_authenticated() and not partner.bundle:
            sent_apps = Application.objects.filter(
                                        editor=self.request.user.editor,
                                        status=Application.SENT,
                                        partner=partner
                                     ).order_by('date_closed')
            open_apps = Application.objects.filter(
                                        editor=self.request.user.editor,
                                        status__in=(Application.PENDING, Application.QUESTION, Application.APPROVED),
                                        partner=partner
                                     )
            context['user_sent_apps'] = False
            context['user_open_apps'] = False
            if sent_apps.count() > 0:
                context['latest_sent_app_pk'] = sent_apps[0].pk
                context['user_sent_apps'] = True
            elif open_apps.count() > 0:
                context['user_open_apps'] = True
                if open_apps.count() > 1:
                    context['multiple_open_apps'] = True
                else:
                    context['multiple_open_apps'] = False
                    context['open_app_pk'] = open_apps[0].pk

        partner_streams = Stream.objects.filter(partner=partner)
        if partner_streams.count() > 0:
            context['stream_unique_accepted'] = {}
            for stream in Stream.objects.filter(partner=partner):
                stream_unique_accepted = User.objects.filter(
                                      editor__applications__partner=partner,
                                      editor__applications__status__in=(Application.APPROVED, Application.SENT),
                                      editor__applications__specific_stream=stream).distinct().count()
                context['stream_unique_accepted'][stream.name] = stream_unique_accepted
        else:
            context['stream_unique_accepted'] = None

        return context


    def get_queryset(self):<commit_msg>Added total approved counts per stream (T191407)<commit_after>from django.contrib import messages
from django.contrib.auth.models import User
from django.http import Http404, HttpResponseRedirect
from django.utils.translation import ugettext as _
from django.views.generic import DetailView, View
from django_filters.views import FilterView

from TWLight.applications.models import Application
from TWLight.graphs.helpers import (get_median,
                                    get_application_status_data,
                                    get_data_count_by_month,
                                    get_users_by_partner_by_month)
from TWLight.view_mixins import CoordinatorsOnly

from .models import Partner, Stream


class PartnersFilterView(FilterView):
    model = Partner

    def get_queryset(self):
        # The ordering here is useful primarily to people familiar with the
        # English alphabet. :/
        if self.request.user.is_staff:
            messages.add_message(self.request, messages.INFO,
                # Translators: Staff members can see partners on the Browse page (https://wikipedialibrary.wmflabs.org/partners/) which are hidden from other users.
                _('Because you are a staff member, this page may include '
                    'Partners who are not yet available to all users.'))
            return Partner.even_not_available.order_by('company_name')
        else:
            return Partner.objects.order_by('company_name')



class PartnersDetailView(DetailView):
    model = Partner

    def get_context_data(self, **kwargs):
        context = super(PartnersDetailView, self).get_context_data(**kwargs)

        partner = self.get_object()

        if partner.status == Partner.NOT_AVAILABLE:
            # This should be guaranteed by get_queryset and the manager
            # definitions.
            assert self.request.user.is_staff
            # Translators: Staff members can view partner pages which are hidden from other users. This message appears on those specific partner resource pages.
            messages.add_message(self.request, messages.WARNING,
                _("This Partner is not available. You can see it because you "
                    "are a staff member, but it is not visible to non-staff "
                    "users."))

        context['total_apps'] = Application.objects.filter(
            partner=partner).count()

        context['total_apps_approved'] = Application.objects.filter(
            partner=partner, status=Application.APPROVED).count()

        context['total_apps_sent'] = Application.objects.filter(
            partner=partner, status=Application.SENT).count()

        context['total_apps_approved_or_sent'] = context['total_apps_approved'] + context['total_apps_sent']

        context['unique_users'] = User.objects.filter(
            editor__applications__partner=partner).distinct().count()

        context['unique_users_approved'] = User.objects.filter(
            editor__applications__partner=partner,
            editor__applications__status=Application.APPROVED).distinct().count()

        context['unique_users_sent'] = User.objects.filter(
            editor__applications__partner=partner,
            editor__applications__status=Application.SENT).distinct().count()

        context['unique_users_approved_or_sent'] = context['unique_users_approved'] + context['unique_users_sent']

        application_end_states = [Application.APPROVED, Application.NOT_APPROVED, Application.SENT]
        partner_app_time = Application.objects.filter(
            partner=partner, status__in=application_end_states).exclude(imported=True).values_list('days_open', flat=True)

        if len(partner_app_time) > 0:
            context['median_days'] = get_median(list(partner_app_time))
        else:
            context['median_days'] = None

        context['app_distribution_data'] = get_application_status_data(
                Application.objects.filter(partner=partner)
            )

        context['signups_time_data'] = get_data_count_by_month(
                Application.objects.filter(partner=partner)
            )

        context['approved_or_sent_signups_time_data'] = get_data_count_by_month(
                Application.objects.filter(
                    partner=partner,
                    status__in=(Application.APPROVED, Application.SENT)
                )
            )

        context['users_time_data'] = get_users_by_partner_by_month(partner)

        # Find out if current user has applications and change the Apply
        # button behaviour accordingly
        if self.request.user.is_authenticated() and not partner.bundle:
            sent_apps = Application.objects.filter(
                                        editor=self.request.user.editor,
                                        status=Application.SENT,
                                        partner=partner
                                     ).order_by('date_closed')
            open_apps = Application.objects.filter(
                                        editor=self.request.user.editor,
                                        status__in=(Application.PENDING, Application.QUESTION, Application.APPROVED),
                                        partner=partner
                                     )
            context['user_sent_apps'] = False
            context['user_open_apps'] = False
            if sent_apps.count() > 0:
                context['latest_sent_app_pk'] = sent_apps[0].pk
                context['user_sent_apps'] = True
            elif open_apps.count() > 0:
                context['user_open_apps'] = True
                if open_apps.count() > 1:
                    context['multiple_open_apps'] = True
                else:
                    context['multiple_open_apps'] = False
                    context['open_app_pk'] = open_apps[0].pk

        partner_streams = Stream.objects.filter(partner=partner)
        if partner_streams.count() > 0:
            context['stream_unique_accepted'] = {}
            for stream in partner_streams:
                stream_unique_accepted = User.objects.filter(
                                      editor__applications__partner=partner,
                                      editor__applications__status__in=(Application.APPROVED, Application.SENT),
                                      editor__applications__specific_stream=stream).distinct().count()
                context['stream_unique_accepted'][stream.name] = stream_unique_accepted
        else:
            context['stream_unique_accepted'] = None

        return context


    def get_queryset(self):
{'fraction_non_alphanumeric': 0.08027384893168824, 'fraction_numerical': 0.001956063797773097, 'mean_word_length': 2.164246607950488, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '89184', 'n_tokens_mistral': 3388, 'n_tokens_neox': 3242, 'n_words': 659}