Merge pull request #6220 from smithellis/04WT-wagtail-decorator

Wagtail - decorator
This commit is contained in:
Tasos Katsoulas 2024-09-10 15:43:11 +03:00 коммит произвёл GitHub
Родитель 0967395fd2 4a1f6d993c
Коммит 0ed4c62339
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 29 добавлений и 0 удалений

Просмотреть файл

@ -8,6 +8,7 @@ from django.shortcuts import get_object_or_404, redirect, render
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
@ -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":

Просмотреть файл

@ -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"
@ -153,3 +158,25 @@ def csp_allow_inline_scripts_and_styles(fn):
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