Skip to content

Commit

Permalink
show correct categories on product pages, and relocalize urls to poin…
Browse files Browse the repository at this point in the history
…t to use the correct locale
  • Loading branch information
Pomax committed Aug 9, 2021
1 parent 85d7eae commit c3d7861
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
46 changes: 29 additions & 17 deletions network-api/networkapi/wagtailpages/pagemodels/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,29 @@
DEFAULT_LOCALE_ID = Locale.objects.get(language_code=settings.LANGUAGE_CODE).id


def get_language_code_from_request(request):
"""
Accepts a request. Returns a language code (string) if there is one. Falls back to English.
"""
default_language_code = settings.LANGUAGE_CODE
if hasattr(request, 'LANGUAGE_CODE'):
default_language_code = request.LANGUAGE_CODE
return default_language_code

def get_categories_for_locale(language_code):
"""
Make sure that we check both the "whatever the current locale" category
for whether or not it's hidden, but also the original English version.
"""
return [
cat
for cat in BuyersGuideProductCategory.objects.filter(
hidden=False,
locale=Locale.objects.get(language_code=language_code)
)
if not cat.original.hidden
]

def get_product_subset(cutoff_date, authenticated, key, products, language_code='en'):
"""
filter a queryset based on our current cutoff date,
Expand Down Expand Up @@ -798,7 +821,8 @@ def get_or_create_votes(self):
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['product'] = self
context['categories'] = BuyersGuideProductCategory.objects.filter(hidden=False)
language_code = get_language_code_from_request(request)
context['categories'] = get_categories_for_locale(language_code)
context['mediaUrl'] = settings.MEDIA_URL
context['use_commento'] = settings.USE_COMMENTO
context['pageTitle'] = f'{self.title} | ' + gettext("Privacy & security guide") + ' | Mozilla Foundation'
Expand Down Expand Up @@ -1375,13 +1399,6 @@ def get_banner(self):
SynchronizedField('search_image'),
]

def get_language_code(self, request):
"""Accepts a request. Returns a language code (string) if there is one. Falls back to English."""
default_language_code = settings.LANGUAGE_CODE
if hasattr(request, 'LANGUAGE_CODE'):
default_language_code = request.LANGUAGE_CODE
return default_language_code

@route(r'^about/$', name='how-to-use-view')
def about_page(self, request):
context = self.get_context(request)
Expand Down Expand Up @@ -1455,7 +1472,7 @@ def product_view(self, request, slug):
@route(r'^categories/(?P<slug>[\w\W]+)/', name='category-view')
def categories_page(self, request, slug):
context = self.get_context(request, bypass_products=True)
language_code = self.get_language_code(request)
language_code = get_language_code_from_request(request)
locale_id = Locale.objects.get(language_code=language_code).id
slug = slugify(slug)

Expand All @@ -1481,6 +1498,7 @@ def categories_page(self, request, slug):
authenticated = request.user.is_authenticated
key = f'cat_product_dicts_{slug}_auth' if authenticated else f'cat_product_dicts_{slug}_live'
key = f'{language_code}_{key}'
cache.clear()
products = cache.get(key)
exclude_cat_ids = [excats.category.id for excats in self.excluded_categories.all()]

Expand Down Expand Up @@ -1535,7 +1553,7 @@ def get_sitemap_urls(self, request):

def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
language_code = self.get_language_code(request)
language_code = get_language_code_from_request(request)

authenticated = request.user.is_authenticated
key = 'home_product_dicts_authed' if authenticated else 'home_product_dicts_live'
Expand All @@ -1552,13 +1570,7 @@ def get_context(self, request, *args, **kwargs):
language_code=language_code
)

# We need to test this to see whether it's actually hiding EN categories in other locales:
categories = BuyersGuideProductCategory.objects.filter(
hidden=False,
locale=Locale.objects.get(language_code=language_code)
)

context['categories'] = categories
context['categories'] = get_categories_for_locale(language_code)
context['products'] = products
context['web_monetization_pointer'] = settings.WEB_MONETIZATION_POINTER
pni_home_page = BuyersGuidePage.objects.first()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{% extends "buyersguide/bg_base.html" %}

{% load bg_selector_tags env l10n i18n static wagtailimages_tags %}
{% load bg_selector_tags env l10n i18n localization static wagtailimages_tags %}

{% get_current_language as lang_code %}

{% block head_extra %}
<meta property="og:title" content="{% blocktrans context "This can be localized. This is a reference to the “*batteries not included” mention on toys." %}privacy not included - {{ product.title }}{% endblocktrans %}" />
Expand Down Expand Up @@ -297,7 +299,7 @@ <h3 class="h2-heading mb-4">{% trans "Related products" %}</h3>
{% for related_product_page in product.related_product_pages.all %}
{% with related_product=related_product_page.related_product.localized %}
<div class="related-product col-6 col-md-3 mb-3 mb-md-0">
<a class="d-block{% if related_product.adult_content %} adult-content{% endif %}" href="{{ related_product.url }}">
<a class="d-block{% if related_product.adult_content %} adult-content{% endif %}" href="{% relocalized_url related_product.url lang_code %}">
<div class="img-container">
{% image related_product.image width-600 as img %}
<img
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% load static i18n wagtailimages_tags %}
{% load static i18n wagtailimages_tags localization %}

{% get_current_language as lang_code %}

<figure class="product-box d-flex flex-column justify-content-between{% if product.draft %} draft-product{% endif %}{% if product.adult_content %} adult-content{% endif %}{% if product.privacy_ding %} privacy-ding{% endif%}" data-creepiness="{{ product.creepiness }}">
<div class="top-left-badge-container">
Expand All @@ -15,7 +16,7 @@

{% include "fragments/adult_content_badge.html" with product=product %}

<a class="product-image text-center mt-4 h-100 d-flex flex-column justify-content-between" href="{{ product.url }}">
<a class="product-image text-center mt-4 h-100 d-flex flex-column justify-content-between" href="{% relocalized_url product.url lang_code %}">
<picture class="product-thumbnail">
<source
{% image product.image fill-360x360 as img_1x %}
Expand All @@ -32,7 +33,7 @@
</a>

<figcaption class="d-block mt-md-2 text-left">
<a class="product-links" href="{{ product.url }}">
<a class="product-links" href="{% relocalized_url product.url lang_code %}">
<div class="product-company body-small">{{product.company}}</div>
<div class="product-name body">{{product.title}}</div>
</a>
Expand Down

0 comments on commit c3d7861

Please sign in to comment.