Skip to content

Commit

Permalink
Remove forced redirect to English for PNI
Browse files Browse the repository at this point in the history
  • Loading branch information
TheoChevalier committed Mar 24, 2020
1 parent 2019c2d commit dacde26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 81 deletions.
2 changes: 1 addition & 1 deletion network-api/networkapi/buyersguide/templates/bg_base.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
{% block guts %}{% endblock %}
</div>

{% include "partials/footer.html" with exclude_language_switcher=True %}
{% include "partials/footer.html" %}
</div>

{% block extra_scripts %}{% endblock %}
Expand Down
68 changes: 12 additions & 56 deletions network-api/networkapi/buyersguide/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from datetime import date

from networkapi.buyersguide.factory import ProductFactory
from networkapi.buyersguide.models import RangeVote, BooleanVote, Product, BuyersGuideProductCategory
from networkapi.buyersguide.models import RangeVote, BooleanVote, Product
from networkapi.buyersguide.views import product_view, category_view, buyersguide_home
from django.core.management import call_command

Expand Down Expand Up @@ -328,33 +328,25 @@ def test_homepage(self):

def test_localised_homepage(self):
"""
Test that the homepage redirects when missing a locale code.
Test that the homepage redirects properly under different locale configurations.
"""
response = self.client.get('/privacynotincluded/')
self.assertEqual(response.status_code, 302, 'simple locale gets redirected')

def test_only_en(self):
"""
Test that the homepage redirects away from the other locales the site supports
"""
response = self.client.get('/fr/privacynotincluded', follow=True)
self.assertEqual(response.redirect_chain[1][0], '/en/privacynotincluded/', 'redirects /fr/ to /en/')
self.assertEqual(response.status_code, 200)

response = self.client.get('/de/privacynotincluded', follow=True)
self.assertEqual(response.redirect_chain[1][0], '/en/privacynotincluded/', 'redirects /de/ to /en/')
self.assertEqual(response.status_code, 200)

response = self.client.get('/pt/privacynotincluded', follow=True)
self.assertEqual(response.redirect_chain[1][0], '/en/privacynotincluded/', 'redirects /pt/ to /en/')
response = self.client.get('/privacynotincluded', follow=True, HTTP_ACCEPT_LANGUAGE='fr')
self.assertEqual(
response.redirect_chain[0][0],
'/fr/privacynotincluded/',
'redirects according to HTTP_ACCEPT_LANGUAGE'
)
self.assertEqual(response.status_code, 200)

response = self.client.get('/es/privacynotincluded', follow=True)
self.assertEqual(response.redirect_chain[1][0], '/en/privacynotincluded/', 'redirects /es/ to /en/')
response = self.client.get('/privacynotincluded', follow=True, HTTP_ACCEPT_LANGUAGE='foo')
self.assertEqual(response.redirect_chain[0][0], '/en/privacynotincluded/', 'redirects to /en/ by default')
self.assertEqual(response.status_code, 200)

response = self.client.get('/pl/privacynotincluded', follow=True)
self.assertEqual(response.redirect_chain[1][0], '/en/privacynotincluded/', 'redirects /pl/ to /en/')
response = self.client.get('/de/privacynotincluded', follow=True, HTTP_ACCEPT_LANGUAGE='it')
self.assertEqual(response.redirect_chain[0][0], '/de/privacynotincluded/', 'no redirect from hardcoded locale')
self.assertEqual(response.status_code, 200)

def test_product_view_404(self):
Expand Down Expand Up @@ -411,39 +403,3 @@ def name_change_changes_slug(self):
p.name = 'name changed'
p.save()
self.assertEqual(p.slug, slugify(p.name))

def test_only_en(self):
p = Product.objects.create(name='this should redirect', review_date=date.today())
url = f'/fr/privacynotincluded/products/{p.slug}/'
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
self.assertEqual(
response['Location'],
f'/en/privacynotincluded/products/{p.slug}/',
'redirects to /en/privacynotincluded/products/{slug}/'
)


class CategoryViewTest(TestCase):
def test_only_en(self):
c = BuyersGuideProductCategory.objects.create(name='testcategory')
url = f'/fr/privacynotincluded/categories/{c.name}/'
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
self.assertEqual(
response['Location'],
f'/en/privacynotincluded/categories/{c.name}/',
'redirects to /en/privacynotincluded/categories/{name}/'
)


class AboutViewTest(TestCase):
def test_only_en(self):
url = f'/fr/privacynotincluded/about/'
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
self.assertEqual(
response['Location'],
f'/en/privacynotincluded/about/',
'redirects to /en/privacynotincluded/about/'
)
24 changes: 0 additions & 24 deletions network-api/networkapi/buyersguide/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,6 @@ def get_average_creepiness(product):
return 50


def path_is_en_prefixed(path):
return path.startswith('/en/')


def get_en_redirect(path):
redirect_path = re.sub(locale_regex, '/en/', path)
return redirect(redirect_path, permanent=False)


def enforce_en_locale(view_handler):
def check_locale(*args, **kwargs):
path = args[0].path
if not path_is_en_prefixed(path):
return get_en_redirect(path)

return view_handler(*args, **kwargs)

return check_locale


def filter_draft_products(request, products):
if request.user.is_authenticated:
return products
Expand All @@ -69,7 +49,6 @@ def filter_draft_products(request, products):


@redirect_to_default_cms_site
@enforce_en_locale
def buyersguide_home(request):
products = cache.get('sorted_product_dicts')

Expand All @@ -88,7 +67,6 @@ def buyersguide_home(request):


@redirect_to_default_cms_site
@enforce_en_locale
def category_view(request, slug):
key = f'products_category__{slug}'
products = cache.get(key)
Expand All @@ -114,7 +92,6 @@ def category_view(request, slug):


@redirect_to_default_cms_site
@enforce_en_locale
def product_view(request, slug):
product = get_object_or_404(Product, slug=slug)

Expand Down Expand Up @@ -152,7 +129,6 @@ def product_view(request, slug):

def bg_about_page(template_name):
@redirect_to_default_cms_site
@enforce_en_locale
def render_view(request):
key = 'categories'
categories = cache.get(key)
Expand Down

0 comments on commit dacde26

Please sign in to comment.