Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

200 lines (185 sloc) 8.086 kb
from django.conf import settings
from django.core.urlresolvers import RegexURLResolver, Resolver404, reverse, RegexURLPattern
from django.conf.urls.defaults import *
from django.utils.importlib import import_module
from django.core.exceptions import ImproperlyConfigured
from django.contrib.sites.models import Site
from cms.exceptions import NoHomeFound
import re
from cms.apphook_pool import apphook_pool
def applications_page_check(request, current_page=None, path=None):
"""Tries to find if given path was resolved over application.
Applications have higher priority than other cms pages.
from cms.utils.moderator import get_page_queryset
if current_page:
return current_page
if path is None:
path = request.path.replace(reverse('pages-root'), '', 1)
# check if application resolver can resolve this
for resolver in APP_RESOLVERS:
page_id = resolver.resolve_page_id(path+"/")
# yes, it is application page
page = get_page_queryset(request).get(id=page_id)
# If current page was matched, then we have some override for content
# from cms, but keep current page. Otherwise return page to which was application assigned.
return page
except Resolver404:
return None
class AppRegexURLResolver(RegexURLResolver):
page_id = None
url_patterns = None
def resolve_page_id(self, path):
"""Resolves requested path similar way how resolve does, but instead
of return callback,.. returns page_id to which was application
tried = []
match =
if match:
new_path = path[match.end():]
for pattern in self.url_patterns:
sub_match = pattern.resolve(new_path)
except Resolver404, e:
if 'tried' in e.args[0]:
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['tried']])
elif 'path' in e.args[0]:
tried.extend([(pattern.regex.pattern + ' ' + t) for t in e.args[0]['path']])
if sub_match:
return pattern.page_id
raise Resolver404, {'tried': tried, 'path': new_path}
def recurse_patterns(path, pattern_list, page_id):
Recurse over a list of to-be-hooked patterns for a given path prefix
newpatterns = []
for pattern in pattern_list:
app_pat = pattern.regex.pattern
if app_pat.startswith('^'):
app_pat = app_pat[1:]
regex = r'^%s%s' % (path, app_pat)
if isinstance(pattern, RegexURLResolver):
# this is an 'include', recurse!
resolver = RegexURLResolver(regex, 'cms_appresolver',
pattern.default_kwargs, pattern.app_name, pattern.namespace)
# This is a bit hacky, usually a RegexURLResolver uses the urlconf_name
# to resolve URLs, but we just override the url_patterns...
resolver.page_id = page_id
resolver.url_patterns = recurse_patterns(regex, pattern.url_patterns, page_id)
# Re-do the RegexURLPattern with the new regular expression
resolver = RegexURLPattern(regex, pattern.callback,
resolver.page_id = page_id
return newpatterns
def get_patterns_for_title(path, title):
Resolve the urlconf module for a path+title combination
Returns a list of url objects.
app = apphook_pool.get_apphook(title.application_urls)
patterns = []
for urlconf_name in app.urls:
mod = import_module(urlconf_name)
if not hasattr(mod, 'urlpatterns'):
raise ImproperlyConfigured("URLConf `%s` has no urlpatterns attribute"
% urlconf_name)
pattern_list = getattr(mod, 'urlpatterns')
if not path.endswith('/'):
path += '/'
page_id =
patterns += recurse_patterns(path, pattern_list, page_id)
return patterns
def get_app_patterns():
Get a list of patterns for all hooked apps.
How this works:
By looking through all titles with an app hook (application_urls) we find all
urlconf modules we have to hook into titles.
If we use the ML URL Middleware, we namespace those patterns with the title
All 'normal' patterns from the urlconf get re-written by prefixing them with
the title path and then included into the cms url patterns.
from cms.models import Title
from cms.models.pagemodel import Page
current_site = Site.objects.get_current()
except Site.DoesNotExist:
current_site = None
included = []
# we don't have a request here so get_page_queryset() can't be used,
# so, if CMS_MODERATOR, use, public() queryset, otherwise
# use draft(). This can be done, because url patterns are used just
# in frontend
is_draft = not settings.CMS_MODERATOR
home = Page.objects.get_home()
home_titles = home.title_set.all()
except NoHomeFound:
home_titles = []
home_slugs = {}
for title in home_titles:
home_slugs[title.language] = title.slug
title_qs = Title.objects.filter(page__publisher_is_draft=is_draft, page__site=current_site)
if 'cms.middleware.multilingual.MultilingualURLMiddleware' in settings.MIDDLEWARE_CLASSES:
use_namespaces = True
hooked_applications = {}
use_namespaces = False
hooked_applications = []
# Loop over all titles with an application hooked to them
for title in title_qs.filter(application_urls__gt="").select_related():
if settings.CMS_FLAT_URLS:
if title.language in home_slugs:
path = title.slug.split(home_slugs[title.language] + "/", 1)[-1]
path = title.slug
if use_namespaces:
mixid = "%s:%s:%s" % (path + "/", title.application_urls, title.language)
mixid = "%s:%s" % (path + "/", title.application_urls)
if title.language in home_slugs:
path = title.path.split(home_slugs[title.language] + "/", 1)[-1]
path = title.path
if use_namespaces:
mixid = "%s:%s:%s" % (path + "/", title.application_urls, title.language)
mixid = "%s:%s" % (path + "/", title.application_urls)
if mixid in included:
# don't add the same thing twice
if not settings.APPEND_SLASH:
path += '/'
if use_namespaces:
if title.language not in hooked_applications:
hooked_applications[title.language] = []
hooked_applications[title.language] += get_patterns_for_title(path, title)
hooked_applications += get_patterns_for_title(path, title)
# Build the app patterns to be included in the cms urlconfs
app_patterns = []
if use_namespaces:
for ns, currentpatterns in hooked_applications.items():
extra_patterns = patterns('', *currentpatterns)
resolver = AppRegexURLResolver(r'', 'app_resolver', namespace=ns)
resolver.url_patterns = extra_patterns
extra_patterns = patterns('', *hooked_applications)
resolver = AppRegexURLResolver(r'', 'app_resolver')
resolver.url_patterns = extra_patterns
return app_patterns
Jump to Line
Something went wrong with that request. Please try again.