forked from ungleich-public/dynamicweb
clone cms with menus, make menus standalone app
This commit is contained in:
parent
1ece5ce7c7
commit
afb096b7a3
1153 changed files with 318897 additions and 37 deletions
81
cms/utils/__init__.py
Normal file
81
cms/utils/__init__.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
# TODO: this is just stuff from utils.py - should be split / moved
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from django.http import HttpRequest
|
||||
|
||||
from cms.utils.compat.warnings import RemovedInDjangoCMS60Warning
|
||||
from cms.utils.i18n import (
|
||||
get_current_language,
|
||||
get_default_language,
|
||||
get_language_code,
|
||||
get_language_list,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
|
||||
def get_current_site(request: HttpRequest = None) -> "Site":
|
||||
"""
|
||||
Returns the current Site instance associated with the given request.
|
||||
"""
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
if request is None:
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"get_current_site() called without request. This may lead to unexpected behavior. "
|
||||
"Use get_current_site(request) instead.",
|
||||
RemovedInDjangoCMS60Warning,
|
||||
stacklevel=2,
|
||||
)
|
||||
|
||||
# Allow middleware to identify the site
|
||||
request_site = getattr(request, "site", None) if request else None
|
||||
if request_site:
|
||||
return request_site
|
||||
|
||||
# Fallback to the default site configuration by SITE_ID or through request
|
||||
return Site.objects.get_current(request=request)
|
||||
|
||||
|
||||
def get_language_from_request(request: HttpRequest, current_page=None) -> str:
|
||||
"""
|
||||
Return the most obvious language according the request
|
||||
"""
|
||||
if getattr(request, '_cms_language', None):
|
||||
return request._cms_language
|
||||
|
||||
language = None
|
||||
if hasattr(request, 'POST'):
|
||||
language = request.POST.get('language', None)
|
||||
if hasattr(request, 'GET') and not language:
|
||||
language = request.GET.get('language', None)
|
||||
site_id = current_page.site_id if current_page else get_current_site(request).pk
|
||||
if language:
|
||||
language = get_language_code(language, site_id=site_id)
|
||||
if language not in get_language_list(site_id):
|
||||
language = None
|
||||
if not language and request:
|
||||
# get the active language
|
||||
language = get_current_language()
|
||||
if language:
|
||||
if language not in get_language_list(site_id):
|
||||
language = None
|
||||
|
||||
if not language and current_page:
|
||||
# in last resort, get the first language available in the page
|
||||
languages = current_page.get_languages()
|
||||
|
||||
if len(languages) > 0:
|
||||
language = languages[0]
|
||||
|
||||
if not language:
|
||||
# language must be defined in CMS_LANGUAGES, so check first if there
|
||||
# is any language with LANGUAGE_CODE, otherwise try to split it and find
|
||||
# best match
|
||||
language = get_default_language(site_id=site_id)
|
||||
|
||||
request._cms_language = language
|
||||
return language
|
||||
29
cms/utils/admin.py
Normal file
29
cms/utils/admin.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from django.contrib.sites.models import Site
|
||||
from django.forms import IntegerField, ValidationError
|
||||
from django.http import JsonResponse
|
||||
from django.utils.encoding import smart_str
|
||||
|
||||
from cms.utils import get_current_site
|
||||
|
||||
NOT_FOUND_RESPONSE = "NotFound"
|
||||
|
||||
|
||||
def jsonify_request(response):
|
||||
""" Turn any response in a 200 response to let jQuery code handle it nicely.
|
||||
Response contains a json object with the following attributes:
|
||||
* status: original response status code
|
||||
* content: original response content
|
||||
"""
|
||||
content = {'status': response.status_code, 'content': smart_str(response.content, response.charset)}
|
||||
return JsonResponse(content)
|
||||
|
||||
|
||||
def get_site_from_request(request):
|
||||
try:
|
||||
site_id = request.GET.get("site") or request.POST.get("site")
|
||||
site_id = IntegerField().clean(site_id)
|
||||
site = Site.objects.get(pk=site_id)
|
||||
except (ValidationError, Site.DoesNotExist):
|
||||
site = get_current_site(request)
|
||||
|
||||
return site
|
||||
105
cms/utils/apphook_reload.py
Normal file
105
cms/utils/apphook_reload.py
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
import logging
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
# Py2 and Py3 compatible reload
|
||||
from importlib import reload
|
||||
from threading import local
|
||||
|
||||
from django.conf import settings
|
||||
from django.urls import clear_url_caches
|
||||
|
||||
logger = logging.getLogger("cms")
|
||||
|
||||
_urlconf_revision = {}
|
||||
_urlconf_revision_threadlocal = local()
|
||||
|
||||
use_threadlocal = False
|
||||
|
||||
|
||||
def ensure_urlconf_is_up_to_date():
|
||||
global_revision = get_global_revision()
|
||||
local_revision = get_local_revision()
|
||||
|
||||
if not local_revision:
|
||||
set_local_revision(global_revision)
|
||||
elif global_revision != local_revision:
|
||||
if settings.DEBUG:
|
||||
log_reloading_apphook(global_revision, local_revision)
|
||||
debug_check_url('my_test_app_view')
|
||||
reload_urlconf(new_revision=global_revision)
|
||||
if settings.DEBUG:
|
||||
debug_check_url('my_test_app_view')
|
||||
|
||||
|
||||
def get_local_revision(default=None):
|
||||
if use_threadlocal:
|
||||
return getattr(_urlconf_revision_threadlocal, "value", default)
|
||||
else:
|
||||
return _urlconf_revision.get('urlconf_revision', default)
|
||||
|
||||
|
||||
def set_local_revision(revision):
|
||||
if use_threadlocal:
|
||||
if revision:
|
||||
_urlconf_revision_threadlocal.value = revision
|
||||
else:
|
||||
if hasattr(_urlconf_revision_threadlocal, "value"):
|
||||
del _urlconf_revision_threadlocal.value
|
||||
else:
|
||||
_urlconf_revision['urlconf_revision'] = revision
|
||||
|
||||
|
||||
def get_global_revision():
|
||||
from ..models import UrlconfRevision
|
||||
revision, _ = UrlconfRevision.get_or_create_revision(
|
||||
revision=str(uuid.uuid4()))
|
||||
return revision
|
||||
|
||||
|
||||
def set_global_revision(new_revision=None):
|
||||
from ..models import UrlconfRevision
|
||||
if new_revision is None:
|
||||
new_revision = str(uuid.uuid4())
|
||||
UrlconfRevision.update_revision(new_revision)
|
||||
|
||||
|
||||
def mark_urlconf_as_changed():
|
||||
new_revision = str(uuid.uuid4())
|
||||
set_global_revision(new_revision=new_revision)
|
||||
return new_revision
|
||||
|
||||
|
||||
def reload_urlconf(urlconf=None, new_revision=None):
|
||||
from cms.appresolver import clear_app_resolvers, get_app_patterns
|
||||
|
||||
if 'cms.urls' in sys.modules:
|
||||
reload(sys.modules['cms.urls'])
|
||||
if urlconf is None:
|
||||
urlconf = settings.ROOT_URLCONF
|
||||
if urlconf in sys.modules:
|
||||
reload(sys.modules[urlconf])
|
||||
clear_app_resolvers()
|
||||
clear_url_caches()
|
||||
get_app_patterns()
|
||||
if new_revision is not None:
|
||||
set_local_revision(new_revision)
|
||||
|
||||
|
||||
def log_reloading_apphook(global_revision, local_revision):
|
||||
logger.debug(f" New revision!!!! RELOAD!\n"
|
||||
f" {global_revision} ({type(global_revision)})\n"
|
||||
f" -> {local_revision} ({type(local_revision)})")
|
||||
|
||||
|
||||
def debug_check_url(url_name):
|
||||
from django.urls import reverse
|
||||
|
||||
try:
|
||||
debug_msg = " reverse('{}'): {} ".format(
|
||||
url_name,
|
||||
reverse('my_test_app_view'),
|
||||
)
|
||||
except Exception as e:
|
||||
debug_msg = f" ERROR: reverse('{url_name}'): {e}"
|
||||
logger.debug(debug_msg)
|
||||
496
cms/utils/check.py
Normal file
496
cms/utils/check.py
Normal file
|
|
@ -0,0 +1,496 @@
|
|||
import inspect
|
||||
from contextlib import contextmanager
|
||||
from itertools import chain
|
||||
|
||||
from django.conf import settings
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.utils.termcolors import colorize
|
||||
from sekizai.helpers import validate_template
|
||||
|
||||
from cms import constants
|
||||
from cms.models import AliasPluginModel
|
||||
from cms.utils.compat.dj import is_installed
|
||||
from cms.utils.conf import get_cms_setting
|
||||
|
||||
SUCCESS = 1
|
||||
WARNING = 2
|
||||
ERROR = 3
|
||||
SKIPPED = 4
|
||||
|
||||
CHECKERS = []
|
||||
|
||||
|
||||
class FileOutputWrapper:
|
||||
"""
|
||||
Wraps two file-like objects (that support at the very least the 'write'
|
||||
method) into an API to be used by the check function further down in
|
||||
this module.
|
||||
|
||||
The following properties are public (and required) by alternative implementations:
|
||||
|
||||
errors: integer count of errors encountered
|
||||
successes: integer count of successes encountered
|
||||
warnings: integer count of warnings encountered
|
||||
skips: integer count of skips encountered
|
||||
successful: Whether the checks were successful (no errors)
|
||||
|
||||
They must also provide these methods:
|
||||
|
||||
write_line(message=''): writes a message to stdout
|
||||
write_stderr_line(message=''): writes a message to stderr
|
||||
success(message): reports and registers a successful check
|
||||
error(message): reports and registers an error
|
||||
warn(message); reports and registers a warning
|
||||
skip(message): reports and registers a skipped check
|
||||
section(title): A context manager that starts a new section. For the
|
||||
Section API see FileSectionWrapper
|
||||
"""
|
||||
|
||||
def __init__(self, stdout, stderr):
|
||||
self.stdout = stdout
|
||||
self.stderr = stderr
|
||||
self.section_wrapper = FileSectionWrapper
|
||||
self.errors = 0
|
||||
self.successes = 0
|
||||
self.warnings = 0
|
||||
self.skips = 0
|
||||
|
||||
def colorize(self, msg, opts=(), **kwargs):
|
||||
return colorize(msg, opts=opts, **kwargs)
|
||||
|
||||
def write_line(self, message=""):
|
||||
self.write("%s\n" % message)
|
||||
|
||||
def write(self, message):
|
||||
self.stdout.write(message)
|
||||
|
||||
def write_stderr_line(self, message=""):
|
||||
self.write_stderr("%s\n" % message)
|
||||
|
||||
def write_stderr(self, message):
|
||||
self.stderr.write(message)
|
||||
|
||||
def success(self, message):
|
||||
self.successes += 1
|
||||
self.write_line("{} {}".format(message, self.colorize("[OK]", fg="green", opts=["bold"])))
|
||||
|
||||
def error(self, message):
|
||||
self.errors += 1
|
||||
self.write_stderr_line("{} {}".format(message, self.colorize("[ERROR]", fg="red", opts=["bold"])))
|
||||
|
||||
def warn(self, message):
|
||||
self.warnings += 1
|
||||
self.write_stderr_line("{} {}".format(message, self.colorize("[WARNING]", fg="yellow", opts=["bold"])))
|
||||
|
||||
def skip(self, message):
|
||||
self.skips += 1
|
||||
self.write_line("{} {}".format(message, self.colorize("[SKIP]", fg="blue", opts=["bold"])))
|
||||
|
||||
@method_decorator(contextmanager)
|
||||
def section(self, title):
|
||||
self.write_line(self.colorize(title, opts=["bold"]))
|
||||
self.write_line(self.colorize("=" * len(title), opts=["bold"]))
|
||||
self.write_line()
|
||||
wrapper = self.section_wrapper(self)
|
||||
try:
|
||||
yield wrapper
|
||||
except: # NOQA
|
||||
self.error("Checker failed, see traceback")
|
||||
raise
|
||||
self.errors += wrapper.errors
|
||||
self.successes += wrapper.successes
|
||||
self.warnings += wrapper.warnings
|
||||
self.skips += wrapper.skips
|
||||
self.write_line("")
|
||||
|
||||
@property
|
||||
def successful(self):
|
||||
return not self.errors
|
||||
|
||||
|
||||
class FileSectionWrapper(FileOutputWrapper):
|
||||
"""
|
||||
Used from FileOutputWrapper to report checks in a section.
|
||||
|
||||
If you want to provide your own output class, you may want to subclass
|
||||
this class for the section reporting too. If you want to use your own,
|
||||
you must defined at least the same API as FileOutputWrapper, as well
|
||||
as these four additional methods:
|
||||
|
||||
finish_success(message): End the section (successfully)
|
||||
finish_error(message): End the section with errors
|
||||
finish_warning(message): End this section with a warning
|
||||
finish_skip(message): End this (skipped) section
|
||||
"""
|
||||
|
||||
def __init__(self, wrapper):
|
||||
super().__init__(wrapper.stdout, wrapper.stderr)
|
||||
self.wrapper = wrapper
|
||||
|
||||
def write_line(self, message=""):
|
||||
self.write(" - %s\n" % message)
|
||||
|
||||
def write_stderr_line(self, message=""):
|
||||
self.write_stderr(" - %s\n" % message)
|
||||
|
||||
def finish_success(self, message):
|
||||
self.wrapper.write_line()
|
||||
self.wrapper.success(message)
|
||||
|
||||
def finish_error(self, message):
|
||||
self.wrapper.write_line()
|
||||
self.wrapper.error(message)
|
||||
|
||||
def finish_warning(self, message):
|
||||
self.wrapper.write_line()
|
||||
self.wrapper.warn(message)
|
||||
|
||||
def finish_skip(self, message):
|
||||
self.wrapper.write_line()
|
||||
self.wrapper.skip(message)
|
||||
|
||||
|
||||
def define_check(func):
|
||||
"""
|
||||
Helper decorator to register a check function.
|
||||
"""
|
||||
CHECKERS.append(func)
|
||||
return func
|
||||
|
||||
|
||||
@define_check
|
||||
def check_sekizai(output):
|
||||
with output.section("Sekizai") as section:
|
||||
sekizai_installed = is_installed("sekizai")
|
||||
|
||||
if sekizai_installed:
|
||||
section.success("Sekizai is installed")
|
||||
else:
|
||||
section.error("Sekizai is not installed, could not find 'sekizai' in INSTALLED_APPS")
|
||||
processors = list(
|
||||
chain(*[template["OPTIONS"].get("context_processors", []) for template in settings.TEMPLATES])
|
||||
)
|
||||
if "sekizai.context_processors.sekizai" in processors:
|
||||
section.success("Sekizai template context processor is installed")
|
||||
else:
|
||||
section.error(
|
||||
"Sekizai template context processor is not installed, could not find "
|
||||
"'sekizai.context_processors.sekizai' in TEMPLATES option context_processors"
|
||||
)
|
||||
|
||||
if not sekizai_installed:
|
||||
# sekizai is not installed.
|
||||
# we can't reliable check templates
|
||||
# because template loading won't work
|
||||
return
|
||||
|
||||
for template, _ in get_cms_setting("TEMPLATES"):
|
||||
if template == constants.TEMPLATE_INHERITANCE_MAGIC:
|
||||
continue
|
||||
if validate_template(template, ["js", "css"]):
|
||||
section.success("Sekizai namespaces 'js' and 'css' found in %r" % template)
|
||||
else:
|
||||
section.error("Sekizai namespaces 'js' and 'css' not found in %r" % template)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_i18n(output):
|
||||
with output.section("Internationalization") as section:
|
||||
if isinstance(getattr(settings, "CMS_LANGUAGES", {}), dict):
|
||||
section.success("New style CMS_LANGUAGES")
|
||||
else:
|
||||
section.warn("Old style (tuple based) CMS_LANGUAGES, please switch to the new (dictionary based) style")
|
||||
if getattr(settings, "LANGUAGE_CODE", "").find("_") > -1:
|
||||
section.warn(
|
||||
"LANGUAGE_CODE must contain a valid language code, not a locale (e.g.: 'en-us' instead of "
|
||||
"'en_US'): '%s' provided" % getattr(settings, "LANGUAGE_CODE", "")
|
||||
)
|
||||
for lang in getattr(settings, "LANGUAGES", ()):
|
||||
if lang[0].find("_") > -1:
|
||||
section.warn(
|
||||
"LANGUAGES must contain valid language codes, not locales (e.g.: 'en-us' instead of "
|
||||
"'en_US'): '%s' provided" % lang[0]
|
||||
)
|
||||
site_id = getattr(settings, "SITE_ID", None)
|
||||
if site_id == hash(site_id):
|
||||
for site, items in get_cms_setting("LANGUAGES").items():
|
||||
if isinstance(site, int):
|
||||
for lang in items:
|
||||
if lang["code"].find("_") > -1:
|
||||
section.warn(
|
||||
"CMS_LANGUAGES entries must contain valid language codes, not locales (e.g.: "
|
||||
"'en-us' instead of 'en_US'): '%s' provided" % lang["code"]
|
||||
)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_site_id(output):
|
||||
with output.section("Site configuration") as section:
|
||||
if not is_installed("django.contrib.sites"):
|
||||
section.warn("django.contrib.sites is not in INSTALLED_APPS")
|
||||
else:
|
||||
section.success("django.contrib.sites is in INSTALLED_APPS")
|
||||
site_id = getattr(settings, "SITE_ID", None)
|
||||
if site_id is None:
|
||||
section.warn("SITE_ID is not set - site identifictation will fall back to request host header or custom middleware")
|
||||
elif site_id != hash(site_id):
|
||||
section.error("SITE_ID must be an integer, not %r" % site_id)
|
||||
else:
|
||||
section.success("SITE_ID is set to %r" % site_id)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_middlewares(output):
|
||||
with output.section("Middlewares") as section:
|
||||
required_middlewares = (
|
||||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||||
"django.middleware.csrf.CsrfViewMiddleware",
|
||||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||||
"django.contrib.messages.middleware.MessageMiddleware",
|
||||
"django.middleware.locale.LocaleMiddleware",
|
||||
"django.middleware.common.CommonMiddleware",
|
||||
"cms.middleware.user.CurrentUserMiddleware",
|
||||
"cms.middleware.page.CurrentPageMiddleware",
|
||||
"cms.middleware.toolbar.ToolbarMiddleware",
|
||||
"cms.middleware.language.LanguageCookieMiddleware",
|
||||
)
|
||||
middlewares = getattr(settings, "MIDDLEWARE", [])
|
||||
|
||||
for middleware in required_middlewares:
|
||||
if middleware not in middlewares:
|
||||
section.error("%s middleware must be in MIDDLEWARE" % middleware)
|
||||
else:
|
||||
section.success("%s middleware found" % middleware)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_context_processors(output):
|
||||
with output.section("Context processors") as section:
|
||||
processors = list(
|
||||
chain(*[template["OPTIONS"].get("context_processors", []) for template in settings.TEMPLATES])
|
||||
)
|
||||
required_processors = ("cms.context_processors.cms_settings", "django.template.context_processors.i18n")
|
||||
for processor in required_processors:
|
||||
if processor not in processors:
|
||||
section.error("%s context processor must be in TEMPLATES option context_processors" % processor)
|
||||
else:
|
||||
section.success("%s context processor found" % processor)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_plugin_instances(output):
|
||||
from cms.management.commands.subcommands.list import plugin_report
|
||||
|
||||
with output.section("Plugin instances") as section:
|
||||
# get the report
|
||||
report = plugin_report()
|
||||
section.success("Plugin instances of %s types found in the database" % len(report))
|
||||
# loop over plugin types in the report
|
||||
for plugin_type in report:
|
||||
# warn about those that are not installed
|
||||
if not plugin_type["model"]:
|
||||
section.error("%s has instances but is no longer installed" % plugin_type["type"])
|
||||
# warn about those that have unsaved instances
|
||||
if plugin_type["unsaved_instances"]:
|
||||
section.error(
|
||||
"{} has {} unsaved instances".format(plugin_type["type"], len(plugin_type["unsaved_instances"]))
|
||||
)
|
||||
|
||||
if not section.successful:
|
||||
section.finish_error(
|
||||
"There are potentially serious problems with the plugins in your database. \nEven if "
|
||||
"your site works, you should run the 'manage.py cms list plugins' \ncommand and then "
|
||||
"the 'manage.py cms delete-orphaned-plugins' command. \nThis will alter your "
|
||||
"database; read the documentation before using it."
|
||||
)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_copy_relations(output):
|
||||
from cms.extensions import extension_pool
|
||||
from cms.extensions.models import BaseExtension
|
||||
from cms.models.pluginmodel import CMSPlugin
|
||||
from cms.plugin_pool import plugin_pool
|
||||
|
||||
def c_to_s(klass):
|
||||
return f"{klass.__module__}.{klass.__name__}"
|
||||
|
||||
def get_class(method_name, model):
|
||||
for cls in inspect.getmro(model):
|
||||
if method_name in cls.__dict__:
|
||||
return cls
|
||||
return None
|
||||
|
||||
with output.section('Presence of "copy_relations"') as section:
|
||||
plugin_pool.discover_plugins()
|
||||
for plugin in plugin_pool.plugins.values():
|
||||
plugin_class = plugin.model
|
||||
if get_class("copy_relations", plugin_class) is not CMSPlugin or plugin_class is CMSPlugin:
|
||||
# this class defines a ``copy_relations`` method, nothing more
|
||||
# to do
|
||||
continue
|
||||
for rel in plugin_class._meta.many_to_many:
|
||||
section.warn(
|
||||
f'{c_to_s(plugin_class)} has a many-to-many relation to {c_to_s(rel.model)},\n but no "copy_relations" method defined.'
|
||||
)
|
||||
for rel in plugin_class._get_related_objects():
|
||||
if (
|
||||
rel.model != CMSPlugin
|
||||
and not issubclass(rel.model, plugin.model)
|
||||
and rel.model != AliasPluginModel
|
||||
):
|
||||
section.warn(
|
||||
f'{c_to_s(plugin_class)} has a foreign key from {c_to_s(rel.model)},\n but no "copy_relations" method defined.'
|
||||
)
|
||||
|
||||
for extension in chain(extension_pool.page_extensions, extension_pool.page_content_extensions):
|
||||
if get_class("copy_relations", extension) is not BaseExtension:
|
||||
# OK, looks like there is a 'copy_relations' defined in the
|
||||
# extension... move along...
|
||||
continue
|
||||
for rel in extension._meta.many_to_many:
|
||||
section.warn(
|
||||
"%s has a many-to-many relation to %s,\n "
|
||||
'but no "copy_relations" method defined.'
|
||||
% (
|
||||
c_to_s(extension),
|
||||
c_to_s(rel.remote_field.model),
|
||||
)
|
||||
)
|
||||
for rel in extension._get_related_objects():
|
||||
if rel.model != extension:
|
||||
section.warn(
|
||||
f'{c_to_s(extension)} has a foreign key from {c_to_s(rel.model)},\n but no "copy_relations" method defined.'
|
||||
)
|
||||
|
||||
if not section.warnings:
|
||||
section.finish_success(
|
||||
'All plugins and page/page content extensions have "copy_relations" method if needed.'
|
||||
)
|
||||
else:
|
||||
section.finish_success(
|
||||
'Some plugins or page/page content extensions do not define a "copy_relations" method.\n'
|
||||
"This might lead to data loss when publishing or copying plugins/extensions.\n"
|
||||
"See https://docs.django-cms.org/en/latest/how_to/09-custom_plugins.html#handling-relations or " # noqa
|
||||
"https://docs.django-cms.org/en/latest/how_to/18-extending_page_contents.html#handling-relations."
|
||||
)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_template_conf(output):
|
||||
with output.section("Template configuration") as section:
|
||||
if get_cms_setting("TEMPLATES"):
|
||||
if isinstance(get_cms_setting("TEMPLATES"), (list, tuple)):
|
||||
for template in get_cms_setting("TEMPLATES"):
|
||||
if not isinstance(template, (list, tuple)):
|
||||
section.error("CMS_TEMPLATES setting contains a non-list/tuple entry")
|
||||
elif len(template) != 2:
|
||||
section.error("CMS_TEMPLATES setting contains a list/tuple with != 2 entries")
|
||||
elif not isinstance(template[0], str):
|
||||
section.error("CMS_TEMPLATES contains a non-string entry")
|
||||
else:
|
||||
section.success("CMS_TEMPLATES_DIR or CMS_TEMPLATES setting found")
|
||||
else:
|
||||
section.error("CMS_TEMPLATES setting is not a list or tuple")
|
||||
if hasattr(settings, "CMS_PLACEHOLDERS"):
|
||||
section.warn("CMS_PLACEHOLDERS setting is also present but will be ignored.")
|
||||
elif get_cms_setting("PLACEHOLDERS"):
|
||||
if isinstance(get_cms_setting("PLACEHOLDERS"), (list, tuple)):
|
||||
for placeholder in get_cms_setting("PLACEHOLDERS"):
|
||||
if not isinstance(placeholder, (list, tuple)):
|
||||
section.error("CMS_PLACEHOLDERS setting contains a non-list/tuple entry")
|
||||
elif not isinstance(placeholder[0], str):
|
||||
section.error(
|
||||
f"CMS_PLACEHOLDERS contains an entry with a non-string identifier: {placeholder[0]}"
|
||||
)
|
||||
else:
|
||||
section.success("CMS_PLACEHOLDERS setting entry found - CMS will run in headless mode")
|
||||
else:
|
||||
section.error("CMS_PLACEHOLDERS setting is not a list or tuple")
|
||||
else:
|
||||
section.warn(
|
||||
"Both CMS_TEMPLATES and CMS_PLACEHOLDERS settings are missing. "
|
||||
'Will run in headless mode with one placeholder called "content"'
|
||||
)
|
||||
|
||||
|
||||
@define_check
|
||||
def check_cmsapps_names(output):
|
||||
from cms.apphook_pool import apphook_pool
|
||||
|
||||
with output.section("Apphooks") as section:
|
||||
apphooks = apphook_pool.get_apphooks()
|
||||
for hook, name in apphooks:
|
||||
if apphook_pool.get_apphook(hook).name is None:
|
||||
section.warn("CMSApps should define a name. %s doesn't have a name" % name)
|
||||
else:
|
||||
section.success("CMSApp %s has a name defined" % name)
|
||||
if not apphooks:
|
||||
section.finish_success("CMSApps configuration is okay")
|
||||
|
||||
|
||||
@define_check
|
||||
def check_plugin_parent_child_relations(output):
|
||||
from cms.plugin_pool import plugin_pool
|
||||
|
||||
with output.section("Plugin parent/child relations") as section:
|
||||
plugin_pool.discover_plugins()
|
||||
for plugin in plugin_pool.plugins.values():
|
||||
# Check parent_classes
|
||||
if hasattr(plugin, "parent_classes") and plugin.parent_classes:
|
||||
for parent_class in plugin.parent_classes:
|
||||
if parent_class not in ("0", "") and parent_class not in plugin_pool.plugins:
|
||||
section.warn(f"Parent plugin class '{parent_class}' not found for plugin '{plugin.__name__}'")
|
||||
|
||||
# Check child_classes
|
||||
if hasattr(plugin, "child_classes") and plugin.child_classes:
|
||||
for child_class in plugin.child_classes:
|
||||
if child_class not in plugin_pool.plugins:
|
||||
section.warn(f"Child plugin class '{child_class}' not found for plugin '{plugin.__name__}'")
|
||||
|
||||
if section.successful:
|
||||
section.finish_success("Plugin parent/child relations are valid")
|
||||
else:
|
||||
section.finish_warning(
|
||||
"Some plugins have invalid parent/child class configurations.\n"
|
||||
"This might cause unexpected behavior in the admin interface."
|
||||
)
|
||||
|
||||
|
||||
def check(output):
|
||||
"""
|
||||
Checks the configuration/environment of this django CMS installation.
|
||||
|
||||
'output' should be an object that provides the same API as FileOutputWrapper.
|
||||
|
||||
Returns whether the configuration/environment are okay (has no errors)
|
||||
"""
|
||||
import cms
|
||||
|
||||
title = f"Checking django CMS {cms.__version__} installation"
|
||||
border = "*" * len(title)
|
||||
output.write_line(output.colorize(border, opts=["bold"]))
|
||||
output.write_line(output.colorize(title, opts=["bold"]))
|
||||
output.write_line(output.colorize(border, opts=["bold"]))
|
||||
output.write_line()
|
||||
for checker in CHECKERS:
|
||||
checker(output)
|
||||
output.write_line()
|
||||
with output.section("OVERALL RESULTS"):
|
||||
if output.errors:
|
||||
output.write_stderr_line(output.colorize("%s errors!" % output.errors, opts=["bold"], fg="red"))
|
||||
if output.warnings:
|
||||
output.write_stderr_line(output.colorize("%s warnings!" % output.warnings, opts=["bold"], fg="yellow"))
|
||||
if output.skips:
|
||||
output.write_line(output.colorize("%s checks skipped!" % output.skips, opts=["bold"], fg="blue"))
|
||||
output.write_line(output.colorize("%s checks successful!" % output.successes, opts=["bold"], fg="green"))
|
||||
output.write_line()
|
||||
if output.errors:
|
||||
output.write_stderr_line(output.colorize("Please check the errors above", opts=["bold"], fg="red"))
|
||||
elif output.warnings:
|
||||
output.write_stderr_line(
|
||||
output.colorize("Installation okay, but please check warnings above", opts=["bold"], fg="yellow")
|
||||
)
|
||||
else:
|
||||
output.write_line(output.colorize("Installation okay", opts=["bold"], fg="green"))
|
||||
return output.successful
|
||||
20
cms/utils/compat/__init__.py
Normal file
20
cms/utils/compat/__init__.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
from platform import python_version
|
||||
|
||||
from django import get_version
|
||||
from packaging.version import Version
|
||||
|
||||
DJANGO_VERSION = get_version()
|
||||
PYTHON_VERSION = python_version()
|
||||
|
||||
# These mean "less than or equal to DJANGO_FOO_BAR"
|
||||
# Kept for backward compatibility, none of them used currently
|
||||
DJANGO_2_2 = Version(DJANGO_VERSION) < Version('3.0')
|
||||
DJANGO_3_0 = Version(DJANGO_VERSION) < Version('3.1')
|
||||
DJANGO_3_1 = Version(DJANGO_VERSION) < Version('3.2')
|
||||
DJANGO_3_2 = Version(DJANGO_VERSION) < Version('3.3')
|
||||
DJANGO_3 = Version(DJANGO_VERSION) < Version('4.0')
|
||||
DJANGO_4_1 = Version(DJANGO_VERSION) < Version('4.2')
|
||||
DJANGO_4_2 = Version(DJANGO_VERSION) < Version('4.3')
|
||||
DJANGO_5_1 = Version(DJANGO_VERSION) < Version('5.2') # To allow testing against django's main branch
|
||||
|
||||
# Currently used version markers:
|
||||
17
cms/utils/compat/dj.py
Normal file
17
cms/utils/compat/dj.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from functools import WRAPPER_ASSIGNMENTS
|
||||
|
||||
from django.apps import apps
|
||||
|
||||
__all__ = ['is_installed', 'installed_apps']
|
||||
|
||||
|
||||
def is_installed(app_name):
|
||||
return apps.is_installed(app_name)
|
||||
|
||||
|
||||
def installed_apps():
|
||||
return [app.name for app in apps.get_app_configs()]
|
||||
|
||||
|
||||
def available_attrs(fn):
|
||||
return WRAPPER_ASSIGNMENTS
|
||||
42
cms/utils/compat/forms.py
Normal file
42
cms/utils/compat/forms.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
import importlib
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
|
||||
# override with custom classes if they exist
|
||||
if settings.AUTH_USER_MODEL != "auth.User": # pragma: no cover
|
||||
# UserAdmin class
|
||||
user_app_name = settings.AUTH_USER_MODEL.split(".")[0]
|
||||
app = apps.get_app_config(user_app_name).models_module
|
||||
|
||||
try:
|
||||
custom_admin = importlib.import_module(app.__name__[:-6] + "admin")
|
||||
|
||||
if hasattr(custom_admin, "UserAdmin"):
|
||||
UserAdmin = custom_admin.UserAdmin
|
||||
else:
|
||||
from django.contrib.auth.admin import UserAdmin
|
||||
except ImportError:
|
||||
from django.contrib.auth.admin import UserAdmin # nopyflakes
|
||||
|
||||
# user form classes
|
||||
try:
|
||||
custom_forms = importlib.import_module(app.__name__[:-6] + "forms")
|
||||
|
||||
if hasattr(custom_forms, "UserCreationForm"):
|
||||
UserCreationForm = custom_forms.UserCreationForm
|
||||
else:
|
||||
from django.contrib.auth.forms import UserCreationForm
|
||||
|
||||
if hasattr(custom_forms, "UserChangeForm"):
|
||||
UserChangeForm = custom_forms.UserChangeForm
|
||||
else:
|
||||
from django.contrib.auth.forms import UserChangeForm
|
||||
except ImportError:
|
||||
from django.contrib.auth.forms import (
|
||||
UserChangeForm, # nopyflakes
|
||||
UserCreationForm, # nopyflakes
|
||||
)
|
||||
else:
|
||||
from django.contrib.auth.admin import UserAdmin # noqa: F401
|
||||
from django.contrib.auth.forms import UserChangeForm, UserCreationForm # noqa: F401
|
||||
11
cms/utils/compat/response.py
Normal file
11
cms/utils/compat/response.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
def get_response_headers(response):
|
||||
"""
|
||||
Django 3.2+ comes with changes to the response headers and the _headers is not present
|
||||
in the newer versions. So this method checks if it's present in the response or not, else
|
||||
returns the newer attribute headers from the response.
|
||||
"""
|
||||
if hasattr(response, '_headers'):
|
||||
response_headers = response._headers
|
||||
else:
|
||||
response_headers = response.headers
|
||||
return response_headers
|
||||
2
cms/utils/compat/warnings.py
Normal file
2
cms/utils/compat/warnings.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
class RemovedInDjangoCMS60Warning(DeprecationWarning):
|
||||
pass
|
||||
336
cms/utils/conf.py
Normal file
336
cms/utils/conf.py
Normal file
|
|
@ -0,0 +1,336 @@
|
|||
import os
|
||||
from functools import update_wrapper
|
||||
from typing import Any
|
||||
from urllib.parse import urljoin
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.cache import caches
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.connection import ConnectionProxy
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from cms import __version__, constants
|
||||
|
||||
__all__ = [
|
||||
'get_cms_setting',
|
||||
'get_site_id'
|
||||
]
|
||||
|
||||
|
||||
class VERIFIED:
|
||||
pass # need a unique identifier for CMS_LANGUAGES
|
||||
|
||||
|
||||
def _load_from_file(module_path):
|
||||
"""
|
||||
Load a python module from its absolute filesystem path
|
||||
"""
|
||||
from importlib.machinery import SourceFileLoader
|
||||
|
||||
imported = None
|
||||
if module_path:
|
||||
imported = SourceFileLoader("mod", module_path).load_module()
|
||||
return imported
|
||||
|
||||
|
||||
def default(name):
|
||||
def decorator(wrapped):
|
||||
def wrapper():
|
||||
if hasattr(settings, name):
|
||||
return getattr(settings, name)
|
||||
return wrapped()
|
||||
|
||||
update_wrapper(wrapper, wrapped)
|
||||
return wrapper
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
DEFAULTS = {
|
||||
'TEMPLATE_INHERITANCE': True,
|
||||
'DEFAULT_X_FRAME_OPTIONS': constants.X_FRAME_OPTIONS_INHERIT,
|
||||
'TOOLBAR_SIMPLE_STRUCTURE_MODE': True,
|
||||
'PLACEHOLDER_CONF': {},
|
||||
'PLACEHOLDERS': (('', ('content',), _("Single placeholder")),),
|
||||
'PERMISSION': False,
|
||||
# Whether to use raw ID lookups for users when PERMISSION is True
|
||||
'RAW_ID_USERS': False,
|
||||
'PUBLIC_FOR': 'all',
|
||||
'APPHOOKS': [],
|
||||
'TOOLBARS': [],
|
||||
'SITE_CHOICES_CACHE_KEY': 'CMS:site_choices',
|
||||
'PAGE_CHOICES_CACHE_KEY': 'CMS:page_choices',
|
||||
'MEDIA_PATH': 'cms/',
|
||||
'PAGE_MEDIA_PATH': 'cms_page_media/',
|
||||
'TITLE_CHARACTER': '+',
|
||||
'PAGE_CACHE': True,
|
||||
'PLACEHOLDER_CACHE': True,
|
||||
'PLUGIN_CACHE': True,
|
||||
'MENU_CACHE_BACKEND': 'default',
|
||||
'CACHE_PREFIX': f'cms_{__version__}_',
|
||||
'PLUGIN_PROCESSORS': [],
|
||||
'PLUGIN_CONTEXT_PROCESSORS': [],
|
||||
'UNIHANDECODE_VERSION': None,
|
||||
'UNIHANDECODE_DECODERS': ['ja', 'zh', 'kr', 'vn', 'diacritic'],
|
||||
'UNIHANDECODE_DEFAULT_DECODER': 'diacritic',
|
||||
'TOOLBAR_ANONYMOUS_ON': True,
|
||||
'TOOLBAR_URL__PERSIST': 'persist',
|
||||
'TOOLBAR_URL__DISABLE': 'toolbar_off',
|
||||
'TOOLBAR_URL__ENABLE': 'toolbar_on',
|
||||
'ADMIN_NAMESPACE': 'admin',
|
||||
'APP_NAME': None,
|
||||
'TOOLBAR_HIDE': False,
|
||||
'INTERNAL_IPS': [],
|
||||
'REQUEST_IP_RESOLVER': 'cms.utils.request_ip_resolvers.default_request_ip_resolver',
|
||||
'PAGE_WIZARD_DEFAULT_TEMPLATE': constants.TEMPLATE_INHERITANCE_MAGIC,
|
||||
'PAGE_WIZARD_CONTENT_PLUGIN': 'TextPlugin',
|
||||
'PAGE_WIZARD_CONTENT_PLUGIN_BODY': 'body',
|
||||
'PAGE_WIZARD_CONTENT_PLACEHOLDER': None, # Use first placeholder it finds.
|
||||
'SIDEFRAME_ENABLED': True,
|
||||
'ENDPOINT_LIVE_URL_QUERYSTRING_PARAM_ENABLED': False,
|
||||
'ENDPOINT_LIVE_URL_QUERYSTRING_PARAM': "live-url",
|
||||
'REDIRECT_PRESERVE_QUERY_PARAMS': False,
|
||||
'REDIRECT_TO_LOWERCASE_SLUG': False,
|
||||
'HIDE_LEGACY_FEATURES': True,
|
||||
'COLOR_SCHEME': 'auto',
|
||||
'COLOR_SCHEME_TOGGLE': True,
|
||||
'CATCH_PLUGIN_500_EXCEPTION': True,
|
||||
'DEFAULT_IN_NAVIGATION': True,
|
||||
'ALWAYS_REFRESH_CONTENT': False,
|
||||
'ENABLE_HELP': True, # Adds help menu toolbar
|
||||
'EXTRA_HELP_MENU_ITEMS': (),
|
||||
'HELP_MENU_ITEMS': (
|
||||
(_('Community forum'), 'https://discourse.django-cms.org/'),
|
||||
(_('Documentation'), 'https://docs.django-cms.org/en/latest/'),
|
||||
(_('Getting started'), 'https://www.django-cms.org/en/get-started-django-cms/'),
|
||||
(_('Talk to us'), 'https://www.django-cms.org/en/support/'),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
def get_cache_durations():
|
||||
"""
|
||||
Returns the setting: CMS_CACHE_DURATIONS or the defaults.
|
||||
"""
|
||||
return getattr(settings, 'CMS_CACHE_DURATIONS', {
|
||||
'menus': 60 * 60,
|
||||
'content': 60,
|
||||
'permissions': 60 * 60,
|
||||
})
|
||||
|
||||
|
||||
def get_menu_cache():
|
||||
return ConnectionProxy(caches, get_cms_setting('MENU_CACHE_BACKEND'))
|
||||
|
||||
|
||||
@default('CMS_MEDIA_ROOT')
|
||||
def get_media_root():
|
||||
return os.path.join(settings.MEDIA_ROOT, get_cms_setting('MEDIA_PATH'))
|
||||
|
||||
|
||||
@default('CMS_MEDIA_URL')
|
||||
def get_media_url():
|
||||
return urljoin(settings.MEDIA_URL, get_cms_setting('MEDIA_PATH'))
|
||||
|
||||
|
||||
@default('CMS_TOOLBAR_URL__PERSIST')
|
||||
def get_toolbar_url__persist():
|
||||
return get_cms_setting('TOOLBAR_URL__PERSIST')
|
||||
|
||||
|
||||
@default('CMS_TOOLBAR_URL__DISABLE')
|
||||
def get_toolbar_url__disable():
|
||||
return get_cms_setting('TOOLBAR_URL__DISABLE')
|
||||
|
||||
|
||||
@default('CMS_TOOLBAR_URL__ENABLE')
|
||||
def get_toolbar_url__enable():
|
||||
return get_cms_setting('TOOLBAR_URL__ENABLE')
|
||||
|
||||
|
||||
def get_templates() -> list[tuple[str, Any]]:
|
||||
if getattr(settings, 'CMS_TEMPLATES_DIR', False):
|
||||
tpldir = getattr(settings, 'CMS_TEMPLATES_DIR', False)
|
||||
# CMS_TEMPLATES_DIR can either be a string pointing to the templates directory
|
||||
# or a dictionary holding 'site: template dir' entries
|
||||
if isinstance(tpldir, dict):
|
||||
tpldir = tpldir[settings.SITE_ID]
|
||||
# We must extract the relative path of CMS_TEMPLATES_DIR to the nearest
|
||||
# valid templates' directory. Here we mimic what the filesystem and
|
||||
# app_directories template loaders do
|
||||
prefix = ''
|
||||
# Relative to TEMPLATE['DIRS'] for filesystem loader
|
||||
|
||||
path = [template['DIRS'][0] for template in settings.TEMPLATES]
|
||||
|
||||
for basedir in path:
|
||||
if tpldir.find(basedir) == 0:
|
||||
prefix = tpldir.replace(basedir + os.sep, '')
|
||||
break
|
||||
# Relative to 'templates' directory that app_directory scans
|
||||
if not prefix:
|
||||
components = tpldir.split(os.sep)
|
||||
try:
|
||||
prefix = os.path.join(*components[components.index('templates') + 1:])
|
||||
except ValueError:
|
||||
# If templates is not found we use the directory name as prefix
|
||||
# and hope for the best
|
||||
prefix = os.path.basename(tpldir)
|
||||
config_path = os.path.join(tpldir, '__init__.py')
|
||||
# Try to load templates list and names from the template module
|
||||
# If module file is not present skip configuration and just dump the filenames as templates
|
||||
if os.path.isfile(config_path):
|
||||
template_module = _load_from_file(config_path)
|
||||
templates = [
|
||||
(os.path.join(prefix, data[0].strip()), data[1]) for data in template_module.TEMPLATES.items()
|
||||
]
|
||||
else:
|
||||
templates = list((os.path.join(prefix, tpl), tpl) for tpl in os.listdir(tpldir))
|
||||
else:
|
||||
templates = list(getattr(settings, 'CMS_TEMPLATES', []))
|
||||
if get_cms_setting('TEMPLATE_INHERITANCE') and templates:
|
||||
templates.append((constants.TEMPLATE_INHERITANCE_MAGIC, _('Inherit the template of the nearest ancestor')))
|
||||
return templates
|
||||
|
||||
|
||||
def get_placeholders():
|
||||
if getattr(settings, 'CMS_PLACEHOLDERS', False):
|
||||
return settings.CMS_PLACEHOLDERS
|
||||
if getattr(settings, 'CMS_TEMPLATES', False) or getattr(settings, 'CMS_TEMPLATES_DIR', False):
|
||||
return ()
|
||||
return DEFAULTS['PLACEHOLDERS']
|
||||
|
||||
|
||||
def _ensure_languages_settings(languages):
|
||||
valid_language_keys = ['code', 'name', 'fallbacks', 'hide_untranslated', 'redirect_on_fallback', 'public']
|
||||
required_language_keys = ['code', 'name']
|
||||
simple_defaults = ['public', 'redirect_on_fallback', 'hide_untranslated']
|
||||
|
||||
if not isinstance(languages, dict):
|
||||
raise ImproperlyConfigured(
|
||||
"CMS_LANGUAGES must be a dictionary with site IDs and 'default'"
|
||||
" as keys. Please check the format.")
|
||||
|
||||
defaults = languages.pop('default', {})
|
||||
default_fallbacks = defaults.get('fallbacks')
|
||||
needs_fallbacks = []
|
||||
|
||||
for key in defaults:
|
||||
if key not in valid_language_keys:
|
||||
raise ImproperlyConfigured("CMS_LANGUAGES has an invalid property in the default properties: %s" % key)
|
||||
|
||||
for key in simple_defaults:
|
||||
if key not in defaults:
|
||||
defaults[key] = True
|
||||
|
||||
for site, language_list in languages.items():
|
||||
if site != hash(site):
|
||||
raise ImproperlyConfigured(
|
||||
"CMS_LANGUAGES can only be filled with integers (site IDs) and 'default'"
|
||||
" for default values. %s is not a valid key." % site)
|
||||
|
||||
for language_object in language_list:
|
||||
for required_key in required_language_keys:
|
||||
if required_key not in language_object:
|
||||
raise ImproperlyConfigured("CMS_LANGUAGES has a language which is missing the required key %r "
|
||||
"in site %r" % (key, site))
|
||||
language_code = language_object['code']
|
||||
for key in language_object:
|
||||
if key not in valid_language_keys:
|
||||
raise ImproperlyConfigured(
|
||||
f"CMS_LANGUAGES has invalid key {key!r} in language {language_code!r} in site {site!r}"
|
||||
)
|
||||
|
||||
if 'fallbacks' not in language_object:
|
||||
if default_fallbacks:
|
||||
language_object['fallbacks'] = default_fallbacks
|
||||
else:
|
||||
needs_fallbacks.append((site, language_object))
|
||||
for key in simple_defaults:
|
||||
if key not in language_object:
|
||||
language_object[key] = defaults[key]
|
||||
|
||||
site_fallbacks = {}
|
||||
for site, language_object in needs_fallbacks:
|
||||
if site not in site_fallbacks:
|
||||
site_fallbacks[site] = [lang['code'] for lang in languages[site] if lang['public']]
|
||||
language_object['fallbacks'] = [
|
||||
lang_code for lang_code in site_fallbacks[site] if lang_code != language_object['code']
|
||||
]
|
||||
|
||||
languages['default'] = defaults
|
||||
languages[VERIFIED] = True # this will be busted by @override_settings and cause a re-check
|
||||
|
||||
return languages
|
||||
|
||||
|
||||
def get_languages():
|
||||
site_settings = getattr(settings, 'SITE_ID', None)
|
||||
if site_settings and site_settings != hash(site_settings):
|
||||
raise ImproperlyConfigured(
|
||||
"SITE_ID must be an integer"
|
||||
)
|
||||
if not settings.USE_I18N:
|
||||
if site_settings:
|
||||
return _ensure_languages_settings(
|
||||
{site_settings: [{'code': settings.LANGUAGE_CODE, 'name': settings.LANGUAGE_CODE}]}
|
||||
)
|
||||
settings.LANGUAGES = ((settings.LANGUAGE_CODE, settings.LANGUAGE_CODE),)
|
||||
return {'default': {}, VERIFIED: True}
|
||||
|
||||
if settings.LANGUAGE_CODE not in dict(settings.LANGUAGES):
|
||||
raise ImproperlyConfigured(
|
||||
'LANGUAGE_CODE "%s" must have a matching entry in LANGUAGES' % settings.LANGUAGE_CODE
|
||||
)
|
||||
|
||||
if site_settings:
|
||||
languages = getattr(settings, 'CMS_LANGUAGES', {
|
||||
site_settings: [{'code': code, 'name': _(name)} for code, name in settings.LANGUAGES]
|
||||
})
|
||||
else:
|
||||
languages = {'default': {}, VERIFIED: True}
|
||||
if VERIFIED in languages:
|
||||
return languages
|
||||
return _ensure_languages_settings(languages)
|
||||
|
||||
|
||||
def get_unihandecode_host():
|
||||
host = getattr(settings, 'CMS_UNIHANDECODE_HOST', None)
|
||||
if not host:
|
||||
return host
|
||||
if host.endswith('/'):
|
||||
return host
|
||||
else:
|
||||
return host + '/'
|
||||
|
||||
|
||||
COMPLEX = {
|
||||
'CACHE_DURATIONS': get_cache_durations,
|
||||
'MEDIA_ROOT': get_media_root,
|
||||
'MEDIA_URL': get_media_url,
|
||||
# complex because not prefixed by CMS_
|
||||
'TEMPLATES': get_templates,
|
||||
'PLACEHOLDERS': get_placeholders,
|
||||
'LANGUAGES': get_languages,
|
||||
'UNIHANDECODE_HOST': get_unihandecode_host,
|
||||
'CMS_TOOLBAR_URL__PERSIST': get_toolbar_url__persist,
|
||||
'CMS_TOOLBAR_URL__DISABLE': get_toolbar_url__disable,
|
||||
'CMS_TOOLBAR_URL__ENABLE': get_toolbar_url__enable,
|
||||
}
|
||||
|
||||
|
||||
def get_cms_setting(name) -> Any:
|
||||
if name in COMPLEX:
|
||||
return COMPLEX[name]()
|
||||
return getattr(settings, 'CMS_%s' % name, DEFAULTS[name])
|
||||
|
||||
|
||||
def get_site_id(site):
|
||||
from django.contrib.sites.models import Site
|
||||
if isinstance(site, Site):
|
||||
return site.id
|
||||
try:
|
||||
return int(site)
|
||||
except (TypeError, ValueError):
|
||||
pass
|
||||
return getattr(settings, 'SITE_ID', None)
|
||||
74
cms/utils/decorators.py
Normal file
74
cms/utils/decorators.py
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
from urllib.parse import quote
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.views import redirect_to_login
|
||||
from django.http import Http404
|
||||
from django.urls import resolve
|
||||
|
||||
from cms.page_rendering import _handle_no_page
|
||||
from cms.utils import get_current_site
|
||||
from cms.utils.page_permissions import user_can_view_page
|
||||
|
||||
|
||||
def cms_perms(func):
|
||||
def inner(request, *args, **kwargs):
|
||||
page = request.current_page
|
||||
if page:
|
||||
if page.login_required and not request.user.is_authenticated:
|
||||
return redirect_to_login(quote(request.get_full_path()), settings.LOGIN_URL)
|
||||
site = get_current_site(request)
|
||||
if not user_can_view_page(request.user, page, site):
|
||||
return _handle_no_page(request)
|
||||
return func(request, *args, **kwargs)
|
||||
inner.__module__ = func.__module__
|
||||
inner.__doc__ = func.__doc__
|
||||
if hasattr(func, "view_class"):
|
||||
inner.view_class = func.view_class
|
||||
if hasattr(func, '__name__'):
|
||||
inner.__name__ = func.__name__
|
||||
elif hasattr(func, '__class__'):
|
||||
inner.__name__ = func.__class__.__name__
|
||||
|
||||
# for django.contrib.admindoc
|
||||
if hasattr(func, '__qualname__'):
|
||||
inner.__qualname__ = func.__qualname__
|
||||
elif hasattr(func, '__class__') and hasattr(func.__class__, '__qualname__'):
|
||||
inner.__qualname__ = func.__class__.__qualname__
|
||||
|
||||
if getattr(func, 'csrf_exempt', False):
|
||||
# view has set csrf_exempt flag
|
||||
# so pass it down to the decorator.
|
||||
inner.csrf_exempt = True
|
||||
return inner
|
||||
|
||||
|
||||
def cms_site_filter(site_id):
|
||||
def _cms_site_filter(func):
|
||||
def inner(request, *args, **kwargs):
|
||||
site = get_current_site(request)
|
||||
if site.id != site_id:
|
||||
match = resolve(request.path)
|
||||
raise Http404(dict(path=request.path, tried=match.tried))
|
||||
return func(request, *args, **kwargs)
|
||||
|
||||
inner.__module__ = func.__module__
|
||||
inner.__doc__ = func.__doc__
|
||||
if hasattr(func, "view_class"):
|
||||
inner.view_class = func.view_class
|
||||
if hasattr(func, '__name__'):
|
||||
inner.__name__ = func.__name__
|
||||
elif hasattr(func, '__class__'):
|
||||
inner.__name__ = func.__class__.__name__
|
||||
|
||||
# for django.contrib.admindoc
|
||||
if hasattr(func, '__qualname__'):
|
||||
inner.__qualname__ = func.__qualname__
|
||||
elif hasattr(func, '__class__') and hasattr(func.__class__, '__qualname__'):
|
||||
inner.__qualname__ = func.__class__.__qualname__
|
||||
|
||||
if getattr(func, 'csrf_exempt', False):
|
||||
# view has set csrf_exempt flag
|
||||
# so pass it down to the decorator.
|
||||
inner.csrf_exempt = True
|
||||
return inner
|
||||
return _cms_site_filter
|
||||
27
cms/utils/encoder.py
Normal file
27
cms/utils/encoder.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from django.core.serializers.json import DjangoJSONEncoder
|
||||
from django.utils.encoding import force_str
|
||||
from django.utils.functional import Promise
|
||||
from django.utils.html import conditional_escape
|
||||
|
||||
|
||||
class SafeJSONEncoder(DjangoJSONEncoder):
|
||||
def _recursive_escape(self, o, esc=conditional_escape):
|
||||
if isinstance(o, dict):
|
||||
return type(o)((esc(k), self._recursive_escape(v)) for (k, v) in dict.items(o))
|
||||
if isinstance(o, (list, tuple)):
|
||||
return type(o)(self._recursive_escape(v) for v in o)
|
||||
if isinstance(o, bool):
|
||||
return o
|
||||
try:
|
||||
return type(o)(esc(o))
|
||||
except (ValueError, TypeError):
|
||||
return self.default(o)
|
||||
|
||||
def encode(self, o):
|
||||
value = self._recursive_escape(o)
|
||||
return super().encode(value)
|
||||
|
||||
def default(self, o):
|
||||
if isinstance(o, Promise):
|
||||
return force_str(o)
|
||||
return super().default(o)
|
||||
116
cms/utils/helpers.py
Normal file
116
cms/utils/helpers.py
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
import re
|
||||
|
||||
from django.apps import apps
|
||||
from django.utils.encoding import force_str
|
||||
from django.utils.timezone import get_current_timezone_name
|
||||
|
||||
|
||||
def find_placeholder_relation(obj):
|
||||
return 'page'
|
||||
|
||||
|
||||
class classproperty:
|
||||
"""Like @property, but for classes, not just instances.
|
||||
|
||||
Example usage:
|
||||
|
||||
>>> from cms.utils.helpers import classproperty
|
||||
>>> class A():
|
||||
... @classproperty
|
||||
... def x(cls):
|
||||
... return 'x'
|
||||
... @property
|
||||
... def y(self):
|
||||
... return 'y'
|
||||
...
|
||||
>>> A.x
|
||||
'x'
|
||||
>>> A().x
|
||||
'x'
|
||||
>>> A.y
|
||||
<property object at 0x2939628>
|
||||
>>> A().y
|
||||
'y'
|
||||
|
||||
"""
|
||||
def __init__(self, fget):
|
||||
self.fget = fget
|
||||
|
||||
def __get__(self, owner_self, owner_cls):
|
||||
return self.fget(owner_cls)
|
||||
|
||||
|
||||
def normalize_name(name):
|
||||
"""
|
||||
Converts camel-case style names into underscore separated words. Example::
|
||||
|
||||
>>> normalize_name('oneTwoThree')
|
||||
'one_two_three'
|
||||
>>> normalize_name('FourFiveSix')
|
||||
'four_five_six'
|
||||
|
||||
taken from django.contrib.formtools
|
||||
"""
|
||||
new = re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', '_\\1', name)
|
||||
return new.lower().strip('_')
|
||||
|
||||
|
||||
def get_header_name(name):
|
||||
"""
|
||||
Returns "HTTP_HEADER_NAME" for "header-name" or "Header-Name", etc.
|
||||
Won't add "HTTP_" to input that already has it or for CONTENT_TYPE or
|
||||
CONTENT_LENGTH.
|
||||
"""
|
||||
uc_name = re.sub(r'\W+', '_', force_str(name)).upper()
|
||||
if uc_name in ['CONTENT_LENGTH', 'CONTENT_TYPE'] or uc_name.startswith('HTTP_'):
|
||||
return uc_name
|
||||
return f'HTTP_{uc_name}'
|
||||
|
||||
|
||||
def get_timezone_name():
|
||||
"""
|
||||
This returns a cross-platform compatible timezone name suitable for
|
||||
embedding into cache-keys. Its inclusion into cache-keys enables, but does
|
||||
not guarantee by itself, that dates are display correctly for the client's
|
||||
timezone. In order to complete this the project should use middleware or
|
||||
some other mechanism to affect's Django's get_current_timezone_name().
|
||||
"""
|
||||
# The datetime module doesn't restrict the output of tzname().
|
||||
# Windows is known to use non-standard, locale-dependant names.
|
||||
# User-defined tzinfo classes may return absolutely anything.
|
||||
# Hence this paranoid conversion to create a valid cache key.
|
||||
tz_name = force_str(get_current_timezone_name(), errors='ignore')
|
||||
return tz_name.encode('ascii', 'ignore').decode('ascii').replace(' ', '_')
|
||||
|
||||
|
||||
def is_editable_model(model_class):
|
||||
"""
|
||||
Return True if the model_class is editable.
|
||||
Checks whether the model_class has any relationships with Placeholder.
|
||||
If not, checks whether the model_class has an admin class
|
||||
and is inherited by FrontendEditableAdminMixin.
|
||||
:param model_class: The model class
|
||||
:return: Boolean
|
||||
"""
|
||||
from django.contrib import admin
|
||||
|
||||
from cms.admin.placeholderadmin import FrontendEditableAdminMixin
|
||||
from cms.models.placeholdermodel import Placeholder
|
||||
|
||||
# First check whether model_class has
|
||||
# any fields which has a relation to Placeholder
|
||||
extension = apps.get_app_config('cms').cms_extension
|
||||
if model_class in extension.toolbar_enabled_models:
|
||||
# Model needs to be toolbar enabled...
|
||||
for field in model_class._meta.get_fields():
|
||||
# ... and have a relationship with Placeholder
|
||||
if field.related_model == Placeholder:
|
||||
return True
|
||||
|
||||
# Check whether model_class has an admin class
|
||||
# and whether its inherited from FrontendEditableAdminMixin
|
||||
try:
|
||||
admin_class = admin.site._registry[model_class]
|
||||
except KeyError:
|
||||
return False
|
||||
return isinstance(admin_class, FrontendEditableAdminMixin) and hasattr(model_class, "get_absolute_url")
|
||||
255
cms/utils/i18n.py
Normal file
255
cms/utils/i18n.py
Normal file
|
|
@ -0,0 +1,255 @@
|
|||
from contextlib import contextmanager
|
||||
|
||||
from django.conf import settings
|
||||
from django.urls import LocalePrefixPattern, get_resolver
|
||||
from django.utils import translation
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from cms.exceptions import LanguageError
|
||||
from cms.utils.compat.warnings import RemovedInDjangoCMS60Warning
|
||||
from cms.utils.conf import get_cms_setting, get_site_id
|
||||
|
||||
|
||||
def _ensure_site_id(site_id, name):
|
||||
"""
|
||||
Ensure that the site_id is an integer.
|
||||
"""
|
||||
if site_id is None:
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
f"{name} called without specifying 'site_id'. This may lead to unexpected behavior. "
|
||||
f"Call {name} with 'site_id=<some_id>' instead.", RemovedInDjangoCMS60Warning, stacklevel=3
|
||||
)
|
||||
return get_site_id(None)
|
||||
return site_id
|
||||
|
||||
|
||||
@contextmanager
|
||||
def force_language(new_lang):
|
||||
old_lang = get_current_language()
|
||||
if old_lang != new_lang:
|
||||
translation.activate(new_lang)
|
||||
yield
|
||||
translation.activate(old_lang)
|
||||
|
||||
|
||||
def get_languages(site_id=None):
|
||||
site_id = _ensure_site_id(site_id, "get_languages")
|
||||
site_id = get_site_id(site_id)
|
||||
result = get_cms_setting('LANGUAGES').get(site_id)
|
||||
if not result:
|
||||
result = []
|
||||
defaults = get_cms_setting('LANGUAGES').get('default', {})
|
||||
for code, name in settings.LANGUAGES:
|
||||
lang = {'code': code, 'name': _(name)}
|
||||
lang.update(defaults)
|
||||
result.append(lang)
|
||||
get_cms_setting('LANGUAGES')[site_id] = result
|
||||
return result
|
||||
|
||||
|
||||
def get_site_language_from_request(request, site_id=None):
|
||||
from cms.utils import get_current_site
|
||||
|
||||
if site_id is None:
|
||||
site_id = get_current_site(request).pk
|
||||
|
||||
# Level 1: language get parameter
|
||||
language = request.GET.get('language', None)
|
||||
if is_valid_site_language(language, site_id=site_id):
|
||||
return language
|
||||
|
||||
# Level 2: LANGUAGE_CODE request parameter
|
||||
language = getattr(request, 'LANGUAGE_CODE', None)
|
||||
if is_valid_site_language(language, site_id=site_id):
|
||||
return language
|
||||
|
||||
# Last resort: default language
|
||||
return get_default_language_for_site(site_id=site_id)
|
||||
|
||||
|
||||
def get_language_code(language_code, site_id=None):
|
||||
"""
|
||||
Returns language code while making sure it's in LANGUAGES
|
||||
"""
|
||||
if not language_code:
|
||||
return None
|
||||
|
||||
site_id = _ensure_site_id(site_id, "get_language_code")
|
||||
languages = get_language_list(site_id)
|
||||
|
||||
if language_code in languages: # direct hit
|
||||
return language_code
|
||||
|
||||
for lang in languages:
|
||||
if language_code.split('-')[0] == lang: # base language hit
|
||||
return lang
|
||||
if lang.split('-')[0] == language_code: # base language hit
|
||||
return lang
|
||||
return language_code
|
||||
|
||||
|
||||
def get_current_language():
|
||||
"""
|
||||
Returns the currently active language
|
||||
|
||||
It's a replacement for Django's translation.get_language() to make sure the
|
||||
LANGUAGE_CODE will be found in LANGUAGES.
|
||||
Overcomes this issue: https://code.djangoproject.com/ticket/9340
|
||||
"""
|
||||
site_id = getattr(settings, 'SITE_ID', None)
|
||||
language_code = translation.get_language()
|
||||
if site_id:
|
||||
return get_language_code(language_code, site_id=site_id)
|
||||
|
||||
# We do not know the site, return an entry from settings.LANGUAGES
|
||||
languages = dict(getattr(settings, 'LANGUAGES', ((language_code, language_code), ))).keys()
|
||||
|
||||
if language_code in languages:
|
||||
return language_code # direct hit
|
||||
|
||||
for lang in languages:
|
||||
if language_code.split('-')[0] == lang: # base language hit
|
||||
return lang
|
||||
if lang.split('-')[0] == language_code: # base language hit
|
||||
return lang
|
||||
return language_code
|
||||
|
||||
|
||||
def get_language_list(site_id=None):
|
||||
"""
|
||||
:return: returns a list of iso2codes for this site
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_language_list")
|
||||
return ([lang['code'] for lang in get_languages(site_id)] if settings.USE_I18N
|
||||
else [settings.LANGUAGE_CODE])
|
||||
|
||||
|
||||
def get_language_tuple(site_id=None):
|
||||
"""
|
||||
:return: returns an list of tuples like the old CMS_LANGUAGES or the LANGUAGES for this site
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_language_tuple")
|
||||
return [(lang['code'], lang['name']) for lang in get_languages(site_id)]
|
||||
|
||||
|
||||
def get_language_dict(site_id=None):
|
||||
"""
|
||||
:return: returns an dict of cms languages
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_language_dict")
|
||||
return dict(get_language_tuple(site_id))
|
||||
|
||||
|
||||
def get_public_languages(site_id=None):
|
||||
"""
|
||||
:return: list of iso2codes of public languages for this site
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_public_languages")
|
||||
return [lang['code'] for lang in get_language_objects(site_id)
|
||||
if lang.get('public', True)]
|
||||
|
||||
|
||||
def get_language_object(language_code, site_id=None):
|
||||
"""
|
||||
:param language_code: RFC5646 language code
|
||||
:return: the language object filled up by defaults
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_language_object")
|
||||
for language in get_languages(site_id):
|
||||
if language['code'] == get_language_code(language_code, site_id):
|
||||
return language
|
||||
raise LanguageError('Language not found: %s' % language_code)
|
||||
|
||||
|
||||
def get_language_objects(site_id=None):
|
||||
"""
|
||||
returns list of all language objects filled up by default values
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_language_objects")
|
||||
return list(get_languages(site_id))
|
||||
|
||||
|
||||
def get_default_language(language_code=None, site_id=None):
|
||||
"""
|
||||
Returns default language depending on settings.LANGUAGE_CODE merged with
|
||||
best match from get_cms_setting('LANGUAGES')
|
||||
|
||||
Returns: language_code
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_default_language")
|
||||
if not language_code:
|
||||
language_code = get_language_code(settings.LANGUAGE_CODE, site_id=site_id)
|
||||
|
||||
languages = get_language_list(site_id)
|
||||
|
||||
# first try if there is an exact language
|
||||
if language_code in languages:
|
||||
return language_code
|
||||
|
||||
# otherwise split the language code if possible, so iso3
|
||||
language_code = language_code.split("-")[0]
|
||||
|
||||
if language_code not in languages:
|
||||
return settings.LANGUAGE_CODE
|
||||
|
||||
return language_code
|
||||
|
||||
|
||||
def get_default_language_for_site(site_id):
|
||||
return get_language_list(site_id)[0]
|
||||
|
||||
|
||||
def get_fallback_languages(language, site_id=None):
|
||||
"""
|
||||
returns a list of fallback languages for the given language
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_fallback_languages")
|
||||
try:
|
||||
language = get_language_object(language, site_id)
|
||||
except LanguageError:
|
||||
language = get_languages(site_id)[0]
|
||||
return language.get('fallbacks', [])
|
||||
|
||||
|
||||
def get_redirect_on_fallback(language, site_id=None):
|
||||
"""
|
||||
returns if you should redirect on language fallback
|
||||
:param language:
|
||||
:param site_id:
|
||||
:return: Boolean
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "get_redirect_on_fallback")
|
||||
language = get_language_object(language, site_id)
|
||||
return language.get('redirect_on_fallback', True)
|
||||
|
||||
|
||||
def hide_untranslated(language, site_id=None):
|
||||
"""
|
||||
Should untranslated pages in this language be hidden?
|
||||
:param language:
|
||||
:param site_id:
|
||||
:return: A Boolean
|
||||
"""
|
||||
site_id = _ensure_site_id(site_id, "hide_untranslated")
|
||||
obj = get_language_object(language, site_id)
|
||||
return obj.get('hide_untranslated', True)
|
||||
|
||||
|
||||
def is_language_prefix_patterns_used():
|
||||
"""
|
||||
Returns `True` if the `LocaleRegexURLResolver` or `LocalePrefixPattern`
|
||||
is used at root level of the urlpatterns and doesn't have empty
|
||||
language_prefix, else it returns `False`.
|
||||
"""
|
||||
for url_pattern in get_resolver(None).url_patterns:
|
||||
pattern = getattr(url_pattern, 'pattern', url_pattern)
|
||||
if isinstance(pattern, LocalePrefixPattern):
|
||||
if pattern.language_prefix != '':
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def is_valid_site_language(language, site_id):
|
||||
return language in get_language_list(site_id)
|
||||
54
cms/utils/mail.py
Normal file
54
cms/utils/mail.py
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
from django.contrib.sites.models import Site
|
||||
from django.core import mail
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
from django.template.loader import render_to_string
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from cms.utils.urlutils import admin_reverse, urljoin
|
||||
|
||||
MAIL_DELIVERY_ERRORS = (OSError,)
|
||||
if hasattr(mail, "MailerDoesNotExist"):
|
||||
MAIL_DELIVERY_ERRORS += (mail.MailerDoesNotExist,)
|
||||
|
||||
|
||||
def send_mail(subject, txt_template, to, context=None, html_template=None, fail_silently=True, site=None):
|
||||
"""
|
||||
Render and send multipart mail. With ``fail_silently=True``, suppress
|
||||
transport errors and an unconfigured mailer, but not programming errors.
|
||||
"""
|
||||
site = site or Site.objects.get_current()
|
||||
|
||||
context = context or {}
|
||||
context.update({
|
||||
'login_url': "https://%s" % urljoin(site.domain, admin_reverse('index')),
|
||||
'title': subject,
|
||||
})
|
||||
|
||||
txt_body = render_to_string(txt_template, context)
|
||||
|
||||
message = EmailMultiAlternatives(subject=subject, body=txt_body, to=to)
|
||||
|
||||
if html_template:
|
||||
body = render_to_string(html_template, context)
|
||||
message.attach_alternative(body, 'text/html')
|
||||
try:
|
||||
message.send()
|
||||
except MAIL_DELIVERY_ERRORS:
|
||||
if not fail_silently:
|
||||
raise
|
||||
|
||||
|
||||
def mail_page_user_change(user, created=False, password="", site=None):
|
||||
"""
|
||||
Send email notification to given user.
|
||||
Used it PageUser profile creation/update.
|
||||
"""
|
||||
if created:
|
||||
subject = _('CMS - your user account was created.')
|
||||
else:
|
||||
subject = _('CMS - your user account was changed.')
|
||||
send_mail(subject, 'admin/cms/mail/page_user_change.txt', [user.email], {
|
||||
'user': user,
|
||||
'password': password or "*" * 8,
|
||||
'created': created,
|
||||
}, 'admin/cms/mail/page_user_change.html', site=site)
|
||||
878
cms/utils/mptree.py
Normal file
878
cms/utils/mptree.py
Normal file
|
|
@ -0,0 +1,878 @@
|
|||
"""
|
||||
Dependency-free materialized-path tree backend
|
||||
|
||||
This replaces the ``django-treebeard`` dependency for the page tree
|
||||
by treating ``parent_id`` as the single source of truth and maintaining
|
||||
``path``/``depth``/``numchild`` as a derived, set-based-recomputed cache.
|
||||
|
||||
The module ships three things:
|
||||
|
||||
* :class:`MaterializedPath` -- a low-level *driver* implementing the tree
|
||||
algorithms against any model exposing the treebeard column layout
|
||||
(``path``/``depth``/``numchild``/``parent``). Operates set-based, never pulls
|
||||
a subtree into Python on the hot paths, uses only cross-database ORM
|
||||
functions (``Concat``/``Substr``/``Length``/``F``), and locks at *parent-row*
|
||||
granularity for concurrency.
|
||||
* :class:`MaterializedPathMixin` -- an abstract model exposing the subset of the
|
||||
treebeard ``MP_Node`` API that django-cms uses, delegating to the driver. Its
|
||||
fields are declared **identically to treebeard** so that swapping it in for
|
||||
``MP_Node`` produces *no* migration.
|
||||
* :func:`get_tree_base` -- selects the base class (treebeard or this) from the
|
||||
``CMS_TREE_BACKEND`` setting, so the backend can be swapped per-deployment
|
||||
(setting + restart, same database, no migration).
|
||||
|
||||
The path encoding is byte-for-byte compatible with treebeard's defaults
|
||||
(base-36 alphabet, ``steplen=4``, no separators), so existing ``path`` values
|
||||
remain valid in either direction.
|
||||
|
||||
A future redesign that removes sibling renumbering and the depth/width ceilings
|
||||
("fractional ordering") is sketched in the **PHASE 2 DESIGN NOTE** at the bottom
|
||||
of this module. It is intentionally *not* implemented: it requires a one-time
|
||||
migration and ends treebeard byte-compatibility, so it is a deliberate later
|
||||
step, not part of this drop-in backend.
|
||||
"""
|
||||
|
||||
from collections import defaultdict
|
||||
from functools import wraps
|
||||
|
||||
from django.db import models, router, transaction
|
||||
from django.db.models import F, Value
|
||||
from django.db.models.functions import Concat, Length, Substr
|
||||
|
||||
DEFAULT_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
DEFAULT_STEPLEN = 4
|
||||
|
||||
# Columns the driver plans from. Re-read under lock before planning a mutation
|
||||
# so a concurrently moved/renumbered node is never laid out from stale values.
|
||||
TREE_FIELDS = ["path", "depth", "numchild", "parent"]
|
||||
|
||||
|
||||
class TreePathOverflow(ValueError):
|
||||
"""
|
||||
Raised when a tree position cannot be represented: a sibling step too large
|
||||
for one ``steplen``-wide segment, or a path too long for the ``path`` column.
|
||||
|
||||
Raised *before* any statement runs, so the transaction is left untouched
|
||||
rather than failing mid-rewrite on a truncated or over-long value.
|
||||
"""
|
||||
|
||||
|
||||
def _atomic_on_alias(method):
|
||||
"""``transaction.atomic`` on the driver's own database, not ``default``."""
|
||||
|
||||
@wraps(method)
|
||||
def wrapped(self, *args, **kwargs):
|
||||
with transaction.atomic(using=self.db_alias):
|
||||
return method(self, *args, **kwargs)
|
||||
|
||||
return wrapped
|
||||
|
||||
|
||||
class MaterializedPath:
|
||||
"""
|
||||
Set-based materialized-path operations for a model with the treebeard
|
||||
column layout: ``path`` (unique CharField), ``depth``, ``numchild`` and a
|
||||
self-referential ``parent`` FK.
|
||||
|
||||
``scope`` is an optional dict of field lookups that partitions the forest
|
||||
(e.g. ``{"site_id": 3}``). It defaults to no scope, i.e. the whole table is
|
||||
one forest -- matching treebeard's behaviour for the page tree.
|
||||
|
||||
``using`` pins every read, write, lock and transaction to one database
|
||||
alias. It defaults to the write database the router picks for ``model``, and
|
||||
callers coming from a loaded instance pass that instance's alias so an
|
||||
operation never straddles two connections.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
model,
|
||||
*,
|
||||
steplen=DEFAULT_STEPLEN,
|
||||
alphabet=DEFAULT_ALPHABET,
|
||||
scope=None,
|
||||
lock=True,
|
||||
using=None,
|
||||
):
|
||||
self.model = model
|
||||
self.steplen = steplen
|
||||
self.alphabet = alphabet
|
||||
self.radix = len(alphabet)
|
||||
self.path_max_length = model._meta.get_field("path").max_length
|
||||
self.scope = scope or {}
|
||||
self.lock = lock
|
||||
self.db_alias = using or router.db_for_write(model)
|
||||
|
||||
# -- encoding (treebeard-compatible) ---------------------------------
|
||||
|
||||
def _int2str(self, num):
|
||||
ret = ""
|
||||
num = int(num)
|
||||
while True:
|
||||
ret = self.alphabet[num % self.radix] + ret
|
||||
if num < self.radix:
|
||||
return ret
|
||||
num //= self.radix
|
||||
|
||||
def _str2int(self, key):
|
||||
num = 0
|
||||
for char in key:
|
||||
num = num * self.radix + self.alphabet.index(char)
|
||||
return num
|
||||
|
||||
def segment(self, step):
|
||||
"""The fixed-width path segment for a 1-based sibling ``step``."""
|
||||
step = int(step)
|
||||
if step < 1 or step >= self.radix**self.steplen:
|
||||
raise TreePathOverflow(
|
||||
f"Sibling step {step} does not fit in a {self.steplen}-character "
|
||||
f"base-{self.radix} path segment."
|
||||
)
|
||||
key = self._int2str(step)
|
||||
return self.alphabet[0] * (self.steplen - len(key)) + key
|
||||
|
||||
def step_of(self, path):
|
||||
"""Decode the last (own) segment of ``path`` back to its integer step."""
|
||||
return self._str2int(path[-self.steplen :])
|
||||
|
||||
# -- path length guards ----------------------------------------------
|
||||
|
||||
def _ensure_path_fits(self, path):
|
||||
self._ensure_path_length_fits(len(path))
|
||||
|
||||
def _ensure_path_length_fits(self, required):
|
||||
if self.path_max_length is not None and required > self.path_max_length:
|
||||
raise TreePathOverflow(
|
||||
f"Path requires {required} characters but "
|
||||
f"{self.model._meta.label}.path allows {self.path_max_length}."
|
||||
)
|
||||
|
||||
# -- read queries ----------------------------------------------------
|
||||
|
||||
def _scoped(self):
|
||||
return self.model._default_manager.using(self.db_alias).filter(**self.scope)
|
||||
|
||||
def roots(self):
|
||||
return self._scoped().filter(depth=1).order_by("path")
|
||||
|
||||
def children(self, node):
|
||||
return self._scoped().filter(
|
||||
path__startswith=node.path, depth=node.depth + 1
|
||||
).order_by("path")
|
||||
|
||||
def descendants(self, node):
|
||||
# The materialized path *is* the precomputed recursion: an indexed
|
||||
# prefix scan, no WITH RECURSIVE required.
|
||||
return self._scoped().filter(
|
||||
path__startswith=node.path, depth__gt=node.depth
|
||||
).order_by("path")
|
||||
|
||||
def ancestors(self, node):
|
||||
paths = [
|
||||
node.path[0:pos]
|
||||
for pos in range(self.steplen, len(node.path), self.steplen)
|
||||
]
|
||||
if not paths:
|
||||
return self.model._default_manager.using(self.db_alias).none()
|
||||
return self._scoped().filter(path__in=paths).order_by("path")
|
||||
|
||||
def tree(self, parent=None):
|
||||
if parent is None:
|
||||
return self._scoped().order_by("path")
|
||||
return self._scoped().filter(
|
||||
path__startswith=parent.path, depth__gte=parent.depth
|
||||
).order_by("path")
|
||||
|
||||
def root_of(self, node):
|
||||
return self._scoped().get(path=node.path[0 : self.steplen])
|
||||
|
||||
def _children_rows(self, parent_path, parent_depth):
|
||||
# Ordered direct children as lightweight (deferred) instances, fetched
|
||||
# once and reused for index/append/layout to avoid repeat queries.
|
||||
return list(
|
||||
self._scoped()
|
||||
.filter(path__startswith=parent_path, depth=parent_depth + 1)
|
||||
.order_by("path")
|
||||
.only("pk", "path", "depth")
|
||||
)
|
||||
|
||||
def _ordered_child_pks(self, parent_path, parent_depth):
|
||||
return [c.pk for c in self._children_rows(parent_path, parent_depth)]
|
||||
|
||||
def _last_child_step(self, parent_path, parent_depth, exclude_pk=None):
|
||||
qs = self._scoped().filter(path__startswith=parent_path, depth=parent_depth + 1)
|
||||
if exclude_pk is not None:
|
||||
# When *moving* a node to the end of its own sibling group, its
|
||||
# current slot must not count -- otherwise it would be bumped into a
|
||||
# spurious gap instead of staying put.
|
||||
qs = qs.exclude(pk=exclude_pk)
|
||||
last = qs.order_by("path").values_list("path", flat=True).last()
|
||||
return self.step_of(last) if last else 0
|
||||
|
||||
def _last_root_step(self):
|
||||
last = self.roots().values_list("path", flat=True).last()
|
||||
return self.step_of(last) if last else 0
|
||||
|
||||
# -- concurrency -----------------------------------------------------
|
||||
|
||||
def _lock_rows(self, *pks):
|
||||
"""
|
||||
Take row-level write locks (in pk order, to avoid deadlocks) on the rows
|
||||
whose sibling slot / numchild caches an operation will touch. Parent-row
|
||||
granularity: writes under different parents do not serialise against each
|
||||
other. No-op on backends without ``SELECT ... FOR UPDATE`` (SQLite).
|
||||
"""
|
||||
if not self.lock:
|
||||
return
|
||||
pks = sorted({pk for pk in pks if pk is not None})
|
||||
if pks:
|
||||
list(
|
||||
self.model._base_manager.using(self.db_alias)
|
||||
.filter(pk__in=pks)
|
||||
.order_by("pk")
|
||||
.select_for_update()
|
||||
)
|
||||
|
||||
def _refresh(self, *instances):
|
||||
"""
|
||||
Re-read the tree columns of caller-supplied instances, after locking and
|
||||
before planning. The caller may have held them across an earlier request
|
||||
or a concurrent move, in which case their ``path``/``depth``/``parent``
|
||||
no longer describe where the node actually is -- and a layout planned
|
||||
from those values rewrites the wrong rows. Only the tree columns are
|
||||
reloaded, so unsaved edits the caller made to other fields survive.
|
||||
"""
|
||||
for instance in instances:
|
||||
if instance is not None and instance.pk is not None:
|
||||
instance.refresh_from_db(using=self.db_alias, fields=TREE_FIELDS)
|
||||
|
||||
# -- low-level subtree rewrite --------------------------------------
|
||||
|
||||
def _reprefix(self, *, old_prefix, new_prefix, depth_delta):
|
||||
"""
|
||||
Rewrite an entire subtree's paths by prefix-swap in one statement::
|
||||
|
||||
new path = new_prefix || (old path with its old_prefix stripped)
|
||||
|
||||
Never pulls descendant rows into Python.
|
||||
"""
|
||||
qs = self._scoped().filter(path__startswith=old_prefix)
|
||||
self._ensure_path_fits(new_prefix)
|
||||
prefix_growth = len(new_prefix) - len(old_prefix)
|
||||
if prefix_growth > 0:
|
||||
# The subtree moves deeper (or is being parked under a longer
|
||||
# placeholder prefix): every descendant path grows by the same
|
||||
# amount, so the deepest one decides whether the whole rewrite
|
||||
# fits. One aggregate query, only on the branch that can overflow.
|
||||
longest = (
|
||||
qs.annotate(_mptree_path_length=Length("path"))
|
||||
.order_by("-_mptree_path_length")
|
||||
.values_list("_mptree_path_length", flat=True)
|
||||
.first()
|
||||
)
|
||||
if longest is not None:
|
||||
self._ensure_path_length_fits(longest + prefix_growth)
|
||||
update = {"path": Concat(Value(new_prefix), Substr("path", len(old_prefix) + 1))}
|
||||
if depth_delta:
|
||||
update["depth"] = F("depth") + depth_delta
|
||||
qs.update(**update)
|
||||
|
||||
def _layout(self, parent_path, parent_depth, ordered_pks, info=None):
|
||||
"""
|
||||
Lay the given child subtrees out as contiguous steps ``1..n`` under the
|
||||
parent, in the given order, fixing each subtree's prefix *and* depth.
|
||||
``info`` may carry already-fetched ``{pk: instance}`` rows (with
|
||||
``path``/``depth``) so the caller's prior query can be reused.
|
||||
|
||||
Two correctness-preserving shortcuts, both byte-identical to a naive
|
||||
full relayout:
|
||||
|
||||
* **Skip already-placed children.** Target slots are a permutation of
|
||||
``1..n``, so a child already at its target slot is wanted by no other
|
||||
child -- leaving it can never collide. Turns "insert near the end"
|
||||
from O(n) rewrites into O(moved).
|
||||
* **Skip the parking pass when nothing moves *down*.** Parking into the
|
||||
disjoint ``~<pk>~`` namespace only exists to break collisions. If no
|
||||
mover targets a lower slot than it currently holds (the usual
|
||||
insert/shift-up / move-from-elsewhere case), writing movers
|
||||
highest-slot-first always lands in a slot just vacated (or never
|
||||
occupied), so a single pass suffices -- halving the statements.
|
||||
A genuine down-move (e.g. moving a later sibling to an earlier slot)
|
||||
falls back to the always-safe park-then-write.
|
||||
|
||||
Both shortcuts assume the movers are *disjoint* subtrees. One mover can
|
||||
sit inside another -- moving a node to a slot relative to its own parent
|
||||
or uncle -- and then rewriting the outer subtree first would drag the
|
||||
inner one along and leave its recorded ``old_path`` matching nothing.
|
||||
Those layouts always park, deepest path first, so that by the time an
|
||||
ancestor is rewritten its nested mover is already out of the way.
|
||||
"""
|
||||
known = dict(info or {})
|
||||
missing = [pk for pk in ordered_pks if pk not in known]
|
||||
if missing:
|
||||
for obj in self._scoped().filter(pk__in=missing).only("pk", "path", "depth"):
|
||||
known[obj.pk] = obj
|
||||
|
||||
target_depth = parent_depth + 1
|
||||
prefix_len = len(parent_path)
|
||||
movers = [] # (pk, target_step, target_prefix, old_path, depth_delta, old_slot)
|
||||
for step, pk in enumerate(ordered_pks, start=1):
|
||||
obj = known[pk]
|
||||
target_prefix = parent_path + self.segment(step)
|
||||
if obj.path == target_prefix and obj.depth == target_depth:
|
||||
continue # already in place; cannot collide (slots are unique)
|
||||
is_child_here = (
|
||||
obj.depth == target_depth
|
||||
and obj.path.startswith(parent_path)
|
||||
and len(obj.path) == prefix_len + self.steplen
|
||||
)
|
||||
old_slot = self.step_of(obj.path) if is_child_here else None
|
||||
movers.append((pk, step, target_prefix, obj.path, target_depth - obj.depth, old_slot))
|
||||
|
||||
needs_park = any(slot is not None and step < slot for _, step, _, _, _, slot in movers)
|
||||
if not needs_park:
|
||||
# Nested movers (one mover's subtree contains another's) cannot be
|
||||
# rewritten in place either. If a path is a proper prefix of another
|
||||
# every path sorting between them shares that prefix, so comparing
|
||||
# sorted neighbours is enough to spot it.
|
||||
in_order = sorted(m[3] for m in movers)
|
||||
needs_park = any(
|
||||
b.startswith(a) for a, b in zip(in_order, in_order[1:], strict=False)
|
||||
)
|
||||
if needs_park:
|
||||
# Deepest first: parking a nested subtree before its ancestor keeps
|
||||
# the ancestor's rewrite from touching it.
|
||||
for pk, _, _, old_path, _, _ in sorted(movers, key=lambda m: len(m[3]), reverse=True):
|
||||
self._reprefix(old_prefix=old_path, new_prefix=f"~{pk}~", depth_delta=0)
|
||||
for pk, _, target_prefix, _, depth_delta, _ in movers:
|
||||
self._reprefix(old_prefix=f"~{pk}~", new_prefix=target_prefix, depth_delta=depth_delta)
|
||||
else:
|
||||
# highest target slot first -> destination is always free
|
||||
for pk, _, target_prefix, old_path, depth_delta, _ in sorted(
|
||||
movers, key=lambda m: m[1], reverse=True
|
||||
):
|
||||
self._reprefix(old_prefix=old_path, new_prefix=target_prefix, depth_delta=depth_delta)
|
||||
|
||||
# -- node construction ----------------------------------------------
|
||||
|
||||
def _materialise(self, instance, attrs, *, path, depth, parent):
|
||||
self._ensure_path_fits(path)
|
||||
# `parent`/`parent_id` are determined by the tree operation, not by the
|
||||
# caller's kwargs (treebeard's add_child/add_sibling accept them as
|
||||
# redundant field values) -- drop them so they can't conflict.
|
||||
attrs.pop("parent", None)
|
||||
attrs.pop("parent_id", None)
|
||||
if instance is None:
|
||||
instance = self.model(**{**self.scope, **attrs})
|
||||
instance.path = path
|
||||
instance.depth = depth
|
||||
instance.numchild = 0
|
||||
instance.parent = parent
|
||||
for field, value in self.scope.items():
|
||||
setattr(instance, field, value)
|
||||
instance.save(using=self.db_alias)
|
||||
return instance
|
||||
|
||||
def _bump_numchild(self, pk, delta):
|
||||
if pk is not None:
|
||||
self.model._base_manager.using(self.db_alias).filter(pk=pk).update(
|
||||
numchild=F("numchild") + delta
|
||||
)
|
||||
|
||||
# -- build (append + positional insert) ------------------------------
|
||||
|
||||
@_atomic_on_alias
|
||||
def add_root(self, instance=None, **attrs):
|
||||
self._lock_rows(*self.roots().values_list("pk", flat=True))
|
||||
step = self._last_root_step() + 1
|
||||
return self._materialise(
|
||||
instance, attrs, path=self.segment(step), depth=1, parent=None
|
||||
)
|
||||
|
||||
@_atomic_on_alias
|
||||
def add_child(self, parent, position="last-child", instance=None, **attrs):
|
||||
self._lock_rows(parent.pk)
|
||||
self._refresh(parent)
|
||||
step = self._last_child_step(parent.path, parent.depth) + 1
|
||||
node = self._materialise(
|
||||
instance,
|
||||
attrs,
|
||||
path=parent.path + self.segment(step),
|
||||
depth=parent.depth + 1,
|
||||
parent=parent,
|
||||
)
|
||||
self._bump_numchild(parent.pk, +1)
|
||||
parent.numchild = (parent.numchild or 0) + 1 # keep caller's instance honest
|
||||
if position == "first-child":
|
||||
existing = [pk for pk in self._ordered_child_pks(parent.path, parent.depth) if pk != node.pk]
|
||||
self._layout(parent.path, parent.depth, [node.pk] + existing)
|
||||
self._refresh(node)
|
||||
return node
|
||||
|
||||
@_atomic_on_alias
|
||||
def add_sibling(self, node, position="last-sibling", instance=None, **attrs):
|
||||
self._lock_rows(node.pk)
|
||||
self._refresh(node)
|
||||
parent = node.parent
|
||||
if parent is None:
|
||||
new = self.add_root(instance=instance, **attrs)
|
||||
if position in ("first-sibling", "left"):
|
||||
# rare: reorder roots is not exercised by django-cms; left to
|
||||
# rebuild() which canonicalises root order.
|
||||
pass
|
||||
return new
|
||||
new = self.add_child(parent, instance=instance, **attrs)
|
||||
if position in ("last-sibling", "right"):
|
||||
# 'right' relative to an arbitrary sibling still lands at the end in
|
||||
# this minimal implementation unless it must sit immediately after
|
||||
# `node`; place precisely:
|
||||
self._place_relative(new, node, after=(position == "right"))
|
||||
elif position in ("first-sibling", "left"):
|
||||
self._place_relative(new, node, after=False)
|
||||
return new
|
||||
|
||||
def _place_relative(self, node, sibling, *, after):
|
||||
parent = sibling.parent
|
||||
parent_path = parent.path if parent else ""
|
||||
parent_depth = parent.depth if parent else 0
|
||||
order = [pk for pk in self._ordered_child_pks(parent_path, parent_depth) if pk != node.pk]
|
||||
idx = order.index(sibling.pk) + (1 if after else 0)
|
||||
order.insert(idx, node.pk)
|
||||
self._layout(parent_path, parent_depth, order)
|
||||
self._refresh(node)
|
||||
|
||||
# -- move (single statement for append; layout for insert) -----------
|
||||
|
||||
@_atomic_on_alias
|
||||
def move(self, node, target, pos="last-child"):
|
||||
"""
|
||||
Move ``node`` (and its whole subtree). Supported ``pos``:
|
||||
``last-child``/``first-child`` (relative to ``target``) and
|
||||
``left``/``right`` (relative to sibling ``target``).
|
||||
"""
|
||||
# Read the parent ids first so every row this move touches can be taken
|
||||
# in one pk-ordered `_lock_rows` -- concurrent moves then queue behind
|
||||
# each other instead of deadlocking on opposite acquisition orders. The
|
||||
# values are unverified at this point; the lock below is what makes them
|
||||
# true, and the re-read after it is what proves they still are.
|
||||
parent_ids = dict(
|
||||
self.model._base_manager.using(self.db_alias)
|
||||
.filter(pk__in={pk for pk in (node.pk, target.pk) if pk is not None})
|
||||
.values_list("pk", "parent_id")
|
||||
)
|
||||
self._lock_rows(
|
||||
node.pk, target.pk, parent_ids.get(node.pk), parent_ids.get(target.pk)
|
||||
)
|
||||
# Everything below -- which parent to lock, which slot to target, the
|
||||
# move-into-own-subtree check -- is planned from these tree columns, so
|
||||
# they have to be the committed ones, not whatever the caller has been
|
||||
# holding since an earlier request.
|
||||
self._refresh(node, target)
|
||||
old_parent_id = node.parent_id
|
||||
|
||||
relative_to_sibling = pos in ("left", "right")
|
||||
new_parent_id = target.parent_id if relative_to_sibling else target.pk
|
||||
# No-op when the pre-read was accurate (re-locking a row this
|
||||
# transaction already holds is free); only a concurrent re-parent
|
||||
# between the read and the lock makes this acquire anything new.
|
||||
self._lock_rows(old_parent_id, new_parent_id)
|
||||
|
||||
if not relative_to_sibling:
|
||||
new_parent = target
|
||||
elif new_parent_id is None:
|
||||
new_parent = None
|
||||
else:
|
||||
# Re-fetched rather than followed through `target.parent`: the
|
||||
# descriptor can hand back an object cached before the lock, i.e.
|
||||
# exactly the stale state re-reading `target` was meant to escape.
|
||||
new_parent = self.model._base_manager.using(self.db_alias).get(
|
||||
pk=new_parent_id
|
||||
)
|
||||
|
||||
if new_parent is not None and (
|
||||
target.path == node.path or new_parent.path.startswith(node.path)
|
||||
):
|
||||
raise ValueError("Cannot move a node into itself or its own subtree.")
|
||||
|
||||
parent_path = new_parent.path if new_parent else ""
|
||||
parent_depth = new_parent.depth if new_parent else 0
|
||||
|
||||
if pos == "last-child":
|
||||
# Fast path: a single set-based UPDATE, no sibling touched.
|
||||
new_step = self._last_child_step(parent_path, parent_depth, exclude_pk=node.pk) + 1
|
||||
new_prefix = parent_path + self.segment(new_step)
|
||||
depth_delta = (parent_depth + 1) - node.depth
|
||||
self._reprefix(old_prefix=node.path, new_prefix=new_prefix, depth_delta=depth_delta)
|
||||
else:
|
||||
# One child fetch, reused for index lookup, append step and layout.
|
||||
siblings = [c for c in self._children_rows(parent_path, parent_depth) if c.pk != node.pk]
|
||||
order = [c.pk for c in siblings]
|
||||
if pos == "first-child":
|
||||
idx = 0
|
||||
elif pos == "left":
|
||||
idx = order.index(target.pk)
|
||||
else: # right
|
||||
idx = order.index(target.pk) + 1
|
||||
if idx == len(order):
|
||||
# Landing at the end is an append: one set-based UPDATE, leaving
|
||||
# the vacated source slot as a gap (matching treebeard, and far
|
||||
# cheaper than relaying out every sibling).
|
||||
new_step = (self.step_of(siblings[-1].path) + 1) if siblings else 1
|
||||
new_prefix = parent_path + self.segment(new_step)
|
||||
depth_delta = (parent_depth + 1) - node.depth
|
||||
self._reprefix(old_prefix=node.path, new_prefix=new_prefix, depth_delta=depth_delta)
|
||||
else:
|
||||
order.insert(idx, node.pk)
|
||||
# node carries fresh path/depth; pass everything so _layout needs
|
||||
# no further query.
|
||||
info = {c.pk: c for c in siblings}
|
||||
info[node.pk] = node
|
||||
self._layout(parent_path, parent_depth, order, info=info)
|
||||
|
||||
new_parent_pk = new_parent.pk if new_parent else None
|
||||
self.model._base_manager.using(self.db_alias).filter(pk=node.pk).update(
|
||||
parent=new_parent
|
||||
)
|
||||
if old_parent_id != new_parent_pk:
|
||||
self._bump_numchild(old_parent_id, -1)
|
||||
self._bump_numchild(new_parent_pk, +1)
|
||||
# Keep the caller's in-memory instances honest: `node` moved, and
|
||||
# `target` may have been renumbered (left/right) or had its numchild
|
||||
# change (first/last-child) -- treebeard updates these in place too.
|
||||
self._refresh(node, target)
|
||||
|
||||
# -- rebuild (recompute everything from parent_id) -------------------
|
||||
|
||||
@_atomic_on_alias
|
||||
def rebuild(self):
|
||||
"""
|
||||
Recompute every ``path``/``depth``/``numchild`` in the scope from the
|
||||
tree structure, compacting sibling steps to ``1..N`` while **preserving
|
||||
the existing sibling order** (read from the current ``path``, exactly
|
||||
like treebeard's ``fix_tree``). The dependency-free replacement for
|
||||
``fix_tree`` -- a rare maintenance operation, written back in two
|
||||
collision-free passes (park in the disjoint ``~<pk>`` namespace, then
|
||||
write final values).
|
||||
|
||||
Ordering by ``path`` means siblings keep the order an editor arranged,
|
||||
not creation order -- so no separate ``position`` field is needed for
|
||||
order to survive a rebuild.
|
||||
"""
|
||||
rows = list(
|
||||
self._scoped().order_by("path").values("pk", "parent_id")
|
||||
)
|
||||
present = {r["pk"] for r in rows}
|
||||
children = defaultdict(list)
|
||||
for r in rows:
|
||||
# rows are in path order, so each parent's children list is built in
|
||||
# sibling order
|
||||
parent = r["parent_id"] if r["parent_id"] in present else None
|
||||
children[parent].append(r["pk"])
|
||||
|
||||
computed = {}
|
||||
stack = [(None, "", 1)]
|
||||
while stack:
|
||||
parent_pk, parent_path, depth = stack.pop()
|
||||
for step, pk in enumerate(children.get(parent_pk, []), start=1):
|
||||
path = parent_path + self.segment(step)
|
||||
self._ensure_path_fits(path)
|
||||
computed[pk] = [path, depth, len(children.get(pk, []))]
|
||||
stack.append((pk, path, depth + 1))
|
||||
|
||||
self.model._base_manager.using(self.db_alias).bulk_update(
|
||||
[self.model(pk=pk, path=f"~{pk}") for pk in computed],
|
||||
["path"],
|
||||
batch_size=500,
|
||||
)
|
||||
self.model._base_manager.using(self.db_alias).bulk_update(
|
||||
[
|
||||
self.model(pk=pk, path=p, depth=d, numchild=n)
|
||||
for pk, (p, d, n) in computed.items()
|
||||
],
|
||||
["path", "depth", "numchild"],
|
||||
batch_size=500,
|
||||
)
|
||||
return len(computed)
|
||||
|
||||
|
||||
class MaterializedPathMixin(models.Model):
|
||||
"""
|
||||
Drop-in replacement base for treebeard's ``MP_Node`` covering the API that
|
||||
django-cms uses. Fields are declared **identically to treebeard** so that
|
||||
substituting this for ``MP_Node`` generates no migration.
|
||||
"""
|
||||
|
||||
path = models.CharField(max_length=255, unique=True)
|
||||
depth = models.PositiveIntegerField()
|
||||
numchild = models.PositiveIntegerField(default=0)
|
||||
|
||||
steplen = DEFAULT_STEPLEN
|
||||
alphabet = DEFAULT_ALPHABET
|
||||
node_order_by = []
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
# -- driver ----------------------------------------------------------
|
||||
|
||||
@classmethod
|
||||
def _tree(cls, using=None):
|
||||
return MaterializedPath(
|
||||
cls, steplen=cls.steplen, alphabet=cls.alphabet, using=using
|
||||
)
|
||||
|
||||
def _instance_tree(self):
|
||||
"""
|
||||
Driver pinned to the database this instance was loaded from, so a node
|
||||
read from a replica or a secondary alias is never queried, locked or
|
||||
rewritten against ``default``.
|
||||
"""
|
||||
return type(self)._tree(using=self._state.db)
|
||||
|
||||
# -- treebeard-compatible API ---------------------------------------
|
||||
|
||||
@classmethod
|
||||
def add_root(cls, instance=None, **attrs):
|
||||
using = None
|
||||
if instance is not None:
|
||||
using = instance._state.db or router.db_for_write(cls, instance=instance)
|
||||
return cls._tree(using=using).add_root(instance=instance, **attrs)
|
||||
|
||||
@classmethod
|
||||
def get_root_nodes(cls, using=None):
|
||||
return cls._tree(using=using).roots()
|
||||
|
||||
@classmethod
|
||||
def get_tree(cls, parent=None, using=None):
|
||||
if using is None and parent is not None:
|
||||
using = parent._state.db
|
||||
return cls._tree(using=using).tree(parent)
|
||||
|
||||
@classmethod
|
||||
def fix_tree(cls, using=None, **kwargs):
|
||||
return cls._tree(using=using).rebuild()
|
||||
|
||||
def add_child(self, instance=None, **attrs):
|
||||
attrs.pop("parent", None) # redundant: the parent is `self`
|
||||
return self._instance_tree().add_child(self, instance=instance, **attrs)
|
||||
|
||||
def add_sibling(self, pos="last-sibling", instance=None, **attrs):
|
||||
attrs.pop("parent", None)
|
||||
attrs.pop("parent_id", None)
|
||||
return self._instance_tree().add_sibling(
|
||||
self, position=pos, instance=instance, **attrs
|
||||
)
|
||||
|
||||
def move(self, target, pos="last-child"):
|
||||
self._instance_tree().move(self, target, pos)
|
||||
|
||||
def get_children(self):
|
||||
return self._instance_tree().children(self)
|
||||
|
||||
def get_descendants(self):
|
||||
return self._instance_tree().descendants(self)
|
||||
|
||||
def get_ancestors(self):
|
||||
return self._instance_tree().ancestors(self)
|
||||
|
||||
def get_root(self):
|
||||
return self._instance_tree().root_of(self)
|
||||
|
||||
def get_parent(self, update=False):
|
||||
return self.parent
|
||||
|
||||
def get_first_child(self):
|
||||
return self._instance_tree().children(self).first()
|
||||
|
||||
def is_root(self):
|
||||
return self.depth == 1
|
||||
|
||||
def is_leaf(self):
|
||||
return self.numchild == 0
|
||||
|
||||
def is_sibling_of(self, other):
|
||||
return self.depth == other.depth and self.parent_id == other.parent_id
|
||||
|
||||
def is_child_of(self, other):
|
||||
return self.parent_id == other.pk
|
||||
|
||||
def is_descendant_of(self, other):
|
||||
return self.depth > other.depth and self.path.startswith(other.path)
|
||||
|
||||
def get_siblings(self):
|
||||
if self.parent_id is None:
|
||||
return self._instance_tree().roots()
|
||||
return self._instance_tree().children(self.parent)
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
# Deleting a node removes it from its parent's child set; keep the
|
||||
# parent's numchild cache correct (treebeard does this via its
|
||||
# queryset/model delete overrides). Descendants are removed by the
|
||||
# parent FK's on_delete cascade and do not affect surviving nodes.
|
||||
parent_id = self.parent_id
|
||||
# Resolved before the delete: `Model.delete` accepts `using` positionally
|
||||
# as well as by keyword, and clears the instance's state afterwards.
|
||||
using = (
|
||||
kwargs.get("using")
|
||||
or (args[0] if args else None)
|
||||
or self._state.db
|
||||
or router.db_for_write(type(self), instance=self)
|
||||
)
|
||||
result = super().delete(*args, **kwargs)
|
||||
if parent_id is not None:
|
||||
type(self)._base_manager.using(using).filter(pk=parent_id).update(
|
||||
numchild=models.F("numchild") - 1
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
def get_tree_backend() -> str:
|
||||
"""
|
||||
The active page-tree backend: ``"treebeard"`` (default) or ``"mptree"``,
|
||||
from the ``CMS_TREE_BACKEND`` setting. An env var of the same name overrides
|
||||
the setting only when the setting is not explicitly defined, which keeps the
|
||||
backends swappable in CI / subprocess tests without touching the settings
|
||||
module. Resolved at import time, so a change requires a process restart --
|
||||
but no migration, because both backends declare identical fields.
|
||||
|
||||
``treebeard`` is imported lazily (only by the selectors below, only when this
|
||||
returns ``"treebeard"``), so the ``mptree`` backend never imports it.
|
||||
"""
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
return getattr(settings, "CMS_TREE_BACKEND", None) or os.environ.get(
|
||||
"CMS_TREE_BACKEND", "mptree"
|
||||
)
|
||||
|
||||
|
||||
def get_tree_base() -> type:
|
||||
"""Base model class for ``Page`` -- treebeard's ``MP_Node`` or our mixin."""
|
||||
if get_tree_backend() == "mptree":
|
||||
return MaterializedPathMixin
|
||||
|
||||
from treebeard.mp_tree import MP_Node
|
||||
|
||||
return MP_Node
|
||||
|
||||
|
||||
def get_queryset_base() -> type:
|
||||
"""
|
||||
Base class for ``PageQuerySet``. Treebeard mode keeps ``MP_NodeQuerySet``
|
||||
(its only contribution is a tree-fixup ``delete``); mptree mode uses a plain
|
||||
Django ``QuerySet`` so treebeard is not imported at all.
|
||||
"""
|
||||
if get_tree_backend() == "mptree":
|
||||
return models.QuerySet
|
||||
|
||||
from treebeard.mp_tree import MP_NodeQuerySet
|
||||
|
||||
return MP_NodeQuerySet
|
||||
|
||||
|
||||
# ======================================================================
|
||||
# PHASE 2 DESIGN NOTE -- FRACTIONAL ORDERING
|
||||
# ======================================================================
|
||||
#
|
||||
# Status: NOT IMPLEMENTED. Deliberate future step. Requires a one-time data
|
||||
# migration and ends treebeard byte-compatibility (so it cannot be hot-swapped
|
||||
# back to treebeard). Documented for potential future dev
|
||||
#
|
||||
# ----------------------------------------------------------------------
|
||||
# Why
|
||||
# ----------------------------------------------------------------------
|
||||
# This backend stores sibling order as contiguous base-36 steps inside `path`.
|
||||
# Two consequences follow from that single choice:
|
||||
#
|
||||
# * Inserting/reordering in the middle of a sibling group renumbers the
|
||||
# following siblings -- `_layout()` rewrites O(width) subtrees. (Benchmarked:
|
||||
# a first-child move into a 2000-wide group is ~840 ms, on par with
|
||||
# treebeard, because both renumber.) The `last-child` fast path is a single
|
||||
# statement; only mid-inserts pay this.
|
||||
# * Fixed `steplen=4` + `path <= 255` caps the tree at ~63 levels deep and
|
||||
# 36^4 (~1.6M) siblings per node.
|
||||
#
|
||||
# Fractional ordering removes BOTH by separating "structure" from "order" into
|
||||
# two explicit source-of-truth columns and demoting `path` to a pure read cache.
|
||||
#
|
||||
# ----------------------------------------------------------------------
|
||||
# Model
|
||||
# ----------------------------------------------------------------------
|
||||
# class FractionalTreeMixin(models.Model):
|
||||
# # --- source of truth ---
|
||||
# parent = FK("self", null=True, on_delete=CASCADE, related_name="children")
|
||||
# position = CharField(max_length=255) # fractional / LexoRank key, e.g.
|
||||
# # "a0", "a0V", "a1"; sibling-local
|
||||
# # --- derived read cache (rebuildable from parent_id + position) ---
|
||||
# path = TextField() # SEP-joined ancestor position keys
|
||||
# path_hash = CharField(max_length=40, unique=True) # sha1(path); see below
|
||||
# depth = PositiveIntegerField()
|
||||
# numchild = PositiveIntegerField(default=0)
|
||||
#
|
||||
# class Meta:
|
||||
# constraints = [UniqueConstraint(fields=["parent", "position"])]
|
||||
# indexes = [Index(fields=["path_hash"])] # + a prefix index for LIKE
|
||||
#
|
||||
# `(parent_id, position)` is the complete, minimal source of truth. `path`,
|
||||
# `depth`, `numchild` are ALL recomputable from it; `path` exists only so reads
|
||||
# stay indexed prefix scans (`path__startswith`) instead of recursion.
|
||||
#
|
||||
# Fractional key invariant: for any two sibling keys A < B there is always a key
|
||||
# strictly between them (densely-ordered strings -- append a digit when A and B
|
||||
# are adjacent). So `key_between(A, B)` lets you place/insert/reorder a node by
|
||||
# touching ONLY that node.
|
||||
#
|
||||
# ----------------------------------------------------------------------
|
||||
# Operation semantics (the payoff)
|
||||
# ----------------------------------------------------------------------
|
||||
# * Insert between A and B: position = key_between(A.position, B.position);
|
||||
# path = parent.path + SEP + position. ONE row. Siblings untouched -- no
|
||||
# renumber, no `_layout`, no parking. The O(width) mid-insert -> ~O(1).
|
||||
# * Reorder within siblings: recompute only the moved node's position between
|
||||
# its new neighbours, then one `_reprefix` of its own subtree.
|
||||
# * Move to a new parent: parent_id + a new position among the new siblings +
|
||||
# one set-based subtree `_reprefix` -- same single statement as today.
|
||||
# * Reads: unchanged. `path__startswith(prefix + SEP)` for strict descendants
|
||||
# (the separator makes prefix matching unambiguous), `order_by("path")` for
|
||||
# DFS order. `position` must not contain SEP.
|
||||
#
|
||||
# ----------------------------------------------------------------------
|
||||
# Gains
|
||||
# ----------------------------------------------------------------------
|
||||
# * No sibling renumbering, ever -> mid-insert/reorder are single-node; the
|
||||
# per-sibling loop (and any CTE written to speed it) becomes unnecessary.
|
||||
# * No ceilings: variable-length path removes the ~63-level depth cap and the
|
||||
# siblings-per-node cap.
|
||||
# * Concurrency: concurrent inserts at different spots compute different keys
|
||||
# with no shared "max+1" counter and no sibling-row locks; same-spot inserts
|
||||
# collide only on the (parent, position) unique constraint and retry. This is
|
||||
# the real write-concurrency win over treebeard.
|
||||
#
|
||||
# ----------------------------------------------------------------------
|
||||
# Costs / things to get right
|
||||
# ----------------------------------------------------------------------
|
||||
# * `path` is effectively unbounded (fractional keys grow under adversarial
|
||||
# repeated-between inserts) -> TextField. MySQL cannot put a UNIQUE index on
|
||||
# a long/text column (767/3072-byte limit), so uniqueness lives on a
|
||||
# `path_hash` (sha1) column, with a prefix index on `path` for LIKE.
|
||||
# * One-time migration + end of treebeard byte-compat: backfill `position` from
|
||||
# each node's current sibling (path-step) order, then recompute every `path`
|
||||
# in the SEP encoding. After this, hot-swapping BACK to treebeard is no
|
||||
# longer possible.
|
||||
# * Own `key_between(a, b)`: ~100 lines, pure Python, no dependency (base-N
|
||||
# midpoint, append a digit when neighbours are adjacent). It is the
|
||||
# correctness core -- test it hard (adjacent keys, empty bounds, long chains).
|
||||
# * Occasional key renormalisation: if a hot spot grows keys long, a rare
|
||||
# maintenance pass reassigns short keys to a parent's children (same class as
|
||||
# `rebuild()`, off the hot path).
|
||||
#
|
||||
# ----------------------------------------------------------------------
|
||||
# Net
|
||||
# ----------------------------------------------------------------------
|
||||
# parent_id = structure, position = order, path = indexed read cache (with depth
|
||||
# /numchild), all caches rebuildable from the first two. Deletes the renumber
|
||||
# problem and the depth/width ceilings; strongest concurrency story. Cost: one
|
||||
# irreversible migration, a TextField path + hash for MySQL uniqueness, and
|
||||
# owning key generation. Reads are unchanged.
|
||||
# ======================================================================
|
||||
146
cms/utils/page.py
Normal file
146
cms/utils/page.py
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
import re
|
||||
|
||||
from django.urls import NoReverseMatch, reverse
|
||||
from django.utils.encoding import force_str
|
||||
|
||||
from cms.constants import PAGE_USERNAME_MAX_LENGTH
|
||||
from cms.utils import get_current_site, get_language_from_request
|
||||
from cms.utils.conf import get_cms_setting
|
||||
|
||||
SUFFIX_REGEX = re.compile(r'^(.*)-(\d+)$')
|
||||
|
||||
|
||||
def get_page_template_from_request(request):
|
||||
"""
|
||||
Gets a valid template from different sources or falls back to the default
|
||||
template.
|
||||
"""
|
||||
templates = get_cms_setting('TEMPLATES')
|
||||
template_names = frozenset(pair[0] for pair in templates)
|
||||
|
||||
if not templates:
|
||||
# no templates defined, CMS is running headless
|
||||
return None
|
||||
|
||||
if len(templates) == 1:
|
||||
# there's only one template
|
||||
# avoid any further computation
|
||||
return templates[0][0]
|
||||
|
||||
manual_template = request.GET.get('template')
|
||||
|
||||
if manual_template and manual_template in template_names:
|
||||
return manual_template
|
||||
|
||||
if request.current_page:
|
||||
return request.current_page.get_template()
|
||||
return get_cms_setting('TEMPLATES')[0][0]
|
||||
|
||||
|
||||
def get_clean_username(user):
|
||||
try:
|
||||
username = force_str(user)
|
||||
except AttributeError:
|
||||
# AnonymousUser may not have USERNAME_FIELD
|
||||
username = "anonymous"
|
||||
else:
|
||||
# limit changed_by and created_by to avoid problems with Custom User Model
|
||||
if len(username) > PAGE_USERNAME_MAX_LENGTH:
|
||||
username = f'{username[:PAGE_USERNAME_MAX_LENGTH - 15]}... (id={user.pk})'
|
||||
return username
|
||||
|
||||
|
||||
def get_page_queryset(site, draft=True, published=False): # pragma: no cover
|
||||
from warnings import warn
|
||||
|
||||
from cms.models import Page
|
||||
from cms.utils.compat.warnings import RemovedInDjangoCMS60Warning
|
||||
|
||||
warn(
|
||||
"cms.utils.page.get_page_queryset is deprecated. "
|
||||
"Use Page.objects.on_site(site) instead.",
|
||||
RemovedInDjangoCMS60Warning,
|
||||
stacklevel=2,
|
||||
)
|
||||
return Page.objects.on_site(site)
|
||||
|
||||
|
||||
def get_page_from_request(request, use_path=None, clean_path=None):
|
||||
"""
|
||||
Gets the current page from a request object.
|
||||
|
||||
URLs can be of the following form (this should help understand the code):
|
||||
http://server.whatever.com/<some_path>/"pages-root"/some/page/slug
|
||||
|
||||
<some_path>: This can be anything, and should be stripped when resolving
|
||||
pages names. This means the CMS is not installed at the root of the
|
||||
server's URLs.
|
||||
"pages-root" This is the root of Django urls for the CMS. It is, in essence
|
||||
an empty page slug (slug == '')
|
||||
|
||||
The page slug can then be resolved to a Page model object
|
||||
"""
|
||||
from cms.models import PageUrl
|
||||
|
||||
if hasattr(request, '_current_page_cache'):
|
||||
# The following is set by CurrentPageMiddleware
|
||||
return request._current_page_cache
|
||||
|
||||
if clean_path is None:
|
||||
clean_path = not bool(use_path)
|
||||
|
||||
path = request.path_info if use_path is None else use_path
|
||||
|
||||
if clean_path:
|
||||
try:
|
||||
pages_root = reverse("pages-root")
|
||||
if path.startswith(pages_root):
|
||||
path = path[len(pages_root):]
|
||||
|
||||
# strip any final slash
|
||||
if path.endswith("/"):
|
||||
path = path[:-1]
|
||||
except NoReverseMatch:
|
||||
pass
|
||||
|
||||
site = get_current_site(request)
|
||||
page_urls = (
|
||||
PageUrl
|
||||
.objects
|
||||
.get_for_site(site)
|
||||
.filter(path=path)
|
||||
.select_related("page", "page__site")
|
||||
)
|
||||
page_urls = list(page_urls) # force queryset evaluation to save 1 query
|
||||
try:
|
||||
page = page_urls[0].page
|
||||
if page_urls[0].language == get_language_from_request(request):
|
||||
page.urls_cache = {url.language: url for url in page_urls}
|
||||
except IndexError:
|
||||
page = None
|
||||
return page
|
||||
|
||||
|
||||
def get_available_slug(site, path, language, suffix='copy', modified=False):
|
||||
"""
|
||||
Generates slug for path.
|
||||
If path is used, appends the value of suffix to the end.
|
||||
"""
|
||||
from cms.models.pagemodel import PageUrl
|
||||
|
||||
base, _, slug = path.rpartition('/')
|
||||
page_urls = PageUrl.objects.get_for_site(site, path=path, language=language)
|
||||
|
||||
if page_urls.exists():
|
||||
match = SUFFIX_REGEX.match(slug)
|
||||
|
||||
if match and modified:
|
||||
_next = int(match.groups()[-1]) + 1
|
||||
slug = SUFFIX_REGEX.sub(f'\\g<1>-{_next}', slug)
|
||||
elif suffix:
|
||||
slug += '-' + suffix + '-2'
|
||||
else:
|
||||
slug += '-2'
|
||||
path = f'{base}/{slug}' if base else slug
|
||||
return get_available_slug(site, path, language, suffix, modified=True)
|
||||
return slug
|
||||
569
cms/utils/page_permissions.py
Normal file
569
cms/utils/page_permissions.py
Normal file
|
|
@ -0,0 +1,569 @@
|
|||
from functools import wraps
|
||||
|
||||
from cms.cache.permissions import get_permission_cache, set_permission_cache
|
||||
from cms.constants import GRANT_ALL_PERMISSIONS
|
||||
from cms.models import Page, PermissionTuple
|
||||
from cms.utils.compat.dj import available_attrs
|
||||
from cms.utils.conf import get_cms_setting
|
||||
from cms.utils.permissions import (
|
||||
cached_func,
|
||||
get_model_permission_codename,
|
||||
get_page_actions_for_user,
|
||||
has_global_permission,
|
||||
)
|
||||
|
||||
PAGE_ADD_CODENAME = get_model_permission_codename(Page, 'add')
|
||||
PAGE_CHANGE_CODENAME = get_model_permission_codename(Page, 'change')
|
||||
PAGE_DELETE_CODENAME = get_model_permission_codename(Page, 'delete')
|
||||
PAGE_PUBLISH_CODENAME = get_model_permission_codename(Page, 'publish')
|
||||
PAGE_VIEW_CODENAME = get_model_permission_codename(Page, 'view')
|
||||
|
||||
|
||||
# Maps an action to the required Django auth permission codes
|
||||
_django_permissions_by_action = {
|
||||
'add_page': [PAGE_ADD_CODENAME, PAGE_CHANGE_CODENAME],
|
||||
'change_page': [PAGE_CHANGE_CODENAME],
|
||||
'change_page_advanced_settings': [PAGE_CHANGE_CODENAME],
|
||||
'change_page_permissions': [PAGE_CHANGE_CODENAME],
|
||||
'delete_page': [PAGE_CHANGE_CODENAME, PAGE_DELETE_CODENAME],
|
||||
'delete_page_translation': [PAGE_CHANGE_CODENAME, PAGE_DELETE_CODENAME],
|
||||
'move_page': [PAGE_CHANGE_CODENAME],
|
||||
'publish_page': [PAGE_CHANGE_CODENAME, PAGE_PUBLISH_CODENAME],
|
||||
}
|
||||
|
||||
|
||||
def _get_all_placeholders(page, language=None):
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
from cms.models import PageContent, Placeholder
|
||||
|
||||
page_contents = PageContent.admin_manager.filter(page=page)
|
||||
if language:
|
||||
page_contents = page_contents.filter(language=language)
|
||||
content_type = ContentType.objects.get_for_model(PageContent)
|
||||
return Placeholder.objects.filter(
|
||||
content_type=content_type,
|
||||
object_id__in=page_contents.values_list('pk', flat=True)
|
||||
)
|
||||
|
||||
|
||||
def _check_delete_translation(user, page, language, site=None):
|
||||
return user_can_change_page(user, page, site=site)
|
||||
|
||||
|
||||
def _get_page_permission_tuples_for_action(user, site, action, check_global=True, use_cache=True):
|
||||
if user.is_superuser or not get_cms_setting('PERMISSION'):
|
||||
# got superuser, or permissions aren't enabled?
|
||||
# just return grant all mark
|
||||
return GRANT_ALL_PERMISSIONS
|
||||
|
||||
if check_global and has_global_permission(user, site, action=action, use_cache=use_cache):
|
||||
return GRANT_ALL_PERMISSIONS
|
||||
|
||||
if use_cache:
|
||||
# read from cache if possible
|
||||
cached = get_permission_cache(user, site, action)
|
||||
get_page_actions = get_page_actions_for_user
|
||||
else:
|
||||
cached = None
|
||||
get_page_actions = get_page_actions_for_user.without_cache
|
||||
|
||||
if cached is not None:
|
||||
return cached
|
||||
|
||||
page_actions = get_page_actions(user, site)
|
||||
# Set cache for all actions calculated
|
||||
for act, page_paths in page_actions.items():
|
||||
set_permission_cache(user, site, act, list(page_paths))
|
||||
return page_actions[action]
|
||||
|
||||
|
||||
def auth_permission_required(action):
|
||||
def decorator(func):
|
||||
@wraps(func, assigned=available_attrs(func))
|
||||
def wrapper(user, *args, **kwargs):
|
||||
if not user.is_authenticated:
|
||||
return False
|
||||
|
||||
permissions = _django_permissions_by_action[action]
|
||||
|
||||
if not user.has_perms(permissions):
|
||||
# Fail fast if the user does not have permissions
|
||||
# in Django to perform the action.
|
||||
return False
|
||||
|
||||
permissions_enabled = get_cms_setting('PERMISSION')
|
||||
|
||||
if not user.is_superuser and permissions_enabled:
|
||||
return func(user, *args, **kwargs)
|
||||
return True
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
|
||||
def change_permission_required(func):
|
||||
@wraps(func, assigned=available_attrs(func))
|
||||
def wrapper(user, page, site=None):
|
||||
if not user_can_change_page(user, page, site=site):
|
||||
return False
|
||||
return func(user, page, site=site)
|
||||
return wrapper
|
||||
|
||||
|
||||
def skip_if_permissions_disabled(func):
|
||||
@wraps(func, assigned=available_attrs(func))
|
||||
def wrapper(user, page, site=None):
|
||||
if not get_cms_setting('PERMISSION'):
|
||||
return True
|
||||
return func(user, page, site=site)
|
||||
return wrapper
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('add_page')
|
||||
def user_can_add_page(user, site=None):
|
||||
if site is None:
|
||||
import warnings
|
||||
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
from cms.utils.compat.warnings import RemovedInDjangoCMS60Warning
|
||||
|
||||
warnings.warn(
|
||||
"user_can_add_page() called without site. This may lead to unexpected behavior. "
|
||||
"Use user_can_add_page(user, site) instead.",
|
||||
RemovedInDjangoCMS60Warning,
|
||||
stacklevel=2,
|
||||
)
|
||||
site = Site.objects.get_current()
|
||||
return has_global_permission(user, site, action='add_page')
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('add_page')
|
||||
def user_can_add_subpage(user, target, site=None):
|
||||
"""
|
||||
Return true if the current user has permission to add a new page
|
||||
under target.
|
||||
:param user:
|
||||
:param target: a Page object
|
||||
:param site: optional Site object (not just PK)
|
||||
:return: Boolean
|
||||
"""
|
||||
has_perm = has_generic_permission(
|
||||
page=target,
|
||||
user=user,
|
||||
action='add_page',
|
||||
site=site,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('change_page')
|
||||
def user_can_change_page(user, page, site=None):
|
||||
can_change = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='change_page',
|
||||
site=site,
|
||||
)
|
||||
return can_change
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('delete_page')
|
||||
def user_can_delete_page(user, page, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='delete_page',
|
||||
site=site,
|
||||
)
|
||||
|
||||
if not has_perm:
|
||||
return False
|
||||
|
||||
placeholders = _get_all_placeholders(page)
|
||||
for placeholder in placeholders:
|
||||
if not placeholder.has_delete_plugins_permission(user, [placeholder.source.language]):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('delete_page_translation')
|
||||
def user_can_delete_page_translation(user, page, language, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='delete_page_translation',
|
||||
site=site,
|
||||
)
|
||||
|
||||
if not has_perm:
|
||||
return False
|
||||
|
||||
placeholders = (
|
||||
_get_all_placeholders(page, language)
|
||||
.filter(cmsplugin__language=language)
|
||||
.distinct()
|
||||
)
|
||||
|
||||
for placeholder in placeholders.iterator():
|
||||
if not placeholder.has_delete_plugins_permission(user, [language]):
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('publish_page')
|
||||
def user_can_publish_page(user, page, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='publish_page',
|
||||
site=site,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('change_page_advanced_settings')
|
||||
def user_can_change_page_advanced_settings(user, page, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='change_page_advanced_settings',
|
||||
site=site,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('change_page_permissions')
|
||||
def user_can_change_page_permissions(user, page, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='change_page_permissions',
|
||||
site=site,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('move_page')
|
||||
def user_can_move_page(user, page, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='move_page',
|
||||
site=site,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
def user_can_view_page(user, page, site=None):
|
||||
if site is None:
|
||||
from django.contrib.sites.models import Site
|
||||
|
||||
site = Site.objects._get_site_by_id(page.site_id)
|
||||
|
||||
if user.is_superuser:
|
||||
return True
|
||||
|
||||
public_for = get_cms_setting('PUBLIC_FOR')
|
||||
can_see_unrestricted = public_for == 'all' or (public_for == 'staff' and user.is_staff)
|
||||
|
||||
# inherited and direct view permissions
|
||||
is_restricted = page.has_view_restrictions(site)
|
||||
|
||||
if not is_restricted and can_see_unrestricted:
|
||||
# Page has no restrictions and project is configured
|
||||
# to allow everyone to see unrestricted pages.
|
||||
return True
|
||||
elif not user.is_authenticated:
|
||||
# Page has restrictions or project is configured
|
||||
# to require staff user status to see pages.
|
||||
return False
|
||||
|
||||
if user_can_view_all_pages(user, site=site):
|
||||
return True
|
||||
|
||||
if not is_restricted:
|
||||
# Page has no restrictions but user can't see unrestricted pages
|
||||
return False
|
||||
|
||||
if user_can_change_page(user, page):
|
||||
# If user has change permissions on a page
|
||||
# then he can automatically view it.
|
||||
return True
|
||||
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='view_page',
|
||||
check_global=False,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('change_page')
|
||||
def user_can_view_page_draft(user, page, site=None):
|
||||
has_perm = has_generic_permission(
|
||||
page=page,
|
||||
user=user,
|
||||
action='change_page',
|
||||
site=site,
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
@cached_func
|
||||
@auth_permission_required('change_page')
|
||||
def user_can_change_all_pages(user, site):
|
||||
return has_global_permission(user, site, action='change_page')
|
||||
|
||||
|
||||
@auth_permission_required('change_page')
|
||||
def user_can_change_at_least_one_page(user, site, use_cache=True):
|
||||
perm_tuples = get_change_perm_tuples(
|
||||
user=user,
|
||||
site=site,
|
||||
check_global=True,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples == GRANT_ALL_PERMISSIONS or bool(perm_tuples)
|
||||
|
||||
|
||||
@cached_func
|
||||
def user_can_view_all_pages(user, site):
|
||||
if user.is_superuser:
|
||||
return True
|
||||
|
||||
if not get_cms_setting('PERMISSION'):
|
||||
public_for = get_cms_setting('PUBLIC_FOR')
|
||||
can_see_unrestricted = public_for == 'all' or (public_for == 'staff' and user.is_staff)
|
||||
return can_see_unrestricted
|
||||
|
||||
if not user.is_authenticated:
|
||||
return False
|
||||
|
||||
if user.has_perm(PAGE_VIEW_CODENAME):
|
||||
# This is for backwards compatibility.
|
||||
# The previous system allowed any user with the explicit view_page
|
||||
# permission to see all pages.
|
||||
return True
|
||||
|
||||
if user_can_change_all_pages(user, site):
|
||||
# If a user can change all pages then he can see all pages.
|
||||
return True
|
||||
return has_global_permission(user, site, action='view_page')
|
||||
|
||||
|
||||
def user_can_relocate_descendants(user, page, site, parent_page, keep_restrictions=True):
|
||||
"""Allow unreadable descendants only when they stay unreadable at ``parent_page``.
|
||||
|
||||
The caller checks root view access and destination add access separately.
|
||||
``copy_with_descendants`` and ``Page.move_page`` preserve inherited source
|
||||
view restrictions. Destination grants can nevertheless widen access: change
|
||||
permission also confers view access, even on a restricted page.
|
||||
"""
|
||||
unreadable = [
|
||||
descendant for descendant in page.get_descendant_pages()
|
||||
if not user_can_view_page(user, descendant, page.site)
|
||||
]
|
||||
if not unreadable:
|
||||
return True
|
||||
if not keep_restrictions:
|
||||
return False
|
||||
|
||||
# Fetch destination permissions without reusing source-site permission caches.
|
||||
view_permissions = get_view_perm_tuples(user, site, use_cache=False)
|
||||
change_permissions = (
|
||||
get_change_perm_tuples(user, site, use_cache=False)
|
||||
if user.has_perm(PAGE_CHANGE_CODENAME) else []
|
||||
)
|
||||
if GRANT_ALL_PERMISSIONS in (view_permissions, change_permissions):
|
||||
return False
|
||||
|
||||
# An all-zero segment cannot identify an existing page. Only permissions
|
||||
# inherited from the destination ancestors can match these prospective paths.
|
||||
root_path = (parent_page.path if parent_page else '') + '0' * Page.steplen
|
||||
destination_permissions = [PermissionTuple(perm) for perm in (*view_permissions, *change_permissions)]
|
||||
return not any(
|
||||
permission.contains(root_path + descendant.path[len(page.path):])
|
||||
for descendant in unreadable
|
||||
for permission in destination_permissions
|
||||
)
|
||||
|
||||
|
||||
def user_can_copy_descendants(user, page, site, parent_page, copy_permissions):
|
||||
"""Whether ``page`` may be copied below ``parent_page`` with its descendants."""
|
||||
return user_can_relocate_descendants(
|
||||
user, page, site, parent_page, keep_restrictions=copy_permissions
|
||||
)
|
||||
|
||||
|
||||
def user_can_move_descendants(user, page, site, parent_page):
|
||||
"""Whether ``page`` may be moved below ``parent_page`` with its descendants.
|
||||
|
||||
A move always preserves the subtree's view restrictions, so only the
|
||||
destination's own grants can widen access.
|
||||
"""
|
||||
return user_can_relocate_descendants(user, page, site, parent_page)
|
||||
|
||||
|
||||
def get_add_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""
|
||||
Give a list of page where the user has add page rights or the string
|
||||
"All" if the user has all rights.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='add_page',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_change_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""
|
||||
Give a list of page where the user has edit rights or the string "All" if
|
||||
the user has all rights.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='change_page',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_change_advanced_settings_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""
|
||||
Give a list of page where the user can change advanced settings or the
|
||||
string "All" if the user has all rights.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='change_page_advanced_settings',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_change_permissions_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""Give a list of page where the user can change permissions.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='change_page_permissions',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_delete_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""
|
||||
Give a list of page where the user has delete rights or the string "All" if
|
||||
the user has all rights.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='delete_page',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_move_page_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""Give a list of pages which user can move.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='move_page',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_publish_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""
|
||||
Give a list of page where the user has publish rights or the string "All" if
|
||||
the user has all rights.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='publish_page',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def get_view_perm_tuples(user, site, check_global=True, use_cache=True):
|
||||
"""Give a list of pages which user can view.
|
||||
"""
|
||||
perm_tuples = _get_page_permission_tuples_for_action(
|
||||
user=user,
|
||||
site=site,
|
||||
action='view_page',
|
||||
check_global=check_global,
|
||||
use_cache=use_cache,
|
||||
)
|
||||
return perm_tuples
|
||||
|
||||
|
||||
def has_generic_permission(page, user, action, site=None, check_global=True, use_cache=True):
|
||||
from django.contrib.sites.models import Site
|
||||
if page is None and site is None:
|
||||
import warnings
|
||||
|
||||
from cms.utils.compat.warnings import RemovedInDjangoCMS60Warning
|
||||
|
||||
warnings.warn(
|
||||
"has_generic_permission() called without site. This may lead to unexpected behavior. "
|
||||
"Use has_generic_permission(page, user, action, site) instead.",
|
||||
RemovedInDjangoCMS60Warning,
|
||||
stacklevel=2,
|
||||
)
|
||||
site = Site.objects.get_current()
|
||||
elif site is None:
|
||||
site = Site.objects._get_site_by_id(page.site_id)
|
||||
|
||||
page_path = page.path
|
||||
actions_map = {
|
||||
'add_page': get_add_perm_tuples,
|
||||
'change_page': get_change_perm_tuples,
|
||||
'change_page_advanced_settings': get_change_advanced_settings_perm_tuples,
|
||||
'change_page_permissions': get_change_permissions_perm_tuples,
|
||||
'delete_page': get_delete_perm_tuples,
|
||||
'delete_page_translation': get_delete_perm_tuples,
|
||||
'publish_page': get_publish_perm_tuples,
|
||||
'move_page': get_move_page_perm_tuples,
|
||||
'view_page': get_view_perm_tuples,
|
||||
}
|
||||
|
||||
func = actions_map[action]
|
||||
|
||||
page_perms = func(user, site, check_global=check_global, use_cache=use_cache)
|
||||
return page_perms == GRANT_ALL_PERMISSIONS or any(
|
||||
PermissionTuple(perm).contains(page_path) for perm in page_perms
|
||||
)
|
||||
510
cms/utils/permissions.py
Normal file
510
cms/utils/permissions.py
Normal file
|
|
@ -0,0 +1,510 @@
|
|||
from collections import defaultdict
|
||||
from contextlib import contextmanager
|
||||
from contextvars import ContextVar
|
||||
from functools import lru_cache, wraps
|
||||
|
||||
from django.contrib.auth import get_permission_codename, get_user_model
|
||||
from django.contrib.auth.models import Group
|
||||
from django.db.models import Q
|
||||
|
||||
from cms.constants import ROOT_USER_LEVEL, SCRIPT_USERNAME
|
||||
from cms.exceptions import NoPermissionsException
|
||||
from cms.models import GlobalPagePermission, PagePermission
|
||||
from cms.utils.compat.dj import available_attrs
|
||||
from cms.utils.conf import get_cms_setting
|
||||
from cms.utils.page import get_clean_username
|
||||
|
||||
|
||||
class _CurrentUserRef:
|
||||
"""Identity-comparable holder for the value of :data:`_current_user`.
|
||||
|
||||
``request.user`` is a ``SimpleLazyObject``, and asgiref compares context
|
||||
variables when it restores a context after handing control back to the
|
||||
event loop (``asgiref.sync._restore_context``). Comparing a lazy user
|
||||
evaluates it, which runs the auth backend's database query on the event
|
||||
loop thread and raises ``SynchronousOnlyOperation`` -- in some asgiref
|
||||
versions before the result future is completed, hanging the request.
|
||||
|
||||
Storing the user behind this wrapper keeps the context variable cheap to
|
||||
compare (default identity equality) so the user is only evaluated where
|
||||
django CMS actually reads it, in synchronous code.
|
||||
"""
|
||||
|
||||
__slots__ = ('user',)
|
||||
|
||||
def __init__(self, user):
|
||||
self.user = user
|
||||
|
||||
|
||||
# context variable support for async-safe user tracking
|
||||
_current_user: ContextVar = ContextVar('current_user', default=None)
|
||||
|
||||
|
||||
def set_current_user(user):
|
||||
"""
|
||||
Assigns current user from request to context variable, used by
|
||||
CurrentUserMiddleware.
|
||||
|
||||
Returns the ``contextvars.Token`` for the change, so callers can restore
|
||||
the previous value with :func:`reset_current_user`.
|
||||
"""
|
||||
return _current_user.set(_CurrentUserRef(user) if user is not None else None)
|
||||
|
||||
|
||||
def reset_current_user(token):
|
||||
"""
|
||||
Restores the current user context variable to the value it held before the
|
||||
``set_current_user`` call that produced ``token``.
|
||||
"""
|
||||
_current_user.reset(token)
|
||||
|
||||
|
||||
def get_current_user():
|
||||
"""
|
||||
Returns current user, or None
|
||||
"""
|
||||
ref = _current_user.get()
|
||||
return ref.user if ref is not None else None
|
||||
|
||||
|
||||
def get_current_user_name():
|
||||
current_user = get_current_user()
|
||||
|
||||
if not current_user:
|
||||
return SCRIPT_USERNAME
|
||||
return get_clean_username(current_user)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def current_user(user):
|
||||
"""
|
||||
Changes the current user just within a context.
|
||||
"""
|
||||
old_user = get_current_user()
|
||||
set_current_user(user)
|
||||
yield
|
||||
set_current_user(old_user)
|
||||
|
||||
|
||||
def get_model_permission_codename(model, action):
|
||||
opts = model._meta
|
||||
return opts.app_label + '.' + get_permission_codename(action, opts)
|
||||
|
||||
|
||||
def user_can_view_placeholder_source(user, source):
|
||||
"""Whether ``user`` may view the placeholders attached to ``source``.
|
||||
|
||||
Viewing the (read-only) structure board of a frontend-editable object
|
||||
requires only *view* permission; mutating its plugins stays gated by
|
||||
change permission at the plugin endpoints. This lets headless reviewers
|
||||
inspect content structure without edit rights.
|
||||
|
||||
Honours a custom ``has_placeholder_view_permission`` hook on the object
|
||||
and otherwise grants access to users holding the model/object ``view`` or
|
||||
``change`` permission (change implies the right to view).
|
||||
"""
|
||||
if hasattr(source, "has_placeholder_view_permission"):
|
||||
return source.has_placeholder_view_permission(user)
|
||||
model = type(source)
|
||||
perms = (
|
||||
get_model_permission_codename(model, "view"),
|
||||
get_model_permission_codename(model, "change"),
|
||||
)
|
||||
return any(user.has_perm(perm) or user.has_perm(perm, source) for perm in perms)
|
||||
|
||||
|
||||
def _has_global_permission(user, site, action):
|
||||
if not user.is_authenticated:
|
||||
return False
|
||||
|
||||
if user.is_superuser:
|
||||
return True
|
||||
|
||||
codename = get_model_permission_codename(GlobalPagePermission, action=action)
|
||||
|
||||
if not user.has_perm(codename):
|
||||
return False
|
||||
|
||||
if not get_cms_setting('PERMISSION'):
|
||||
return True
|
||||
|
||||
has_perm = (
|
||||
GlobalPagePermission
|
||||
.objects
|
||||
.get_with_change_permissions(user, site.pk)
|
||||
.exists()
|
||||
)
|
||||
return has_perm
|
||||
|
||||
|
||||
def _global_permission_flags(queryset):
|
||||
"""OR together the ``can_*`` flags of every row in ``queryset``."""
|
||||
flags = GlobalPagePermission.get_all_permissions()
|
||||
granted = set()
|
||||
|
||||
for row in queryset.values(*flags):
|
||||
granted.update(flag for flag in flags if row[flag])
|
||||
return granted
|
||||
|
||||
|
||||
def _managed_global_permission_flags(queryset):
|
||||
"""Like :func:`_global_permission_flags`, but empty without ``can_change_permissions``.
|
||||
|
||||
Holding a flag on a site is not enough to hand it out there: the user must
|
||||
also be allowed to manage permissions on that site.
|
||||
"""
|
||||
granted = _global_permission_flags(queryset)
|
||||
return granted if "can_change_permissions" in granted else set()
|
||||
|
||||
|
||||
def get_grantable_global_permissions(user, site_ids=None):
|
||||
"""Return the ``can_*`` flags ``user`` may hand out through a global permission.
|
||||
|
||||
A delegated permission manager must never grant a right they do not hold
|
||||
themselves (see ``docs/explanation/permissions.rst``), so what is grantable
|
||||
depends on the sites the grant would cover:
|
||||
|
||||
* ``site_ids=None`` -- the union of the flags the user holds on any site.
|
||||
The widest set they could ever grant, used to decide which fields to
|
||||
offer at all.
|
||||
* ``site_ids=[]`` -- an empty ``GlobalPagePermission.sites`` means "every
|
||||
site", so granting requires an equally unrestricted grant of the flag.
|
||||
* a non-empty list -- the flags held on *every* one of those sites. A flag
|
||||
held on one site alone cannot be used to grant it on another.
|
||||
|
||||
For ``[]`` and a list of sites, nothing is grantable on a site where the user
|
||||
lacks ``can_change_permissions``, so ``"can_change_permissions" in result``
|
||||
tells whether the user may manage permissions on all of those sites.
|
||||
|
||||
In every case a flag is only grantable if the user also holds the Django
|
||||
model permissions that page actions require alongside it.
|
||||
"""
|
||||
all_flags = set(GlobalPagePermission.get_all_permissions())
|
||||
|
||||
if not user or not user.is_authenticated:
|
||||
return set()
|
||||
|
||||
if user.is_superuser or not get_cms_setting('PERMISSION'):
|
||||
return all_flags
|
||||
|
||||
if site_ids is None:
|
||||
held = _global_permission_flags(GlobalPagePermission.objects.with_user(user))
|
||||
elif not site_ids:
|
||||
held = _managed_global_permission_flags(
|
||||
GlobalPagePermission.objects.with_user(user).filter(sites__isnull=True)
|
||||
)
|
||||
else:
|
||||
held = all_flags
|
||||
for site_id in site_ids:
|
||||
held &= _managed_global_permission_flags(
|
||||
GlobalPagePermission.objects.get_with_site(user, site_id)
|
||||
)
|
||||
if not held:
|
||||
break
|
||||
return held & _flags_with_django_permissions(user)
|
||||
|
||||
|
||||
# ``can_view`` has no Django permission counterpart: viewing restricted pages
|
||||
# is governed by CMS permissions alone.
|
||||
_global_permission_actions = {
|
||||
"can_add": "add_page",
|
||||
"can_change": "change_page",
|
||||
"can_delete": "delete_page",
|
||||
"can_publish": "publish_page",
|
||||
"can_change_advanced_settings": "change_page_advanced_settings",
|
||||
"can_change_permissions": "change_page_permissions",
|
||||
"can_move_page": "move_page",
|
||||
}
|
||||
|
||||
|
||||
def _flags_with_django_permissions(user):
|
||||
"""Return the ``can_*`` flags whose Django model permissions ``user`` holds.
|
||||
|
||||
A CMS flag alone does not let a user act: the page permission checks also
|
||||
require the corresponding Django permissions (``auth_permission_required``).
|
||||
A manager holding the flag but not the Django permission does not have the
|
||||
right, so they must not be able to hand it out either.
|
||||
"""
|
||||
from cms.utils.page_permissions import _django_permissions_by_action
|
||||
|
||||
return {
|
||||
flag for flag in GlobalPagePermission.get_all_permissions()
|
||||
if flag not in _global_permission_actions
|
||||
or user.has_perms(_django_permissions_by_action[_global_permission_actions[flag]])
|
||||
}
|
||||
|
||||
|
||||
def user_can_add_global_permissions(user, site):
|
||||
return _has_global_permission(user, site, action='add')
|
||||
|
||||
|
||||
def user_can_change_global_permissions(user, site):
|
||||
return _has_global_permission(user, site, action='change')
|
||||
|
||||
|
||||
def user_can_delete_global_permissions(user, site):
|
||||
return _has_global_permission(user, site, action='delete')
|
||||
|
||||
|
||||
def get_user_permission_level(user, site):
|
||||
"""
|
||||
Returns highest user level from the page/permission hierarchy on which
|
||||
user haves can_change_permission. Also takes look into user groups. Higher
|
||||
level equals to lower number. Users on top of hierarchy have level 0. Level
|
||||
is the same like page.depth attribute.
|
||||
|
||||
Example:
|
||||
A,W level 0
|
||||
/ \
|
||||
user B,GroupE level 1
|
||||
/ \
|
||||
C,X D,Y,W level 2
|
||||
|
||||
Users A, W have user level 0. GroupE and all his users have user level 1
|
||||
If user D is a member of GroupE, his user level will be 1, otherwise is
|
||||
2.
|
||||
|
||||
"""
|
||||
if not user.is_authenticated:
|
||||
raise NoPermissionsException
|
||||
|
||||
if user.is_superuser or not get_cms_setting('PERMISSION'):
|
||||
return ROOT_USER_LEVEL
|
||||
|
||||
has_global_perms = (
|
||||
GlobalPagePermission
|
||||
.objects
|
||||
.get_with_change_permissions(user, site.pk)
|
||||
.exists()
|
||||
)
|
||||
|
||||
if has_global_perms:
|
||||
return ROOT_USER_LEVEL
|
||||
|
||||
try:
|
||||
permission = (
|
||||
PagePermission
|
||||
.objects
|
||||
.get_with_change_permissions(user, site)
|
||||
.select_related('page')
|
||||
.order_by('page__path')
|
||||
)[0]
|
||||
except IndexError:
|
||||
# user isn't assigned to any node
|
||||
raise NoPermissionsException
|
||||
return permission.page.depth
|
||||
|
||||
|
||||
def cached_func(func):
|
||||
@wraps(func, assigned=available_attrs(func))
|
||||
def cached_func(user, *args, **kwargs):
|
||||
from cms.cache.permissions import get_cache_permission_version
|
||||
|
||||
func_cache_name = '_djangocms_cached_func_%s' % func.__name__
|
||||
func_version_name = '%s_version' % func_cache_name
|
||||
version = get_cache_permission_version()
|
||||
|
||||
if not hasattr(user, func_cache_name) or getattr(user, func_version_name, None) != version:
|
||||
cached_func = lru_cache(maxsize=None)(func)
|
||||
setattr(user, func_cache_name, cached_func)
|
||||
setattr(user, func_version_name, version)
|
||||
return getattr(user, func_cache_name)(user, *args, **kwargs)
|
||||
|
||||
# Allows us to access the un-cached function
|
||||
cached_func.without_cache = func
|
||||
return cached_func
|
||||
|
||||
|
||||
def clear_func_cache(user, func):
|
||||
func_cache_name = '_djangocms_cached_func_%s' % func.__name__
|
||||
if hasattr(user, func_cache_name):
|
||||
delattr(user, func_cache_name)
|
||||
func_version_name = '%s_version' % func_cache_name
|
||||
if hasattr(user, func_version_name):
|
||||
delattr(user, func_version_name)
|
||||
|
||||
|
||||
def clear_permission_lru_caches(user):
|
||||
"""
|
||||
Clear all python lru caches used by the permission system
|
||||
"""
|
||||
clear_func_cache(user, get_global_actions_for_user)
|
||||
clear_func_cache(user, get_page_actions_for_user)
|
||||
|
||||
|
||||
@cached_func
|
||||
def get_global_actions_for_user(user, site):
|
||||
actions = set()
|
||||
global_perms = (
|
||||
GlobalPagePermission
|
||||
.objects
|
||||
.get_with_site(user, site.pk)
|
||||
)
|
||||
|
||||
for global_perm in global_perms.iterator():
|
||||
actions.update(global_perm.get_configured_actions())
|
||||
return actions
|
||||
|
||||
|
||||
@cached_func
|
||||
def get_page_actions_for_user(user, site):
|
||||
actions = defaultdict(list)
|
||||
|
||||
page_permissions = (
|
||||
PagePermission
|
||||
.objects
|
||||
.with_user(user)
|
||||
.select_related('page')
|
||||
.filter(page__site=site)
|
||||
)
|
||||
|
||||
for perm in page_permissions.iterator():
|
||||
permission_tuple = perm.grant_on, perm.page.path
|
||||
for action in perm.get_configured_actions():
|
||||
actions[action].append(permission_tuple)
|
||||
return actions
|
||||
|
||||
|
||||
def has_global_permission(user, site, action, use_cache=True):
|
||||
if use_cache:
|
||||
actions = get_global_actions_for_user(user, site)
|
||||
else:
|
||||
actions = get_global_actions_for_user.without_cache(user, site)
|
||||
return action in actions
|
||||
|
||||
|
||||
def get_subordinate_users(user, site):
|
||||
"""
|
||||
Returns users queryset, containing all subordinate users to given user
|
||||
including users created by given user and not assigned to any page.
|
||||
|
||||
Not assigned users must be returned, because they shouldn't get lost, and
|
||||
user should still have possibility to see them.
|
||||
|
||||
Only users created_by given user which are on the same, or lover level are
|
||||
returned.
|
||||
|
||||
If user haves global permissions or is a superuser, then he can see all the
|
||||
users.
|
||||
|
||||
Superusers are never subordinate to a non-superuser, no matter how many
|
||||
permissions the latter holds. Otherwise a delegated administrator could
|
||||
manage - and take over - a superuser account.
|
||||
|
||||
This function is currently used in PagePermissionInlineAdminForm for limit
|
||||
users in permission combobox.
|
||||
|
||||
Example:
|
||||
A,W level 0
|
||||
/ \
|
||||
user B,GroupE level 1
|
||||
Z / \
|
||||
C,X D,Y,W level 2
|
||||
|
||||
Rules: W was created by user, Z was created by user, but is not assigned
|
||||
to any page.
|
||||
|
||||
Will return [user, C, X, D, Y, Z]. W was created by user, but is also
|
||||
assigned to higher level.
|
||||
"""
|
||||
from cms.utils.page_permissions import get_change_permissions_perm_tuples
|
||||
|
||||
def without_superusers(qs):
|
||||
# A non-superuser must never be handed a superuser as a subordinate:
|
||||
# being able to manage the account means being able to take it over.
|
||||
if user.is_superuser:
|
||||
return qs
|
||||
return qs.exclude(is_superuser=True)
|
||||
|
||||
try:
|
||||
user_level = get_user_permission_level(user, site)
|
||||
except NoPermissionsException:
|
||||
# user has no Global or Page permissions.
|
||||
# return only staff users created by user
|
||||
# whose page permission record has no page attached.
|
||||
qs = get_user_model().objects.distinct().filter(
|
||||
Q(is_staff=True) & Q(pageuser__created_by=user) & Q(pagepermission__page=None)
|
||||
)
|
||||
qs = qs.exclude(pk=user.pk).exclude(groups__user__pk=user.pk)
|
||||
return without_superusers(qs)
|
||||
|
||||
if user_level == ROOT_USER_LEVEL:
|
||||
return without_superusers(get_user_model().objects.all())
|
||||
|
||||
from cms.models import PermissionTuple
|
||||
allow_list = Q()
|
||||
for perm_tuple in get_change_permissions_perm_tuples(user, site, check_global=False):
|
||||
allow_list |= PermissionTuple(perm_tuple).allow_list("pagepermission__page")
|
||||
|
||||
# normal query
|
||||
qs = get_user_model().objects.distinct().filter(
|
||||
Q(is_staff=True) & (
|
||||
allow_list & Q(pagepermission__page__depth__gte=user_level)
|
||||
) | (
|
||||
Q(pageuser__created_by=user) & Q(pagepermission__page=None)
|
||||
)
|
||||
)
|
||||
qs = qs.exclude(pk=user.pk).exclude(groups__user__pk=user.pk)
|
||||
return without_superusers(qs)
|
||||
|
||||
|
||||
def get_subordinate_groups(user, site):
|
||||
"""
|
||||
Similar to get_subordinate_users, but returns queryset of Groups instead
|
||||
of Users.
|
||||
"""
|
||||
from cms.utils.page_permissions import get_change_permissions_perm_tuples
|
||||
|
||||
try:
|
||||
user_level = get_user_permission_level(user, site)
|
||||
except NoPermissionsException:
|
||||
# user has no Global or Page permissions.
|
||||
# return only groups created by user
|
||||
# whose page permission record has no page attached.
|
||||
groups = (
|
||||
Group
|
||||
.objects
|
||||
.filter(
|
||||
Q(pageusergroup__created_by=user) & Q(pagepermission__page__isnull=True)
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
# no permission no records
|
||||
# page_id_allow_list is empty
|
||||
return groups
|
||||
|
||||
if user_level == ROOT_USER_LEVEL:
|
||||
return Group.objects.all()
|
||||
|
||||
from cms.models import PermissionTuple
|
||||
allow_list = Q()
|
||||
for perm_tuple in get_change_permissions_perm_tuples(user, site, check_global=False):
|
||||
allow_list |= PermissionTuple(perm_tuple).allow_list("pagepermission__page")
|
||||
|
||||
return Group.objects.distinct().filter(
|
||||
(
|
||||
allow_list & Q(pagepermission__page__depth__gte=user_level)
|
||||
) | (
|
||||
Q(pageusergroup__created_by=user) & Q(pagepermission__page__isnull=True)
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def has_plugin_permission(user, plugin_type, permission_type):
|
||||
"""
|
||||
Checks that a user has permissions for the plugin-type given to perform
|
||||
the action defined in permission_type
|
||||
permission_type should be 'add', 'change' or 'delete'.
|
||||
"""
|
||||
from cms.plugin_pool import plugin_pool
|
||||
try:
|
||||
plugin_class = plugin_pool.get_plugin(plugin_type)
|
||||
codename = get_model_permission_codename(
|
||||
plugin_class.model,
|
||||
action=permission_type,
|
||||
)
|
||||
return user.has_perm(codename)
|
||||
except KeyError:
|
||||
# Grant all permissions for uninstalled plugins, so they do not block
|
||||
# emptying placeholders.
|
||||
return True
|
||||
442
cms/utils/placeholder.py
Normal file
442
cms/utils/placeholder.py
Normal file
|
|
@ -0,0 +1,442 @@
|
|||
import operator
|
||||
import os
|
||||
import warnings
|
||||
from collections import OrderedDict, defaultdict
|
||||
from functools import cache
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.db import connection, models
|
||||
from django.template import (
|
||||
Context,
|
||||
NodeList,
|
||||
Template,
|
||||
TemplateDoesNotExist,
|
||||
TemplateSyntaxError,
|
||||
Variable,
|
||||
engines,
|
||||
)
|
||||
from django.template.base import VariableNode
|
||||
from django.template.loader import get_template
|
||||
from django.template.loader_tags import BlockNode, ExtendsNode, IncludeNode
|
||||
from sekizai.helpers import get_varname
|
||||
|
||||
from cms.exceptions import DuplicatePlaceholderWarning
|
||||
from cms.models import EmptyPageContent, Placeholder
|
||||
from cms.utils.compat.warnings import RemovedInDjangoCMS60Warning
|
||||
from cms.utils.conf import get_cms_setting
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from cms.templatetags.cms_tags import DeclaredPlaceholder
|
||||
|
||||
|
||||
RANGE_START = 128
|
||||
|
||||
|
||||
def _get_nodelist(tpl):
|
||||
if hasattr(tpl, "template"):
|
||||
return tpl.template.nodelist
|
||||
else:
|
||||
return tpl.nodelist
|
||||
|
||||
|
||||
def get_context():
|
||||
if engines is not None:
|
||||
context = Context()
|
||||
context.template = Template("")
|
||||
return context
|
||||
else:
|
||||
return {}
|
||||
|
||||
|
||||
def _get_placeholder_settings():
|
||||
"""Convert CMS_PLACEHOLDER_CONF into a faster to access format since it is accessed many times
|
||||
in each request response cycle:
|
||||
* setting key on the first level dict, scope on the second level
|
||||
* resolve inheritance
|
||||
* Check of template-specific configs exist (expensive to access)
|
||||
"""
|
||||
conf = get_cms_setting("PLACEHOLDER_CONF")
|
||||
template_in_conf = any(".htm" in key for key in conf if key)
|
||||
|
||||
def resolve_inheritance(key, visited=None):
|
||||
if visited is None:
|
||||
visited = set()
|
||||
if key in visited:
|
||||
raise ImproperlyConfigured(f"Circular inheritance detected in CMS_PLACEHOLDER_CONF at key '{key}'")
|
||||
visited.add(key)
|
||||
if "inherit" in conf[key]:
|
||||
return resolve_inheritance(conf[key]["inherit"], visited) | {
|
||||
k: v for k, v in conf[key].items() if k != "inherit"
|
||||
}
|
||||
# Return a copy: the caller updates the result in place, which must
|
||||
# not leak into the inherited-from slot's own configuration.
|
||||
return dict(conf[key])
|
||||
|
||||
new_conf = {}
|
||||
for key, value in conf.items():
|
||||
if "inherit" in value:
|
||||
new_conf[key] = resolve_inheritance(value["inherit"])
|
||||
new_conf[key].update({k: v for k, v in value.items() if k != "inherit"})
|
||||
else:
|
||||
new_conf[key] = value
|
||||
|
||||
settings = defaultdict(dict)
|
||||
for key, value in new_conf.items():
|
||||
for setting, setting_value in value.items():
|
||||
settings[setting][key] = setting_value
|
||||
|
||||
return settings, template_in_conf
|
||||
|
||||
|
||||
_placeholder_settings, _template_in_conf = _get_placeholder_settings()
|
||||
|
||||
|
||||
def _clear_placeholder_conf_cache():
|
||||
# Needed by the override_placeholder_conf context manager for tests
|
||||
global _placeholder_settings, _template_in_conf
|
||||
_placeholder_settings, _template_in_conf = _get_placeholder_settings()
|
||||
|
||||
|
||||
def get_placeholder_conf(setting: str, placeholder: str, template: str | None = None, default=None):
|
||||
"""
|
||||
Returns the placeholder configuration for a given setting. The key would for
|
||||
example be 'plugins' or 'name'.
|
||||
|
||||
Resulting value will be the last from:
|
||||
|
||||
CMS_PLACEHOLDER_CONF[None] (global)
|
||||
CMS_PLACEHOLDER_CONF['template'] (if template is given)
|
||||
CMS_PLACEHOLDER_CONF['placeholder'] (where placeholder denotes a slot)
|
||||
CMS_PLACEHOLDER_CONF['template placeholder'] (if template is given)
|
||||
|
||||
Template is only evaluated if the placeholder configuration contains key with ".html" or ".htm"
|
||||
"""
|
||||
|
||||
if setting not in _placeholder_settings:
|
||||
return default
|
||||
|
||||
if _template_in_conf and template is not None:
|
||||
keys = [f"{template} {placeholder}", placeholder, str(template), None]
|
||||
else:
|
||||
keys = [placeholder, None]
|
||||
|
||||
conf = _placeholder_settings[setting]
|
||||
for key in keys:
|
||||
if (value := conf.get(key, None)) is not None:
|
||||
return value
|
||||
return default
|
||||
|
||||
|
||||
def get_toolbar_plugin_struct(plugins, slot=None, obj=None, page=None):
|
||||
"""
|
||||
Return the list of plugins to render in the toolbar.
|
||||
The dictionary contains the label, the classname and the module for the
|
||||
plugin.
|
||||
Names and modules can be defined on a per-placeholder basis using
|
||||
'plugin_modules' and 'plugin_labels' attributes in CMS_PLACEHOLDER_CONF
|
||||
|
||||
:param plugins: list of plugins
|
||||
:param slot: placeholder slot name
|
||||
:param page: the page
|
||||
:return: list of dictionaries
|
||||
"""
|
||||
template = None
|
||||
|
||||
obj = obj or page
|
||||
if page:
|
||||
import warnings
|
||||
|
||||
warnings.warn(
|
||||
"The 'page' argument is deprecated. Please use 'obj' instead.",
|
||||
RemovedInDjangoCMS60Warning,
|
||||
stacklevel=2,
|
||||
)
|
||||
if obj and hasattr(obj, "get_template"):
|
||||
template = obj.get_template()
|
||||
|
||||
modules = get_placeholder_conf("plugin_modules", slot, template, default={})
|
||||
names = get_placeholder_conf("plugin_labels", slot, template, default={})
|
||||
|
||||
main_list = []
|
||||
|
||||
# plugin.value points to the class name of the plugin
|
||||
# It's added on registration. TIL.
|
||||
for plugin in plugins:
|
||||
main_list.append(
|
||||
{
|
||||
"value": plugin.value,
|
||||
"name": names.get(plugin.value, plugin.name),
|
||||
"module": modules.get(plugin.value, plugin.module),
|
||||
"add_form": plugin.show_add_form and not plugin.is_slot,
|
||||
}
|
||||
)
|
||||
return sorted(main_list, key=operator.itemgetter("module"))
|
||||
|
||||
|
||||
def validate_placeholder_name(name):
|
||||
if not isinstance(name, str):
|
||||
raise ImproperlyConfigured("Placeholder identifier names need to be of type string. ")
|
||||
|
||||
try:
|
||||
name.encode("ascii")
|
||||
except UnicodeEncodeError:
|
||||
raise ImproperlyConfigured(
|
||||
"Placeholder identifiers names may not "
|
||||
"contain non-ascii characters. If you wish your placeholder "
|
||||
"identifiers to contain non-ascii characters when displayed to "
|
||||
"users, please use the CMS_PLACEHOLDER_CONF setting with the 'name' "
|
||||
"key to specify a verbose name."
|
||||
)
|
||||
|
||||
|
||||
def restore_sekizai_context(context, changes):
|
||||
varname = get_varname()
|
||||
sekizai_container = context.get(varname)
|
||||
for key, values in changes.items():
|
||||
sekizai_namespace = sekizai_container[key]
|
||||
for value in values:
|
||||
sekizai_namespace.append(value)
|
||||
|
||||
|
||||
def _scan_placeholders(nodelist, node_class=None, current_block=None, ignore_blocks=None):
|
||||
from cms.templatetags.cms_tags import Placeholder
|
||||
|
||||
if not node_class:
|
||||
node_class = Placeholder
|
||||
|
||||
nodes = []
|
||||
|
||||
if ignore_blocks is None:
|
||||
# List of BlockNode instances to ignore.
|
||||
# This is important to avoid processing overridden block nodes.
|
||||
ignore_blocks = []
|
||||
|
||||
for node in nodelist:
|
||||
# check if this is a placeholder first
|
||||
if isinstance(node, node_class):
|
||||
nodes.append(node)
|
||||
elif isinstance(node, IncludeNode):
|
||||
# if there's an error in the to-be-included template, node.template becomes None
|
||||
if node.template:
|
||||
# Check if it quacks like a template object, if not
|
||||
# presume is a template path and get the object out of it
|
||||
if not callable(getattr(node.template, "render", None)):
|
||||
# If it's a variable there is no way to expand it at this stage so we
|
||||
# need to skip it
|
||||
if isinstance(node.template.var, Variable):
|
||||
continue
|
||||
else:
|
||||
try:
|
||||
template = get_template(node.template.var)
|
||||
except TemplateDoesNotExist:
|
||||
# Include might be inside if else, so no need to error out here
|
||||
# Actual errors will be raised at render time
|
||||
continue
|
||||
else:
|
||||
template = node.template
|
||||
nodes += _scan_placeholders(_get_nodelist(template), node_class, current_block)
|
||||
# handle {% extends ... %} tags
|
||||
elif isinstance(node, ExtendsNode):
|
||||
try:
|
||||
nodes += _get_placeholder_nodes_from_extend(node, node_class)
|
||||
except (TemplateSyntaxError, TemplateDoesNotExist):
|
||||
# If the extends tag uses a variable that's not available in the
|
||||
# scanning context (like CMS_TEMPLATE), we can't resolve the parent
|
||||
# template. Skip scanning this extends node.
|
||||
continue
|
||||
# in block nodes we have to scan for super blocks
|
||||
elif isinstance(node, VariableNode) and current_block:
|
||||
if node.filter_expression.token == "block.super":
|
||||
if not hasattr(current_block.super, "nodelist"):
|
||||
raise TemplateSyntaxError("Cannot render block.super for blocks without a parent.")
|
||||
nodes += _scan_placeholders(_get_nodelist(current_block.super), node_class, current_block.super)
|
||||
# ignore nested blocks which are already handled
|
||||
elif isinstance(node, BlockNode) and node.name in ignore_blocks:
|
||||
continue
|
||||
# if the node has the newly introduced 'child_nodelists' attribute, scan
|
||||
# those attributes for nodelists and recurse them
|
||||
elif hasattr(node, "child_nodelists"):
|
||||
for nodelist_name in node.child_nodelists:
|
||||
if hasattr(node, nodelist_name):
|
||||
subnodelist = getattr(node, nodelist_name)
|
||||
if isinstance(subnodelist, NodeList):
|
||||
if isinstance(node, BlockNode):
|
||||
current_block = node
|
||||
nodes += _scan_placeholders(subnodelist, node_class, current_block, ignore_blocks)
|
||||
# else just scan the node for nodelist instance attributes
|
||||
else:
|
||||
for attr in dir(node):
|
||||
obj = getattr(node, attr)
|
||||
if isinstance(obj, NodeList):
|
||||
if isinstance(node, BlockNode):
|
||||
current_block = node
|
||||
nodes += _scan_placeholders(obj, node_class, current_block, ignore_blocks)
|
||||
return nodes
|
||||
|
||||
|
||||
def get_placeholders(template: str) -> list["DeclaredPlaceholder"]:
|
||||
compiled_template = get_template(template)
|
||||
|
||||
placeholders = []
|
||||
nodes = _scan_placeholders(_get_nodelist(compiled_template))
|
||||
clean_placeholders = []
|
||||
|
||||
for node in nodes:
|
||||
placeholder = node.get_declaration()
|
||||
slot = placeholder.slot
|
||||
|
||||
if slot in clean_placeholders:
|
||||
warnings.warn(
|
||||
f'Duplicate {{% placeholder "{slot}" %}} ' f"in template {template}.",
|
||||
DuplicatePlaceholderWarning,
|
||||
)
|
||||
else:
|
||||
validate_placeholder_name(slot)
|
||||
placeholders.append(placeholder)
|
||||
clean_placeholders.append(slot)
|
||||
return placeholders
|
||||
|
||||
|
||||
if settings.DEBUG is False or os.environ.get("DJANGO_TESTS"):
|
||||
# Cache in production only, so template changes in development
|
||||
# are always reflected without needing a server restart
|
||||
get_placeholders = cache(get_placeholders)
|
||||
|
||||
|
||||
def _get_block_nodes(extend_node):
|
||||
parent = extend_node.get_parent(get_context())
|
||||
parent_nodelist = _get_nodelist(parent)
|
||||
parent_nodes = parent_nodelist.get_nodes_by_type(BlockNode)
|
||||
parent_extend_nodes = parent_nodelist.get_nodes_by_type(ExtendsNode)
|
||||
|
||||
if parent_extend_nodes:
|
||||
# Start at the top
|
||||
# Scan the extends node from the parent (if any)
|
||||
nodes = _get_block_nodes(parent_extend_nodes[0])
|
||||
else:
|
||||
nodes = OrderedDict()
|
||||
|
||||
# Continue with the parent template nodes
|
||||
for node in parent_nodes:
|
||||
nodes[node.name] = node
|
||||
|
||||
# Move on to the current template nodes
|
||||
current_nodes = _get_nodelist(extend_node).get_nodes_by_type(BlockNode)
|
||||
|
||||
for node in current_nodes:
|
||||
if node.name in nodes:
|
||||
# set this node as the super node (for {{ block.super }})
|
||||
node.super = nodes[node.name]
|
||||
nodes[node.name] = node
|
||||
return nodes
|
||||
|
||||
|
||||
def _get_placeholder_nodes_from_extend(extend_node, node_class):
|
||||
"""
|
||||
Returns a list of placeholders found in the parent template(s) of this
|
||||
ExtendsNode
|
||||
"""
|
||||
# This is a dictionary mapping all BlockNode instances found
|
||||
# in the template that contains the {% extends %} tag
|
||||
block_nodes = _get_block_nodes(extend_node)
|
||||
block_names = list(block_nodes.keys())
|
||||
|
||||
placeholders = []
|
||||
|
||||
for block in block_nodes.values():
|
||||
placeholders.extend(_scan_placeholders(_get_nodelist(block), node_class, block, block_names))
|
||||
|
||||
# Scan topmost template for placeholder outside of blocks
|
||||
parent_template = _find_topmost_template(extend_node)
|
||||
placeholders += _scan_placeholders(_get_nodelist(parent_template), node_class, None, block_names)
|
||||
return placeholders
|
||||
|
||||
|
||||
def _find_topmost_template(extend_node):
|
||||
parent_template = extend_node.get_parent(get_context())
|
||||
for node in _get_nodelist(parent_template).get_nodes_by_type(ExtendsNode):
|
||||
# Their can only be one extend block in a template, otherwise django raises an exception
|
||||
return _find_topmost_template(node)
|
||||
# No ExtendsNode
|
||||
return extend_node.get_parent(get_context())
|
||||
|
||||
|
||||
def rescan_placeholders_for_obj(obj: models.Model) -> dict[str, Placeholder]:
|
||||
from cms.models import CMSPlugin, Placeholder
|
||||
|
||||
declared_placeholders = get_declared_placeholders_for_obj(obj)
|
||||
placeholders = {pl.slot: None for pl in declared_placeholders} # Fix order of placeholders in dict
|
||||
|
||||
# Fill in existing placeholders
|
||||
placeholders.update(
|
||||
{
|
||||
placeholder.slot: placeholder
|
||||
for placeholder in Placeholder.objects.get_for_obj(obj)
|
||||
if placeholder.slot in placeholders
|
||||
}
|
||||
)
|
||||
|
||||
# Create missing placeholders
|
||||
new_placeholders = [
|
||||
Placeholder(slot=slot, source=obj) for slot, placeholder in placeholders.items() if placeholder is None
|
||||
]
|
||||
if new_placeholders:
|
||||
if connection.features.can_return_rows_from_bulk_insert:
|
||||
Placeholder.objects.bulk_create(new_placeholders)
|
||||
for placeholder in new_placeholders:
|
||||
# No plugins in newly created placeholder yet
|
||||
placeholder._prefetched_objects_cache = {"cmsplugin_set": CMSPlugin.objects.none()}
|
||||
placeholders[placeholder.slot] = placeholder
|
||||
else:
|
||||
# Some MySql versions do not support returning IDs from bulk_create
|
||||
for placeholder in new_placeholders:
|
||||
placeholder.save()
|
||||
# No plugins in newly created placeholder yet
|
||||
placeholder._prefetched_objects_cache = {"cmsplugin_set": CMSPlugin.objects.none()}
|
||||
placeholders[placeholder.slot] = placeholder
|
||||
|
||||
return placeholders
|
||||
|
||||
|
||||
def get_declared_placeholders_for_obj(obj: models.Model | EmptyPageContent | None) -> list["DeclaredPlaceholder"]:
|
||||
"""Returns declared placeholders for an object. The object is supposed to either have a method
|
||||
``get_placeholder_slots`` which returns the list of placeholders or a method ``get_template``
|
||||
which returns the template path as a string that renders the object. ``get_declared_placeholders`` returns
|
||||
a list of placeholders used in the template by the ``{% placeholder %}`` template tag.
|
||||
"""
|
||||
template = getattr(obj, "get_template", lambda: None)()
|
||||
if template:
|
||||
return get_placeholders(template)
|
||||
|
||||
if hasattr(obj, "get_placeholder_slots"):
|
||||
from cms.templatetags.cms_tags import DeclaredPlaceholder
|
||||
|
||||
return [
|
||||
DeclaredPlaceholder(slot=slot, inherit=False) if isinstance(slot, str) else DeclaredPlaceholder(**slot)
|
||||
for slot in obj.get_placeholder_slots()
|
||||
]
|
||||
raise NotImplementedError(
|
||||
"%s should implement either get_placeholder_slots or get_template" % obj.__class__.__name__
|
||||
)
|
||||
|
||||
|
||||
def get_placeholder_from_slot(
|
||||
placeholder_relation: models.Manager, slot: str, template_obj=None, default_width: int | None = None
|
||||
) -> Placeholder:
|
||||
"""Retrieves the placeholder instance for a PlaceholderRelationField either by scanning the template
|
||||
of the template_obj (if given) or by creating or getting a Placeholder in the database
|
||||
"""
|
||||
if hasattr(template_obj, "get_template"):
|
||||
# Tries to get a placeholder (based on the template for the template_obj
|
||||
# or - if non exists - raises a Placeholder.DoesNotExist exception
|
||||
# Placeholders are marked in the template with {% placeholder %}
|
||||
try:
|
||||
return placeholder_relation.get(slot=slot)
|
||||
except Placeholder.DoesNotExist:
|
||||
return rescan_placeholders_for_obj(template_obj).get(slot)
|
||||
else:
|
||||
# Gets or creates the placeholder in any model. Placeholder is
|
||||
# rendered by {% render_placeholder %}
|
||||
return placeholder_relation.get_or_create(slot=slot, default_width=default_width)[0]
|
||||
560
cms/utils/plugins.py
Normal file
560
cms/utils/plugins.py
Normal file
|
|
@ -0,0 +1,560 @@
|
|||
import logging
|
||||
import sys
|
||||
from collections import OrderedDict, defaultdict, deque
|
||||
from collections.abc import Iterable, Sequence
|
||||
from copy import deepcopy
|
||||
from itertools import starmap
|
||||
from operator import itemgetter
|
||||
|
||||
from django.db import models, transaction
|
||||
from django.http import HttpRequest
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from cms.constants import PLUGIN_ITERATOR_CHUNK_SIZE
|
||||
from cms.exceptions import PluginLimitReached
|
||||
from cms.models.pluginmodel import CMSPlugin
|
||||
from cms.plugin_base import CMSPluginBase
|
||||
from cms.plugin_pool import plugin_pool
|
||||
from cms.utils import get_language_from_request
|
||||
from cms.utils.permissions import has_plugin_permission
|
||||
from cms.utils.placeholder import get_placeholder_conf
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_plugin_class(plugin_type: str) -> type[CMSPluginBase]:
|
||||
"""Returns the plugin class for a given plugin_type (str)"""
|
||||
return plugin_pool.get_plugin(plugin_type)
|
||||
|
||||
|
||||
def get_plugin_model(plugin_type: str) -> type[CMSPlugin]:
|
||||
"""Returns the plugin model class for a given plugin_type (str)"""
|
||||
return get_plugin_class(plugin_type).model
|
||||
|
||||
|
||||
def get_plugins(request, placeholder, template, lang=None):
|
||||
"""
|
||||
Get a list of plugins for a placeholder in a specified template. Respects the placeholder's cache.
|
||||
|
||||
:param request: (HttpRequest) The HTTP request object.
|
||||
:param placeholder: (Placeholder) The placeholder object for which to retrieve plugins.
|
||||
:param template: (Template) The template object in which the placeholder resides (not used).
|
||||
:param lang: (str, optional) The language code for localization. Defaults to None.
|
||||
|
||||
Returns:
|
||||
list: A list of plugins for the specified placeholder in the template.
|
||||
|
||||
Raises:
|
||||
None.
|
||||
|
||||
Examples::
|
||||
|
||||
# Get plugins for a placeholder in a template
|
||||
plugins = get_plugins(request, placeholder, template)
|
||||
|
||||
# Get plugins for a placeholder in a template with specific language
|
||||
plugins = get_plugins(request, placeholder, template, lang="en")
|
||||
"""
|
||||
if not placeholder:
|
||||
return []
|
||||
if not hasattr(placeholder, "_plugins_cache"):
|
||||
assign_plugins(request, [placeholder], template, lang)
|
||||
return placeholder._plugins_cache
|
||||
|
||||
|
||||
def assign_plugins(request, placeholders, template=None, lang=None):
|
||||
"""
|
||||
Fetch all plugins for the given ``placeholders`` and
|
||||
cast them down to the concrete instances in one query
|
||||
per type.
|
||||
|
||||
:param request: The current request.
|
||||
:param placeholders: An iterable of placeholder objects.
|
||||
:param template: (optional) The template object.
|
||||
:param lang: (optional) The language code.
|
||||
|
||||
This method assigns plugins to the given placeholders. It retrieves the plugins from the database based on the
|
||||
placeholders and the language. The plugins are then downcasted to their specific plugin types.
|
||||
|
||||
The plugins are split up by placeholder and stored in a dictionary where the key is the placeholder ID and the
|
||||
value is a list of plugins.
|
||||
|
||||
For each placeholder, if there are plugins assigned to it, the plugins are organized as a layered tree structure.
|
||||
Otherwise, an empty list is assigned.
|
||||
|
||||
The list of all plugins for each placeholder is stored in the `_all_plugins_cache` attribute of the placeholder,
|
||||
while the list of root plugins is stored in the `_plugins_cache` attribute
|
||||
"""
|
||||
if not placeholders:
|
||||
return
|
||||
placeholders = tuple(placeholders) # Trigger db hit
|
||||
lang = lang or get_language_from_request(request)
|
||||
plugins = list(CMSPlugin.objects.filter(placeholder__in=placeholders, language=lang))
|
||||
if not plugins:
|
||||
# Create default plugins if enabled
|
||||
plugins = create_default_plugins(request, placeholders, template, lang)
|
||||
else:
|
||||
plugins = downcast_plugins(plugins, placeholders, request=request)
|
||||
|
||||
# split the plugins up by placeholder
|
||||
plugins_by_placeholder = defaultdict(list)
|
||||
|
||||
for plugin in plugins:
|
||||
plugins_by_placeholder[plugin.placeholder_id].append(plugin)
|
||||
|
||||
for placeholder in placeholders:
|
||||
all_plugins = plugins_by_placeholder[placeholder.pk]
|
||||
|
||||
if all_plugins:
|
||||
layered_plugins = get_plugins_as_layered_tree(all_plugins)
|
||||
else:
|
||||
layered_plugins = []
|
||||
# This is all the plugins.
|
||||
placeholder._all_plugins_cache = all_plugins
|
||||
# This is only the root plugins.
|
||||
placeholder._plugins_cache = layered_plugins
|
||||
|
||||
|
||||
def create_default_plugins(request, placeholders, template, lang):
|
||||
"""
|
||||
Create all default plugins for the given ``placeholders`` if they have
|
||||
a "default_plugins" configuration value in settings.
|
||||
return all plugins, children, grandchildren (etc.) created
|
||||
"""
|
||||
from cms.api import add_plugin
|
||||
|
||||
def _create_default_plugins(placeholder, confs, parent=None):
|
||||
"""
|
||||
Auxiliary function that builds all of a placeholder's default plugins
|
||||
at the current level and drives the recursion down the tree.
|
||||
Returns the plugins at the current level along with all descendants.
|
||||
"""
|
||||
plugins, descendants = [], []
|
||||
addable_confs = (conf for conf in confs if has_plugin_permission(request.user, conf["plugin_type"], "add"))
|
||||
for conf in addable_confs:
|
||||
plugin = add_plugin(placeholder, conf["plugin_type"], lang, target=parent, **conf["values"])
|
||||
if "children" in conf:
|
||||
args = placeholder, conf["children"], plugin
|
||||
descendants += _create_default_plugins(*args)
|
||||
plugin.notify_on_autoadd(request, conf)
|
||||
plugins.append(plugin)
|
||||
if parent:
|
||||
parent.notify_on_autoadd_children(request, conf, plugins)
|
||||
return plugins + descendants
|
||||
|
||||
unfiltered_confs = ((ph, get_placeholder_conf("default_plugins", ph.slot, template)) for ph in placeholders)
|
||||
# Empty confs must be filtered before filtering on add permission
|
||||
mutable_confs = (
|
||||
(ph, default_plugin_confs)
|
||||
for ph, default_plugin_confs in filter(itemgetter(1), unfiltered_confs)
|
||||
if ph.has_change_permission(request.user)
|
||||
)
|
||||
return sum(starmap(_create_default_plugins, mutable_confs), [])
|
||||
|
||||
|
||||
def get_plugins_as_layered_tree(plugins):
|
||||
"""
|
||||
Given an iterable of plugins ordered by position,
|
||||
returns a deque of root plugins with their respective
|
||||
children set in the child_plugin_instances attribute.
|
||||
"""
|
||||
delayed = defaultdict(deque)
|
||||
root_plugins = deque()
|
||||
|
||||
for plugin in reversed(plugins):
|
||||
plugin.child_plugin_instances = delayed[plugin.pk]
|
||||
|
||||
if plugin.parent_id:
|
||||
delayed[plugin.parent_id].appendleft(plugin)
|
||||
else:
|
||||
root_plugins.appendleft(plugin)
|
||||
return root_plugins
|
||||
|
||||
|
||||
def get_plugin_restrictions(plugin, page=None, restrictions_cache=None):
|
||||
if restrictions_cache is None:
|
||||
restrictions_cache = {}
|
||||
|
||||
plugin_type = plugin.plugin_type
|
||||
plugin_class = get_plugin_class(plugin.plugin_type)
|
||||
cache = plugin_pool.get_restrictions_cache(restrictions_cache, plugin, page or plugin.placeholder.source)
|
||||
parents_cache = cache.setdefault("plugin_parents", {})
|
||||
children_cache = cache.setdefault("plugin_children", {})
|
||||
try:
|
||||
parent_classes = parents_cache[plugin_type]
|
||||
except KeyError:
|
||||
parent_classes = plugin_class.get_parent_classes(
|
||||
slot=plugin.placeholder.slot,
|
||||
page=page,
|
||||
instance=plugin,
|
||||
)
|
||||
|
||||
if plugin_class.cache_parent_classes:
|
||||
parents_cache[plugin_type] = parent_classes or []
|
||||
|
||||
# Note: a plugin's allowed *parents* are not sent to the frontend -- droppability is already
|
||||
# encoded in every potential parent's child list (including the placeholder's root list, which
|
||||
# excludes plugins that require a parent). ``parent_classes`` is still returned here for
|
||||
# backwards compatibility with external callers.
|
||||
child_classes = []
|
||||
if plugin_class.allow_children:
|
||||
# Only check for children if children are allowed
|
||||
try:
|
||||
child_classes = children_cache[plugin_type]
|
||||
# Still check child classes that did not cache their parent classes
|
||||
uncached_child_classes = plugin_class.get_child_classes(
|
||||
slot=plugin.placeholder.slot,
|
||||
page=page,
|
||||
instance=plugin,
|
||||
only_uncached=True,
|
||||
)
|
||||
child_classes = child_classes + uncached_child_classes # Creates a new list to not change the cache
|
||||
child_classes = child_classes or [""]
|
||||
except KeyError:
|
||||
child_classes = plugin_class.get_child_classes(
|
||||
slot=plugin.placeholder.slot,
|
||||
page=page,
|
||||
instance=plugin,
|
||||
only_uncached=False,
|
||||
)
|
||||
if plugin_class.cache_child_classes: # Check if child classes should be cached
|
||||
# Only add plugins to the cache that have the cache_parent_class attribute set
|
||||
children_cache[plugin_type] = [
|
||||
plugin for plugin in (child_classes or []) if plugin_pool.get_plugin(plugin).cache_parent_classes
|
||||
] or [""]
|
||||
# An empty list of allowed children (e.g. ``child_classes = []``) is sent to the
|
||||
# frontend as the ``[""]`` sentinel so that no plugins can be dropped as children.
|
||||
child_classes = child_classes or [""]
|
||||
|
||||
return child_classes, parent_classes
|
||||
|
||||
|
||||
def _reunite_orphaned_placeholder_plugin_children(root_plugin, orphaned_plugin_list, plugins_by_id):
|
||||
"""
|
||||
Handle plugins where the parent hasn't yet been copied (child seen before the parent)
|
||||
|
||||
CAVEAT: The only reason this exists is because the plugin position is not
|
||||
sequential through children when the user nests plugins.
|
||||
It's now too late as content already has this issue, it would be a very expensive
|
||||
calculation to recalculate every placeholders positions, needs to be handled gracefully
|
||||
so that it doesn't actually matter :-).
|
||||
"""
|
||||
for old_plugin_parent_id, new_plugin in orphaned_plugin_list:
|
||||
new_parent = plugins_by_id.get(old_plugin_parent_id, root_plugin)
|
||||
if new_parent:
|
||||
new_plugin.parent = new_parent
|
||||
new_plugin.save()
|
||||
|
||||
|
||||
@transaction.atomic
|
||||
def copy_plugins_to_placeholder(
|
||||
plugins,
|
||||
placeholder,
|
||||
language=None,
|
||||
root_plugin=None,
|
||||
start_positions=None,
|
||||
plugins_are_downcast=False,
|
||||
):
|
||||
"""Copies an iterable of plugins to a placeholder
|
||||
|
||||
:param iterable plugins: Plugins to be copied
|
||||
:param placeholder: Target placeholder
|
||||
:type placeholder: :class:`cms.models.placeholdermodel.Placeholder` instance
|
||||
:param str language: target language (if no root plugin is given)
|
||||
:param root_plugin:
|
||||
:type placeholder: :class:`cms.models.pluginmodel.CMSPlugin` instance
|
||||
:param int start_positions: Cache for start positions by language
|
||||
:param bool plugins_are_downcast: Whether the plugins are already concrete plugin instances
|
||||
|
||||
The logic of this method is the following:
|
||||
|
||||
#. Get bound plugins for each source plugin
|
||||
#. Get the parent plugin (if it exists)
|
||||
#. then get a copy of the source plugin instance
|
||||
#. Set the id/pk to None to it the id of the generic plugin instance above;
|
||||
this will effectively change the generic plugin created above
|
||||
into a concrete one
|
||||
#. find the position in the new placeholder
|
||||
#. save the concrete plugin (which creates a new plugin in the database)
|
||||
#. trigger the copy relations
|
||||
#. return the plugin ids
|
||||
"""
|
||||
plugin_pairs = []
|
||||
plugins_by_id = OrderedDict()
|
||||
# Keeps track of the next available position per language.
|
||||
positions_by_language = {}
|
||||
orphaned_plugin_list = []
|
||||
|
||||
if start_positions:
|
||||
positions_by_language.update(start_positions)
|
||||
|
||||
if root_plugin:
|
||||
language = root_plugin.language
|
||||
|
||||
source_plugins = plugins if plugins_are_downcast else get_bound_plugins(plugins)
|
||||
for source_plugin in source_plugins:
|
||||
parent = plugins_by_id.get(source_plugin.parent_id, root_plugin)
|
||||
plugin_model = source_plugin.__class__ # get_plugin_model(source_plugin.plugin_type)
|
||||
|
||||
if plugin_model is not CMSPlugin:
|
||||
new_plugin = deepcopy(source_plugin)
|
||||
new_plugin.pk = None
|
||||
new_plugin.id = None
|
||||
new_plugin.language = language or new_plugin.language
|
||||
new_plugin.placeholder = placeholder
|
||||
new_plugin.parent = parent
|
||||
else:
|
||||
new_plugin = CMSPlugin(
|
||||
language=(language or source_plugin.language),
|
||||
parent=parent,
|
||||
plugin_type=source_plugin.plugin_type,
|
||||
placeholder=placeholder,
|
||||
)
|
||||
|
||||
try:
|
||||
position = positions_by_language[new_plugin.language]
|
||||
except KeyError:
|
||||
offset = placeholder.get_last_plugin_position(new_plugin.language) or 0
|
||||
# The position is relative to language.
|
||||
position = placeholder.get_next_plugin_position(
|
||||
language=new_plugin.language,
|
||||
parent=new_plugin.parent,
|
||||
insert_order="last",
|
||||
)
|
||||
# Because it is the first time this language is processed,
|
||||
# shift all plugins to the right of the next position.
|
||||
placeholder._shift_plugin_positions(
|
||||
new_plugin.language,
|
||||
start=position,
|
||||
offset=offset,
|
||||
)
|
||||
|
||||
new_plugin.position = position
|
||||
new_plugin.save()
|
||||
positions_by_language[new_plugin.language] = position + 1
|
||||
|
||||
if plugin_model != CMSPlugin:
|
||||
new_plugin.copy_relations(source_plugin)
|
||||
plugin_pairs.append((new_plugin, source_plugin))
|
||||
plugins_by_id[source_plugin.pk] = new_plugin
|
||||
|
||||
# Rescue any orphaned plugins
|
||||
if not parent and source_plugin.parent_id:
|
||||
orphaned_plugin_list.append((source_plugin.parent_id, new_plugin))
|
||||
|
||||
# Reunite any orphaned plugins with the parent
|
||||
if orphaned_plugin_list:
|
||||
_reunite_orphaned_placeholder_plugin_children(root_plugin, orphaned_plugin_list, plugins_by_id)
|
||||
|
||||
# Backwards compatibility
|
||||
# This magic is needed for advanced plugins like Text Plugins that can have
|
||||
# nested plugins and need to update their content based on the new plugins.
|
||||
for new_plugin, old_plugin in plugin_pairs:
|
||||
new_plugin.post_copy(old_plugin, plugin_pairs)
|
||||
|
||||
for language in positions_by_language:
|
||||
placeholder._recalculate_plugin_positions(language)
|
||||
|
||||
return list(plugins_by_id.values())
|
||||
|
||||
|
||||
def get_bound_plugins(plugins):
|
||||
"""
|
||||
Get the bound plugins by downcasting the plugins to their respective classes. Raises a KeyError if the plugin type
|
||||
is not available.
|
||||
|
||||
Creates a map of plugin types and their corresponding plugin IDs for later use in downcasting.
|
||||
Then, retrieves the plugin instances from the plugin model using the mapped plugin IDs.
|
||||
Finally, iterates over the plugins and yields the downcasted versions if they have a valid parent.
|
||||
Does not affect caching.
|
||||
|
||||
:param plugins: List of ``CMSPlugin`` instances.
|
||||
:type plugins: List[CMSPlugin]
|
||||
:return: Generator that yields the downcasted plugins.
|
||||
:rtype: Generator[CMSPlugin, None, None]
|
||||
|
||||
Example::
|
||||
|
||||
plugins = [plugin_instance1, plugin_instance2]
|
||||
for bound_plugin in get_bound_plugins(plugins):
|
||||
# Do something with the bound_plugin
|
||||
pass
|
||||
"""
|
||||
plugin_types_map = defaultdict(list)
|
||||
plugin_ids = []
|
||||
plugin_lookup = {}
|
||||
|
||||
# make a map of plugin types, needed later for downcasting
|
||||
for plugin in plugins:
|
||||
plugin_ids.append(plugin.pk)
|
||||
plugin_model = get_plugin_model(plugin.plugin_type)
|
||||
base_model = plugin_model._meta.concrete_model # Collect all base models
|
||||
if base_model is CMSPlugin:
|
||||
plugin.__class__ = plugin_model # In case it's a proxy model
|
||||
plugin_lookup[plugin.pk] = plugin # Otherwise, no downcast needed
|
||||
else:
|
||||
plugin_types_map[base_model].append(plugin.pk)
|
||||
|
||||
for base_model, pks in plugin_types_map.items():
|
||||
plugin_queryset = base_model.objects.filter(pk__in=pks)
|
||||
# put them in a map, so we can replace the base CMSPlugins with their
|
||||
# downcasted versions
|
||||
for instance in plugin_queryset.iterator(chunk_size=PLUGIN_ITERATOR_CHUNK_SIZE):
|
||||
model = get_plugin_model(instance.plugin_type) # Get original class
|
||||
instance.__class__ = model # Cast to correct model (including proxies)
|
||||
plugin_lookup[instance.pk] = instance
|
||||
|
||||
for plugin in plugins:
|
||||
parent_not_available = not plugin.parent_id or plugin.parent_id not in plugin_ids
|
||||
# The plugin either has no parent or needs to have a non-ghost parent
|
||||
valid_parent = parent_not_available or plugin.parent_id in plugin_lookup
|
||||
|
||||
if valid_parent and plugin.pk in plugin_lookup:
|
||||
plugin._inst = plugin_lookup[plugin.pk] # Populate bound plugin cache for plugin
|
||||
yield plugin_lookup[plugin.pk]
|
||||
|
||||
|
||||
def downcast_plugins(
|
||||
plugins: Iterable[CMSPlugin],
|
||||
placeholders: Sequence | None = None,
|
||||
select_placeholder: bool = False,
|
||||
request: HttpRequest | None = None,
|
||||
) -> Iterable[CMSPlugin]:
|
||||
"""
|
||||
Downcasts the given list of plugins to their respective classes. Ignores any plugins
|
||||
that are not available.
|
||||
|
||||
:param plugins: List of plugins to downcast.
|
||||
:type plugins: List[CMSPlugin]
|
||||
:param placeholders: List of placeholders associated with the plugins.
|
||||
:type placeholders: Optional[List[Placeholder]]
|
||||
:param select_placeholder: If True, select_related the plugin queryset with placeholder.
|
||||
:type select_placeholder: bool
|
||||
:param request: The current request.
|
||||
:type request: Optional[HttpRequest]
|
||||
:return: Generator that yields the downcasted plugins.
|
||||
:rtype: Generator[CMSPlugin, None, None]
|
||||
"""
|
||||
plugin_types_map = defaultdict(list)
|
||||
plugin_lookup = {}
|
||||
plugin_ids = []
|
||||
|
||||
# make a map of plugin types, needed later for downcasting
|
||||
for plugin in plugins:
|
||||
# Keep track of the plugin ids we've received
|
||||
try:
|
||||
plugin_model = get_plugin_model(plugin.plugin_type)
|
||||
base_model = plugin_model._meta.concrete_model # Collect all base models
|
||||
except KeyError:
|
||||
# Plugin not available
|
||||
logger.error(f"Plugin not installed: {plugin.plugin_type} (pk={plugin.pk})", exc_info=sys.exc_info())
|
||||
continue
|
||||
plugin_ids.append(plugin.pk)
|
||||
if base_model is CMSPlugin:
|
||||
plugin.__class__ = plugin_model # In case it is a proxy model
|
||||
plugin_lookup[plugin.pk] = plugin # otherwise, no downcast needed
|
||||
else:
|
||||
plugin_types_map[base_model].append(plugin.pk)
|
||||
|
||||
placeholders = placeholders or []
|
||||
placeholders_by_id = {placeholder.pk: placeholder for placeholder in placeholders}
|
||||
|
||||
for plugin_model, pks in plugin_types_map.items():
|
||||
# get all the plugins of type cls.model
|
||||
plugin_qs = plugin_model.objects.filter(pk__in=pks)
|
||||
|
||||
if select_placeholder:
|
||||
plugin_qs = plugin_qs.select_related("placeholder")
|
||||
|
||||
# put them in a map, so we can replace the base CMSPlugins with their
|
||||
# downcasted versions
|
||||
for instance in plugin_qs.iterator(chunk_size=PLUGIN_ITERATOR_CHUNK_SIZE):
|
||||
cls = get_plugin_class(instance.plugin_type) # Plugin class
|
||||
instance.__class__ = cls.model # Cast to original model (including proxies)
|
||||
plugin_lookup[instance.pk] = instance
|
||||
|
||||
for plugin in plugins:
|
||||
parent_not_available = not plugin.parent_id or plugin.parent_id not in plugin_ids
|
||||
# The plugin either has no parent or needs to have a non-ghost parent
|
||||
valid_parent = parent_not_available or plugin.parent_id in plugin_lookup
|
||||
|
||||
if valid_parent and plugin.pk in plugin_lookup:
|
||||
instance = plugin_lookup[plugin.pk]
|
||||
if instance.parent_id in plugin_lookup:
|
||||
instance._state.fields_cache["parent"] = plugin_lookup[instance.parent_id]
|
||||
placeholder = placeholders_by_id.get(instance.placeholder_id)
|
||||
if placeholder:
|
||||
instance.placeholder = placeholder
|
||||
cls = get_plugin_class(instance.plugin_type)
|
||||
if not cls.cache and not cls().get_cache_expiration(request, instance, placeholder):
|
||||
placeholder.cache_placeholder = False
|
||||
|
||||
plugin._inst = instance # Populate bound plugin cache for plugin
|
||||
yield instance
|
||||
|
||||
|
||||
def has_reached_plugin_limit(placeholder, plugin_type, language, template=None):
|
||||
"""
|
||||
Checks if the global maximum limit for plugins in a placeholder has been reached.
|
||||
If not then it checks if it has reached its maximum plugin_type limit.
|
||||
|
||||
Parameters:
|
||||
- placeholder: The placeholder object to check the limit for.
|
||||
- plugin_type: The type of plugin to check the limit for.
|
||||
- language: The language code for the plugins.
|
||||
- template: The template object for the placeholder. Optional.
|
||||
|
||||
Returns:
|
||||
- False if the limit has not been reached.
|
||||
|
||||
Raises:
|
||||
- PluginLimitReached: If the limit has been reached for the placeholder.
|
||||
|
||||
"""
|
||||
limits = get_placeholder_conf("limits", placeholder.slot, template)
|
||||
if not limits:
|
||||
return False
|
||||
|
||||
global_limit = limits.get("global")
|
||||
type_limit = limits.get(plugin_type)
|
||||
# total plugin counts
|
||||
counts = placeholder.get_plugins(language=language).aggregate(
|
||||
total_count=models.Count('pk'),
|
||||
type_count=models.Count('pk', filter=models.Q(plugin_type=plugin_type))
|
||||
)
|
||||
# Ensure counts default to integers if None
|
||||
total_count = counts.get('total_count') or 0
|
||||
type_count = counts.get('type_count') or 0
|
||||
|
||||
global_limit = limits.get("global")
|
||||
type_limit = limits.get(plugin_type)
|
||||
|
||||
if global_limit and total_count >= global_limit:
|
||||
raise PluginLimitReached(_("This placeholder already has the maximum number of plugins (%s).") % global_limit)
|
||||
|
||||
if type_limit and type_count >= type_limit:
|
||||
plugin_name = get_plugin_class(plugin_type).name
|
||||
raise PluginLimitReached(
|
||||
_(
|
||||
"This placeholder already has the maximum number (%(limit)s) of allowed %(plugin_name)s plugins."
|
||||
)
|
||||
% {"limit": type_limit, "plugin_name": plugin_name}
|
||||
)
|
||||
|
||||
|
||||
def get_plugin_disallowed_in_slot(plugin_types: Iterable[str], slot: str | None) -> str | None:
|
||||
"""Return the first plugin type whose ``allowed_slots`` restriction forbids ``slot``.
|
||||
|
||||
``plugin_types`` is an iterable of plugin type names (e.g. the plugin being moved or
|
||||
copied together with all of its descendants). Returns the offending plugin type name,
|
||||
or ``None`` if every plugin may be added to ``slot``. Unregistered plugin types are
|
||||
skipped (treated as allowed), mirroring the rest of the move/copy handling.
|
||||
"""
|
||||
for plugin_type in dict.fromkeys(plugin_types):
|
||||
try:
|
||||
plugin_class = plugin_pool.get_plugin(plugin_type)
|
||||
except KeyError:
|
||||
continue
|
||||
if not plugin_class.is_allowed_in_slot(slot):
|
||||
return plugin_type
|
||||
return None
|
||||
72
cms/utils/request_ip_resolvers.py
Normal file
72
cms/utils/request_ip_resolvers.py
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import importlib
|
||||
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from cms.utils.conf import get_cms_setting
|
||||
|
||||
|
||||
def get_request_ip_resolver():
|
||||
"""
|
||||
This is the recommended method for obtaining the specified
|
||||
CMS_REQUEST_IP_RESOLVER as it also does some basic import validation.
|
||||
|
||||
Returns the resolver or raises an ImproperlyConfigured exception.
|
||||
"""
|
||||
module, attribute = get_cms_setting('REQUEST_IP_RESOLVER').rsplit('.', 1)
|
||||
try:
|
||||
ip_resolver_module = importlib.import_module(module)
|
||||
ip_resolver = getattr(ip_resolver_module, attribute)
|
||||
except ImportError as err:
|
||||
raise ImproperlyConfigured(
|
||||
_('Unable to find the specified CMS_REQUEST_IP_RESOLVER module: '
|
||||
'"{0}".').format(module)) from err
|
||||
except AttributeError as err:
|
||||
raise ImproperlyConfigured(
|
||||
_('Unable to find the specified CMS_REQUEST_IP_RESOLVER function: '
|
||||
'"{0}" in module "{1}".').format(attribute, module)) from err
|
||||
return ip_resolver
|
||||
|
||||
|
||||
def default_request_ip_resolver(request):
|
||||
"""
|
||||
This is a hybrid request IP resolver that attempts should address most
|
||||
cases. Order is important here. A 'REAL_IP' header supersedes an
|
||||
'X_FORWARDED_FOR' header which supersedes a 'REMOTE_ADDR' header.
|
||||
"""
|
||||
return (
|
||||
real_ip(request) or x_forwarded_ip(request) or remote_addr_ip(request)
|
||||
)
|
||||
|
||||
|
||||
def real_ip(request):
|
||||
"""
|
||||
Returns the IP Address contained in the HTTP_X_REAL_IP headers, if
|
||||
present. Otherwise, `None`.
|
||||
|
||||
Should handle Nginx and some other WSGI servers.
|
||||
"""
|
||||
return request.headers.get('X-Real-Ip')
|
||||
|
||||
|
||||
def remote_addr_ip(request):
|
||||
"""
|
||||
Returns the IP Address contained in the 'REMOTE_ADDR' header, if
|
||||
present. Otherwise, `None`.
|
||||
|
||||
Should be suitable for local-development servers and some HTTP servers.
|
||||
"""
|
||||
return request.META.get('REMOTE_ADDR')
|
||||
|
||||
|
||||
def x_forwarded_ip(request):
|
||||
"""
|
||||
Returns the IP Address contained in the 'HTTP_X_FORWARDED_FOR' header, if
|
||||
present. Otherwise, `None`.
|
||||
|
||||
Should handle properly configured proxy servers.
|
||||
"""
|
||||
ip_address_list = request.headers.get('X-Forwarded-For')
|
||||
if ip_address_list:
|
||||
ip_address_list = ip_address_list.split(',')
|
||||
return ip_address_list[0]
|
||||
73
cms/utils/setup.py
Normal file
73
cms/utils/setup.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.template import engines
|
||||
from django.template.backends.django import DjangoTemplates
|
||||
|
||||
from cms.app_registration import (
|
||||
autodiscover_cms_configs,
|
||||
backwards_compatibility_config,
|
||||
configure_cms_apps,
|
||||
get_cms_extension_apps,
|
||||
ready_cms_apps,
|
||||
)
|
||||
from cms.utils.compat.dj import is_installed as app_is_installed
|
||||
|
||||
|
||||
def validate_dependencies():
|
||||
"""
|
||||
Check for installed apps, their versions and configuration options
|
||||
"""
|
||||
from cms.utils.mptree import get_tree_backend
|
||||
|
||||
# treebeard is only required for the (default) treebeard tree backend; the
|
||||
# dependency-free mptree backend neither imports nor needs it.
|
||||
if get_tree_backend() == "treebeard" and not app_is_installed("treebeard"):
|
||||
raise ImproperlyConfigured(
|
||||
'django CMS requires django-treebeard. Please install it and add "treebeard" to INSTALLED_APPS.'
|
||||
)
|
||||
|
||||
|
||||
def validate_settings():
|
||||
"""
|
||||
Check project settings file for required options
|
||||
"""
|
||||
django_engines = [engine for engine in engines.all() if isinstance(engine, DjangoTemplates)]
|
||||
|
||||
if not django_engines:
|
||||
raise ImproperlyConfigured(
|
||||
"django CMS requires a template engine that inherits from "
|
||||
"'django.template.backends.django.DjangoTemplates'."
|
||||
)
|
||||
|
||||
engine = django_engines[0]
|
||||
context_processors = engine.engine.context_processors
|
||||
template_request = "django.template.context_processors.request"
|
||||
|
||||
if template_request not in context_processors:
|
||||
raise ImproperlyConfigured(
|
||||
"django CMS requires django.template.context_processors.request in "
|
||||
"'django.template.backends.django.DjangoTemplates' context processors."
|
||||
)
|
||||
|
||||
|
||||
def setup():
|
||||
"""
|
||||
Gather all checks and validations
|
||||
"""
|
||||
from cms.plugin_pool import plugin_pool
|
||||
|
||||
validate_dependencies()
|
||||
validate_settings()
|
||||
plugin_pool.validate_templates()
|
||||
|
||||
|
||||
def setup_cms_apps():
|
||||
"""
|
||||
Check for django apps which provide functionality that extends the
|
||||
cms. Configure all apps which have configs that declare use of
|
||||
any of this functionality.
|
||||
"""
|
||||
autodiscover_cms_configs()
|
||||
cms_apps = get_cms_extension_apps()
|
||||
configure_cms_apps(cms_apps)
|
||||
backwards_compatibility_config()
|
||||
ready_cms_apps(cms_apps)
|
||||
121
cms/utils/urlutils.py
Normal file
121
cms/utils/urlutils.py
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
import re
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from django.conf import settings
|
||||
from django.urls import reverse
|
||||
from django.utils.encoding import force_str
|
||||
from django.utils.http import urlencode
|
||||
|
||||
import cms
|
||||
from cms.utils.conf import get_cms_setting
|
||||
|
||||
# checks validity of absolute / relative url
|
||||
any_path_re = re.compile('^/?[a-zA-Z0-9_.-]+(/[a-zA-Z0-9_.-]+)*/?$')
|
||||
|
||||
# checks validity of relative url
|
||||
# matches the following:
|
||||
# /test
|
||||
# /test/
|
||||
# ./test/
|
||||
# ../test/
|
||||
# ``<`` and ``>`` are excluded over the *whole* value, not just the leading segment: a
|
||||
# trailing ``.*`` used to let markup through as soon as one safe character preceded it
|
||||
# (e.g. ``foo/a<img src=x onerror=alert(1)>``).
|
||||
relative_url_regex = re.compile(r'^[^/<>]+/[^/<>][^<>]*$|^/[^<>]*$', re.IGNORECASE)
|
||||
|
||||
# checks validity of a single path segment, e.g. an "Overwrite URL" of "contact".
|
||||
# ``relative_url_regex`` only matches values containing a "/", so slug-like paths need
|
||||
# their own check. The character class mirrors the one above; the empty path is allowed
|
||||
# because that is the home page.
|
||||
path_segment_regex = re.compile(r'^[^/<>]*$', re.IGNORECASE)
|
||||
|
||||
|
||||
def levelize_path(path):
|
||||
"""Splits given path to list of paths removing latest level in each step.
|
||||
|
||||
>>> path = '/application/item/new'
|
||||
>>> levelize_path(path)
|
||||
['/application/item/new', '/application/item', '/application']
|
||||
"""
|
||||
parts = tuple(filter(None, path.split('/')))
|
||||
return ['/' + '/'.join(parts[:n]) for n in range(len(parts), 0, -1)]
|
||||
|
||||
|
||||
def urljoin(*segments):
|
||||
"""Joins url segments together and appends trailing slash if required.
|
||||
|
||||
>>> urljoin('a', 'b', 'c')
|
||||
u'a/b/c/'
|
||||
|
||||
>>> urljoin('a', '//b//', 'c')
|
||||
u'a/b/c/'
|
||||
|
||||
>>> urljoin('/a', '/b/', '/c/')
|
||||
u'/a/b/c/'
|
||||
|
||||
>>> urljoin('/a', '')
|
||||
u'/a/'
|
||||
"""
|
||||
url = '/' if segments[0].startswith('/') else ''
|
||||
url += '/'.join(filter(None, (force_str(s).strip('/') for s in segments)))
|
||||
return url + '/' if settings.APPEND_SLASH else url
|
||||
|
||||
|
||||
def is_media_request(request):
|
||||
"""
|
||||
Check if a request is a media request.
|
||||
"""
|
||||
parsed_media_url = urlparse(settings.MEDIA_URL)
|
||||
if request.path_info.startswith(parsed_media_url.path):
|
||||
if parsed_media_url.netloc:
|
||||
if request.get_host() == parsed_media_url.netloc:
|
||||
return True
|
||||
else:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def static_with_version(path):
|
||||
"""
|
||||
Changes provided path from `path/to/filename.ext` to `path/to/$CMS_VERSION/filename.ext`
|
||||
"""
|
||||
path_re = re.compile('(.*)/([^/]*$)')
|
||||
|
||||
return re.sub(path_re, r'\1/%s/\2' % (cms.__version__), path)
|
||||
|
||||
|
||||
def add_url_parameters(url, *args, **params):
|
||||
"""
|
||||
adds parameters to an url -> url?p1=v1&p2=v2...
|
||||
:param url: url without any parameters
|
||||
:param args: one or more dictionaries containing url parameters
|
||||
:param params: url parameters as keyword arguments
|
||||
:return: url with parameters if any
|
||||
"""
|
||||
for arg in args:
|
||||
params.update(arg)
|
||||
if params:
|
||||
return f"{url}?{urlencode(params)}"
|
||||
return url
|
||||
|
||||
|
||||
admin_namespace = get_cms_setting('ADMIN_NAMESPACE')
|
||||
|
||||
|
||||
def admin_reverse(
|
||||
viewname: str,
|
||||
urlconf: str | None = None,
|
||||
args: Sequence | None = None,
|
||||
kwargs: dict[str, Any] | None = None,
|
||||
prefix=None,
|
||||
current_app: str | None = None
|
||||
):
|
||||
return reverse(
|
||||
f"{admin_namespace}:{viewname}",
|
||||
urlconf=urlconf,
|
||||
args=args,
|
||||
kwargs=kwargs,
|
||||
current_app=current_app
|
||||
)
|
||||
Loading…
Add table
Add a link
Reference in a new issue