Skip to content

Commit

Permalink
Merge pull request #6220 from smithellis/04WT-wagtail-decorator
Browse files Browse the repository at this point in the history
Wagtail - decorator
  • Loading branch information
akatsoulas authored Sep 10, 2024
2 parents 0967395 + 4a1f6d9 commit 0ed4c62
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions kitsune/products/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from product_details import product_details

from kitsune.products.models import Product, Topic, TopicSlugHistory
from kitsune.sumo.decorators import prefer_cms
from kitsune.wiki.decorators import check_simple_wiki_locale
from kitsune.wiki.facets import documents_for, topics_for
from kitsune.wiki.models import Document, Revision
Expand All @@ -23,6 +24,7 @@ def product_list(request):


@check_simple_wiki_locale
@prefer_cms
def product_landing(request, slug):
"""The product landing page."""
if slug == "firefox-accounts":
Expand Down
27 changes: 27 additions & 0 deletions kitsune/sumo/decorators.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import json
import re
from functools import wraps

from csp.utils import build_policy
from django import http
from django.conf import settings
from django.core.exceptions import PermissionDenied
from django.http import Http404

from kitsune.sumo.utils import is_ratelimited

from wagtail.views import serve as wagtail_serve


# Copy/pasta from from https://gist.github.com/1405096
# TODO: Log the hell out of the exceptions.
JSON = "application/json"
Expand Down Expand Up @@ -153,3 +158,25 @@ def wrapped(*args, **kwargs):
return response

return wrapped


def remove_locale(url):
# Define the regex pattern for locale (e.g., /en-US/ or /en-us/)
locale_pattern = r"^/([a-z]{2}(-[a-zA-Z]{2})?)/"
# Remove the locale part
return re.sub(locale_pattern, "/", url)


def prefer_cms(view_func):
@wraps(view_func)
def _wrapped_view(request, *args, **kwargs):
path = remove_locale(request.path_info)
try:
wagtail_response = wagtail_serve(request, path)
if wagtail_response.status_code == 200:
return wagtail_response
except Http404:
pass # Continue to the original view if no Wagtail page is found
return view_func(request, *args, **kwargs)

return _wrapped_view

0 comments on commit 0ed4c62

Please sign in to comment.