def _build_available_collection_object(self, context, language_code, partner_id_set): """ Helper function to build an available collections object that will fill the Available Collections section of the redesigned My Library template ---------- context : dict The context dictionary language_code: str The language code that some tags and descriptions will be translated to partner_id_set: set A set of partner IDs which are to be excluded from the query because they're already in the My Collections section of the interface Returns ------- dict The context dictionary with the available collections added """ if self.request.user.is_staff: available_collections = ( Partner.even_not_available.order_by("company_name").exclude( authorization_method__in=[Partner.BUNDLE]).exclude( id__in=partner_id_set)) else: # Available collections do not include bundle partners and collections # that the user is already authorized to access available_collections = Partner.objects.exclude( authorization_method__in=[Partner.BUNDLE]).exclude( id__in=partner_id_set) partner_filtered_list = PartnerFilter( self.request.GET, queryset=available_collections, language_code=language_code, ) context["filter"] = partner_filtered_list available_collection_obj = [] for available_collection in partner_filtered_list.qs: # Obtaining translated partner description partner_short_description_key = "{pk}_short_description".format( pk=available_collection.pk) partner_description_key = "{pk}_description".format( pk=available_collection.pk) partner_descriptions = get_partner_description( language_code, partner_short_description_key, partner_description_key) try: partner_logo = available_collection.logos.logo.url except PartnerLogo.DoesNotExist: partner_logo = None # Getting tags from locale files translated_tags = get_tag_names(language_code, available_collection.new_tags) available_collection_obj.append({ "pk": available_collection.pk, "partner_name": available_collection.company_name, "partner_logo": partner_logo, "short_description": partner_descriptions["short_description"], "description": partner_descriptions["description"], "languages": available_collection.get_languages, "tags": translated_tags, "is_not_available": available_collection.is_not_available, "is_waitlisted": available_collection.is_waitlisted, }) context["available_collections"] = sorted( available_collection_obj, key=lambda k: k["partner_name"]) context["number_available_collections"] = len(available_collection_obj) return context
def get_context_data(self, **kwargs): context = super(HomePageView, self).get_context_data(**kwargs) # Library bundle requirements # ----------------------------------------------------- # We bundle these up into a list so that we can loop them and have a simpler time # setting the relevant CSS. if self.request.user.is_authenticated: editor = self.request.user.editor sufficient_edits = editor.wp_enough_edits sufficient_tenure = editor.wp_account_old_enough sufficient_recent_edits = editor.wp_enough_recent_edits not_blocked = editor.wp_not_blocked else: sufficient_edits = False sufficient_tenure = False sufficient_recent_edits = False not_blocked = False context["bundle_criteria"] = [ # Translators: This text is shown next to a tick or cross denoting whether the current user has made more than 500 edits from their Wikimedia account. (_("500+ edits"), sufficient_edits), # Translators: This text is shown next to a tick or cross denoting whether the current user has Wikimedia account that is at least 6 months old. (_("6+ months editing"), sufficient_tenure), # Translators: This text is shown next to a tick or cross denoting whether the current user has made more than 10 edits within the last month (30 days) from their Wikimedia account. (_("10+ edits in the last month"), sufficient_recent_edits), # Translators: This text is shown next to a tick or cross denoting whether the current user's Wikimedia account has been blocked on any project. (_("No active blocks"), not_blocked), ] # Partner count # ----------------------------------------------------- context["partner_count"] = Partner.objects.all().count() context["bundle_partner_count"] = Partner.objects.filter( authorization_method=Partner.BUNDLE).count() # Apply section # ----------------------------------------------------- featured_partners_obj = [] featured_partners = Partner.objects.filter(featured=True)[:3] for partner in featured_partners: # Obtaining translated partner description language_code = get_language() partner_short_description_key = "{pk}_short_description".format( pk=partner.pk) partner_description_key = "{pk}_description".format(pk=partner.pk) partner_descriptions = get_partner_description( language_code, partner_short_description_key, partner_description_key) featured_partners_obj.append({ "pk": partner.pk, "partner_name": partner.company_name, "partner_logo": partner.logos.logo.url, "short_description": partner_descriptions["short_description"], "description": partner_descriptions["description"], }) context["featured_partners"] = featured_partners_obj return context
def _build_authorization_object(self, authorization_queryset, language_code, partner_id_set): """ Helper function to convert an Authorization queryset to an object that the view can parse ---------- authorization_queryset : Queryset<Authorization> The authorization queryset language_code: str The language code that some tags and descriptions will be translated to partner_id_set: set A set that will be filled with partner IDs. These partners will be excluded in the Available Collections section Returns ------- list A list that contains the transformed Authorization queryset """ user_authorization_obj = [] for user_authorization in authorization_queryset: partner_filtered_list = PartnerFilter( self.request.GET, queryset=user_authorization.partners.all(), language_code=language_code, ) # If there are no collections after filtering, we will skip this auth if partner_filtered_list.qs.count() == 0: continue else: open_app = user_authorization.get_open_app if user_authorization.date_expires: if user_authorization.date_expires < date.today(): has_expired = True else: has_expired = False else: has_expired = False for user_authorization_partner in partner_filtered_list.qs: # Obtaining translated partner description partner_short_description_key = "{pk}_short_description".format( pk=user_authorization_partner.pk) partner_description_key = "{pk}_description".format( pk=user_authorization_partner.pk) partner_descriptions = get_partner_description( language_code, partner_short_description_key, partner_description_key, ) try: partner_logo = user_authorization_partner.logos.logo.url except PartnerLogo.DoesNotExist: partner_logo = None # Getting tags from locale files translated_tags = get_tag_names( language_code, user_authorization_partner.new_tags) # Use the partner access url by default. access_url = user_authorization_partner.get_access_url # If the authorization is for a stream, and that stream has an access url, use it. stream = user_authorization.stream if stream and stream.get_access_url: access_url = stream.get_access_url user_authorization_obj.append({ "auth_pk": user_authorization.pk, "auth_date_authorized": user_authorization.date_authorized, "auth_date_expires": user_authorization.date_expires, "auth_is_valid": user_authorization.is_valid, "auth_latest_sent_app": user_authorization.get_latest_sent_app, "auth_open_app": open_app, "auth_has_expired": has_expired, "partner_pk": user_authorization_partner.pk, "partner_name": user_authorization_partner.company_name, "partner_logo": partner_logo, "partner_short_description": partner_descriptions["short_description"], "partner_description": partner_descriptions["description"], "partner_languages": user_authorization_partner.get_languages, "partner_tags": translated_tags, "partner_authorization_method": user_authorization_partner.authorization_method, "partner_access_url": access_url, "partner_is_not_available": user_authorization_partner.is_not_available, "partner_is_waitlisted": user_authorization_partner.is_waitlisted, }) partner_id_set.add(user_authorization_partner.pk) # Sort by partner name return sorted(user_authorization_obj, key=lambda k: k["partner_name"])
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context["bundle_criteria"] = [ # Translators: This text is shown next to a tick or cross denoting whether the current user has made more than 500 edits from their Wikimedia account. _("500+ edits"), # Translators: This text is shown next to a tick or cross denoting whether the current user has Wikimedia account that is at least 6 months old. _("6+ months editing"), # Translators: This text is shown next to a tick or cross denoting whether the current user has made more than 10 edits within the last month (30 days) from their Wikimedia account. _("10+ edits in the last month"), # Translators: This text is shown next to a tick or cross denoting whether the current user's Wikimedia account has been blocked on any project. _("No active blocks"), ] language_code = get_language() translated_tags = get_tag_dict(language_code) if len(translated_tags) > 9: context["tags"] = dict(list(translated_tags.items())[0:9]) context["more_tags"] = dict( list(translated_tags.items())[10:len(translated_tags)]) else: context["tags"] = translated_tags context["more_tags"] = None partners_obj = [] try: tags = self.request.GET.get("tags") if tags: # Since multidisciplinary partners may have content that users may # find useful, we are filtering by the multidisciplinary tag as well tag_filter = Q(new_tags__tags__contains=tags) | Q( new_tags__tags__contains="multidisciplinary_tag") # This variable is to indicate which tag filter has been selected context["selected"] = tags # It is harder to get only one tag value from a dictionary in a # template, so we are getting the translated tag value in the view context["selected_value"] = translated_tags[tags] else: tag_filter = Q(featured=True) except KeyError: tag_filter = Q(featured=True) # Partners will appear ordered by the selected tag first, then by the # multidisciplinary tag if tags: # Order by ascending tag order if tag name is before multidisciplinary if tags < "multidisciplinary_tag": partners = Partner.objects.filter(tag_filter).order_by( "new_tags__tags", "?") # Order by descending tag order if tag name is after multidisciplinary else: partners = Partner.objects.filter(tag_filter).order_by( "-new_tags__tags", "?") # No tag filter was passed, ordering can be random else: partners = Partner.objects.filter(tag_filter).order_by("?") for partner in partners: # Obtaining translated partner description partner_short_description_key = "{pk}_short_description".format( pk=partner.pk) partner_description_key = "{pk}_description".format(pk=partner.pk) partner_descriptions = get_partner_description( language_code, partner_short_description_key, partner_description_key) try: partner_logo = partner.logos.logo.url except PartnerLogo.DoesNotExist: partner_logo = None partners_obj.append({ "pk": partner.pk, "partner_name": partner.company_name, "partner_logo": partner_logo, "short_description": partner_descriptions["short_description"], "description": partner_descriptions["description"], }) context["partners"] = partners_obj return context