Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cms.views - set request language according to request.LANGUAGE_CODE #7610

Open
wants to merge 13 commits into
base: develop-4
Choose a base branch
from
Open
24 changes: 23 additions & 1 deletion cms/cache/page.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hashlib
from datetime import timedelta
from importlib import import_module

from django.conf import settings
from django.utils.cache import (
Expand All @@ -17,13 +18,34 @@
from cms.utils.conf import get_cms_setting
from cms.utils.helpers import get_timezone_name

# during python load phase, resolve custom cache key extra getter from settings into function
_page_cache_key_extra = None
if hasattr(settings, "CMS_PAGE_CACHE_KEY_EXTRA"):
try:
mod_path, met = settings.CMS_PAGE_CACHE_KEY_EXTRA.rsplit('.', 1)

mod = import_module(mod_path)
_page_cache_key_extra = getattr(mod, met)
except ImportError:
pass


def _page_cache_key(request):
"""
Wrapper function to resolve if custom page cache key extra getter was provided
"""
page_cache_key = _default_page_cache_key(request)
if _page_cache_key_extra is not None:
page_cache_key += _page_cache_key_extra(request)
return page_cache_key


def _default_page_cache_key(request):
# sha1 key of current path
cache_key = "%s:%d:%s" % (
get_cms_setting("CACHE_PREFIX"),
settings.SITE_ID,
hashlib.sha1(iri_to_uri(request.get_full_path()).encode('utf-8')).hexdigest()
hashlib.sha1(iri_to_uri(request.get_full_path()).encode('utf-8')).hexdigest(),
)
if settings.USE_TZ:
cache_key += '.%s' % get_timezone_name()
Expand Down
25 changes: 24 additions & 1 deletion cms/cache/placeholder.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,37 @@
The vary-on header-names are also stored with the version. This enables us to
check for cache hits without re-computing placeholder.get_vary_cache_on().
"""
import hashlib
import time

from importlib import import_module

from django.conf import settings
from django.utils.timezone import now

from cms.utils.conf import get_cms_setting
from cms.utils.helpers import get_header_name, get_timezone_name

# during python load phase, resolve custom cache key extra getter from settings into function

Check failure on line 30 in cms/cache/placeholder.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

cms/cache/placeholder.py:19:1: I001 Import block is un-sorted or un-formatted
_placeholder_cache_key_extra = None
if hasattr(settings, "CMS_PLACEHOLDER_CACHE_KEY_EXTRA"):
try:
mod_path, met = settings.CMS_PLACEHOLDER_CACHE_KEY_EXTRA.rsplit('.', 1)

mod = import_module(mod_path)
_placeholder_cache_key_extra = getattr(mod, met)
except ImportError:
pass

def _get_placeholder_cache_key(placeholder, lang, site_id, request, soft=False):
"""
Wrapper function to resolve if custom placeholder cache key extra getter was provided
"""
placeholder_cache_key = _default_placeholder_cache_key(placeholder, lang, site_id, request, soft=False)
if _placeholder_cache_key_extra is not None:
placeholder_cache_key += _placeholder_cache_key_extra(placeholder, lang, site_id, request, soft=False)
return placeholder_cache_key


def _get_placeholder_cache_version_key(placeholder, lang, site_id):
"""
Expand Down Expand Up @@ -85,7 +108,7 @@
cache.set(key, (version, vary_on_list), duration)


def _get_placeholder_cache_key(placeholder, lang, site_id, request, soft=False):
def _default_placeholder_cache_key(placeholder, lang, site_id, request, soft=False):
"""
Returns the fully-addressed cache key for the given placeholder and
the request.
Expand Down
6 changes: 2 additions & 4 deletions cms/page_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,10 @@ def _handle_no_page(request):
return HttpResponseRedirect(redirect_url)

# add a $ to the end of the url (does not match on the cms anymore)
resolve('%s$' % request.path)
return resolve('%s$' % request.path).func(request)
except Resolver404 as e:
# raise a django http 404 page
exc = Http404(dict(path=request.path, tried=e.args[0]['tried']))
raise exc
raise Http404('CMS Page not found: %s' % request.path)
raise Http404(dict(path=request.path, tried=e.args[0]['tried']))


def _handle_no_apphook(request):
Expand Down
7 changes: 4 additions & 3 deletions cms/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,11 @@ def details(request, slug):
user_languages = get_public_languages(site_id=site.pk)

request_language = None
if is_language_prefix_patterns_used():
request_language = get_language_from_request(request, check_path=True)
if hasattr(request, "LANGUAGE_CODE"):
# use language from middleware - usually django.middleware.locale.LocaleMiddleware
request_language = request.LANGUAGE_CODE
if not request_language:
request_language = get_default_language_for_site(get_current_site().pk)
request_language = get_default_language_for_site(site.pk)

if not page.is_home and request_language not in user_languages:
# The homepage is treated differently because
Expand Down
9 changes: 5 additions & 4 deletions menus/menu_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,13 @@ def __init__(self, pool, request):
# instance lives.
self.menus = pool.get_registered_menus(for_rendering=True)
self.request = request
self.site = Site.objects.get_current(request)
self.request_language = None
if is_language_prefix_patterns_used():
self.request_language = get_language_from_request(request, check_path=True)
if hasattr(request, "LANGUAGE_CODE"):
# use language from middleware - usually django.middleware.locale.LocaleMiddleware
self.request_language = request.LANGUAGE_CODE
if not self.request_language:
self.request_language = get_default_language_for_site(get_current_site().pk)
self.site = Site.objects.get_current(request)
self.request_language = get_default_language_for_site(self.site.pk)
toolbar = getattr(request, "toolbar", None)
self.edit_or_preview = toolbar.edit_mode_active or toolbar.preview_mode_active if toolbar else False

Expand Down
Loading