def handle(self, *args, **options): all_partners = Partner.objects.filter( authorization_method=Partner.PROXY, status=Partner.WAITLIST).exclude(specific_stream=True, accounts_available__isnull=True) for each_partner in all_partners: valid_authorizations = count_valid_authorizations(each_partner.pk) total_accounts_available_for_distribution = ( each_partner.accounts_available - valid_authorizations) if total_accounts_available_for_distribution > 0: each_partner.status = Partner.AVAILABLE each_partner.save() logger.info("Partner {name} is unwaitlisted".format( name=each_partner.company_name))
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