Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localize PNI categories & products #7124

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions network-api/networkapi/wagtailpages/pagemodels/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

from wagtail.admin.edit_handlers import InlinePanel, FieldPanel, MultiFieldPanel, PageChooserPanel
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.core.models import Orderable, Page, TranslatableMixin
from wagtail.core.models import Locale, Orderable, Page, TranslatableMixin

from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.search import index
Expand Down Expand Up @@ -46,14 +46,17 @@
]


def get_product_subset(cutoff_date, authenticated, key, products):
def get_product_subset(cutoff_date, authenticated, key, products, language_code='en'):
"""
filter a queryset based on our current cutoff date,
as well as based on whether a user is authenticated
to the system or not (authenticated users get to
see all products, including draft products)
"""
products = products.filter(review_date__gte=cutoff_date)
products = products.filter(
review_date__gte=cutoff_date,
locale=Locale.objects.get(language_code=language_code)
)
if not authenticated:
products = products.live()
products = sort_average(products)
Expand Down Expand Up @@ -1365,6 +1368,13 @@ def get_banner(self):
),
]

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 @@ -1438,6 +1448,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)
slug = slugify(slug)

# If getting by slug fails, also try to get it by name.
Expand All @@ -1448,6 +1459,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}'
products = cache.get(key)
exclude_cat_ids = [excats.category.id for excats in self.excluded_categories.all()]

Expand All @@ -1457,7 +1469,8 @@ def categories_page(self, request, slug):
authenticated,
key,
ProductPage.objects.filter(product_categories__category__in=[category])
.exclude(product_categories__category__id__in=exclude_cat_ids)
.exclude(product_categories__category__id__in=exclude_cat_ids),
language_code=language_code
)

context['category'] = category.slug
Expand Down Expand Up @@ -1500,9 +1513,11 @@ 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)

authenticated = request.user.is_authenticated
key = 'home_product_dicts_authed' if authenticated else 'home_product_dicts_live'
key = f'{key}_{language_code}'
products = cache.get(key)
exclude_cat_ids = [excats.category.id for excats in self.excluded_categories.all()]

Expand All @@ -1511,10 +1526,15 @@ def get_context(self, request, *args, **kwargs):
self.cutoff_date,
authenticated,
key,
ProductPage.objects.exclude(product_categories__category__id__in=exclude_cat_ids)
ProductPage.objects.exclude(product_categories__category__id__in=exclude_cat_ids),
language_code=language_code
)

context['categories'] = BuyersGuideProductCategory.objects.filter(hidden=False)
categories = BuyersGuideProductCategory.objects.filter(
hidden=False,
locale=Locale.objects.get(language_code=language_code)
)
context['categories'] = categories
context['products'] = products
context['web_monetization_pointer'] = settings.WEB_MONETIZATION_POINTER
pni_home_page = BuyersGuidePage.objects.first()
Expand Down