Skip to content
Closed
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
33 changes: 27 additions & 6 deletions network-api/networkapi/wagtailpages/pagemodels/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.core.models import Orderable, Page
from wagtail.core.models import Locale, Orderable, Page
from wagtail.core import hooks

from wagtail_localize.fields import SynchronizedField, TranslatableField
Expand Down Expand Up @@ -57,14 +57,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 = products.specific()
Expand Down Expand Up @@ -1252,6 +1255,13 @@ def get_banner(self):
SynchronizedField('slug'),
]

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 @@ -1325,6 +1335,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 @@ -1335,14 +1346,16 @@ 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)

if products is None:
products = get_product_subset(
self.cutoff_date,
authenticated,
key,
ProductPage.objects.filter(product_categories__category__in=[category])
ProductPage.objects.filter(product_categories__category__in=[category]),
language_code=language_code
)

context['category'] = category.slug
Expand Down Expand Up @@ -1386,20 +1399,28 @@ 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)

if not kwargs.get('bypass_products', False) and products is None:
products = get_product_subset(
self.cutoff_date,
authenticated,
key,
ProductPage.objects.all()
ProductPage.objects.all(),
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
return context
Expand Down