Skip to content

Commit

Permalink
Fix category fallthrough (#7212)
Browse files Browse the repository at this point in the history
* make missing categories fall through to the EN version

* small docs update

* ???

* oops

* data-name should stay English
  • Loading branch information
Pomax authored Aug 10, 2021
1 parent 0cdd901 commit 3e028c4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 18 deletions.
39 changes: 25 additions & 14 deletions network-api/networkapi/wagtailpages/pagemodels/products.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,32 +43,43 @@
('Needs Improvement', 'Needs Improvement'),
('Bad', 'Bad')
]

DEFAULT_LOCALE_ID = Locale.objects.get(language_code=settings.LANGUAGE_CODE).id
DEFAULT_LANGUAGE_CODE = settings.LANGUAGE_CODE
DEFAULT_LOCALE = Locale.objects.get(language_code=DEFAULT_LANGUAGE_CODE)
DEFAULT_LOCALE_ID = DEFAULT_LOCALE.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
language_code = DEFAULT_LANGUAGE_CODE
if hasattr(request, 'LANGUAGE_CODE'):
default_language_code = request.LANGUAGE_CODE
return default_language_code
language_code = request.LANGUAGE_CODE
return 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.
Start with the English list of categories, and replace any of them
with their localized counterpart, where possible, so that we don't
end up with an incomplete category list due to missing locale records.
"""
default_locale_list = BuyersGuideProductCategory.objects.filter(
hidden=False,
locale=DEFAULT_LOCALE,
)

if language_code == DEFAULT_LANGUAGE_CODE:
return default_locale_list

actual_locale = Locale.objects.get(language_code=language_code)
return [
cat
for cat in BuyersGuideProductCategory.objects.filter(
hidden=False,
locale=Locale.objects.get(language_code=language_code)
)
if not cat.original.hidden
]
BuyersGuideProductCategory.objects.filter(
translation_key=cat.translation_key,
locale=actual_locale,
).first() or cat for cat in default_locale_list
]


def get_product_subset(cutoff_date, authenticated, key, products, language_code='en'):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
{% with original=cat.original %}
{% if original.published_product_page_count > 0 %}
{% localizedroutablepageurl home_page 'category-view' lang_code original.slug as cat_url %}
<a class="multipage-link {% check_active_category current_category cat %}{% if original.featured is True %} featured{% endif %}" href="{{ cat_url }}" data-name="{{ cat.name }}">{{ cat.name }}</a>
<a class="multipage-link {% check_active_category current_category cat %}{% if original.featured is True %} featured{% endif %}" href="{{ cat_url }}" data-name="{{ original.name }}">{{ cat.name }}</a>
{% endif %}
{% endwith %}
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<a
class="multipage-link {% check_active_category current_category cat %}{% if original.featured is True %} featured{% endif %}"
href="{{ cat_url }}"
data-name="{{ cat.name }}">
data-name="{{ original.name }}">
{{ cat.name }}
</a>
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
def check_active_category(current_category, target_category):
# because we're working with potentially localized data,
# make sure to compare the linguistic originals.
current_category = getattr(current_category, 'alias_of', current_category)
target_category = getattr(target_category, 'alias_of', target_category)
current_category = getattr(current_category, 'original', current_category)
target_category = getattr(target_category, 'original', target_category)
return 'active' if current_category == target_category else ''


Expand Down

0 comments on commit 3e028c4

Please sign in to comment.