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 messages.add_message( self.request, messages.WARNING, _("This Partner's status 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'] partner_app_time = Application.objects.filter( partner=partner).values_list('days_open', flat=True) context['median_days'] = get_median(list(partner_app_time)) 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_signups_time_data'] = get_data_count_by_month( Application.objects.filter(partner=partner, status=Application.APPROVED)) context['users_time_data'] = get_users_by_partner_by_month(partner) return context
def get_context_data(self, **kwargs): context = super(PartnersDetailView, self).get_context_data(**kwargs) partner = self.get_object() context['total_apps'] = Application.objects.filter( partner=partner).count() context['total_apps_approved'] = Application.objects.filter( partner=partner, status=Application.APPROVED).count() 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() partner_app_time = Application.objects.filter( partner=partner).values_list('days_open', flat=True) context['median_days'] = get_median(list(partner_app_time)) 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_signups_time_data'] = get_data_count_by_month( Application.objects.filter( partner=partner, status=Application.APPROVED ) ) context['users_time_data'] = get_users_by_partner_by_month(partner) return context
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'] # This if else block supports the template with the number of accounts available partner_streams = Stream.objects.filter(partner=partner) context['partner_streams'] = partner_streams if partner_streams.count() > 0: context['total_accounts_available_stream'] = {} context['stream_unique_accepted'] = {} for stream in partner_streams: if stream.accounts_available is not None: total_apps_approved_or_sent_stream = User.objects.filter( editor__applications__partner=partner, editor__applications__status__in=(Application.APPROVED, Application.SENT), editor__applications__specific_stream=stream).count() total_accounts_available = stream.accounts_available context['total_accounts_available_stream'][ stream. name] = total_accounts_available - total_apps_approved_or_sent_stream 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['total_accounts_available_stream'] = None context['stream_unique_accepted'] = None if partner.accounts_available is not None: context[ 'total_accounts_available_partner'] = partner.accounts_available - context[ 'total_apps_approved_or_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'] = User.objects.filter( editor__applications__partner=partner, editor__applications__status__in=( Application.APPROVED, Application.SENT)).distinct().count() 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)) # To restrict the graph from rendering, if there's only a week's worth of data earliest_date = get_earliest_creation_date( Application.objects.filter(partner=partner)) context['insufficient_data'] = False if earliest_date: current_date = timezone.now().date() days_since_earliest_application = (current_date - earliest_date).days if days_since_earliest_application < 7: # Less than a week's of data context['insufficient_data'] = True 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.authorization_method == 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 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 elif sent_apps.count() > 0: # Because using sent_apps[0] may not always hold the latest application, # particularly when multiple applications where made on the same day for every_app in sent_apps: if every_app.is_renewable: context['latest_sent_app_pk'] = every_app.pk context['user_sent_apps'] = True break 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_context_data(self, **kwargs): context = super(PartnersDetailView, self).get_context_data(**kwargs) partner = self.get_object() if partner.status == Partner.NOT_AVAILABLE: 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_accounts_distributed_partner"] = count_valid_authorizations( partner ) partner_streams = Stream.objects.filter(partner=partner) if partner_streams.count() > 0: context["total_accounts_distributed_streams"] = {} for stream in partner_streams: context["total_accounts_distributed_streams"][ stream ] = count_valid_authorizations(partner, stream) else: context["total_accounts_distributed_streams"] = None context["total_users"] = Authorization.objects.filter(partners=partner).count() 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 # Find out if current user has authorizations/apps # and change the Apply button and the help text # behaviour accordingly context["apply"] = False context["has_open_apps"] = False context["has_auths"] = False if ( self.request.user.is_authenticated and not partner.authorization_method == partner.BUNDLE ): context["apply"] = True user = self.request.user apps = Application.objects.filter( status__in=[ Application.PENDING, Application.QUESTION, Application.APPROVED, ], partner=partner, editor=user.editor, ) if partner_streams.count() == 0: if apps.count() > 0: # User has open applications, don't show 'apply', # but link to apps page context["has_open_apps"] = True if not partner.specific_title: context["apply"] = False try: Authorization.objects.get(partners=partner, user=user) # User has an authorization, don't show 'apply', # but link to collection page if not partner.specific_title: context["apply"] = False # User fulfills initial authorization fulfills_auth = True # Checking if user has agreed to terms and conditions, otherwise # they shouldn't be authorized to access the collection user_agreed_terms = user.userprofile.terms_of_use final_auth = fulfills_auth and user_agreed_terms context["has_auths"] = final_auth except Authorization.DoesNotExist: pass except Authorization.MultipleObjectsReturned: logger.info( "Multiple authorizations returned for partner {} and user {}".format( partner, user ) ) messages.add_message( self.request, messages.ERROR, # fmt: off # Translators: If multiple authorizations where returned for a partner with no collections, this message is shown to an user _("Multiple authorizations were returned – something's wrong. Please contact us and don't forget to mention this message."), # fmt: on ) else: authorizations = Authorization.objects.filter( partners=partner, user=user ) if authorizations.count() == partner_streams.count(): # User has correct number of auths, don't show 'apply', # but link to collection page context["apply"] = False # User fulfills initial authorization fulfills_auth = True # Checking if user has agreed to terms and conditions, otherwise # they shouldn't be authorized to access the collection user_agreed_terms = user.userprofile.terms_of_use final_auth = fulfills_auth and user_agreed_terms context["has_auths"] = final_auth if apps.count() > 0: # User has open apps, link to apps page context["has_open_apps"] = True else: auth_streams = [] for each_authorization in authorizations: # We are interested in the streams of existing authorizations if each_authorization.stream in partner_streams: auth_streams.append(each_authorization.stream) if auth_streams: # User fulfills initial authorization fulfills_auth = True # Checking if user has agreed to terms and conditions, otherwise # they shouldn't be authorized to access the collection user_agreed_terms = user.userprofile.terms_of_use final_auth = fulfills_auth and user_agreed_terms # User has authorizations, link to collection page context["has_auths"] = final_auth no_auth_streams = partner_streams.exclude( name__in=auth_streams ) # streams with no corresponding authorizations – we'll want to know if these have apps if apps.count() > 0: # User has open apps, link to apps page context["has_open_apps"] = True # The idea behind the logic below is to find out if we have # at least a single stream the user hasn't applied to. If so, # we show the apply button; if not, we disable it. all_streams_have_apps = True for each_no_auth_stream in no_auth_streams: stream_has_app = False for each_app in apps: if each_app.specific_stream == each_no_auth_stream: stream_has_app = True break if not stream_has_app: all_streams_have_apps = False break if all_streams_have_apps: context["apply"] = False return context
def get_context_data(self, **kwargs): context = super(PartnersDetailView, self).get_context_data(**kwargs) partner = self.get_object() if partner.status == Partner.NOT_AVAILABLE: # 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_accounts_distributed_partner"] = count_valid_authorizations( partner) partner_streams = Stream.objects.filter(partner=partner) if partner_streams.count() > 0: context["total_accounts_distributed_streams"] = {} for stream in partner_streams: context["total_accounts_distributed_streams"][ stream] = count_valid_authorizations(partner, stream) else: context["total_accounts_distributed_streams"] = None context["total_users"] = Authorization.objects.filter( partner=partner).count() 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 # Find out if current user has applications and change the Apply # button behaviour accordingly if (self.request.user.is_authenticated() and not partner.authorization_method == 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 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 elif sent_apps.count() > 0: # Because using sent_apps[0] may not always hold the latest application, # particularly when multiple applications where made on the same day for every_app in sent_apps: if every_app.is_renewable: context["latest_sent_app_pk"] = every_app.pk context["user_sent_apps"] = True break return context