Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 226 lines (205 sloc) 8.756 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
# -*- coding: utf-8 -*-
from cms.apphook_pool import apphook_pool
from cms.exceptions import NoHomeFound
from cms.utils.moderator import get_page_queryset

from django.conf import settings
from django.conf.urls.defaults import patterns
from django.contrib.sites.models import Site
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import RegexURLResolver, Resolver404, reverse, \
    RegexURLPattern
from django.utils.importlib import import_module

APP_RESOLVERS = []

def clear_app_resolvers():
    global APP_RESOLVERS
    APP_RESOLVERS = []

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.
"""
    if current_page:
        return current_page
    if path is None:
        # We should get in this branch only if an apphook is active on /
        # This removes the non-CMS part of the URL.
        path = request.path.replace(reverse('pages-root'), '', 1)
    # check if application resolver can resolve this
    for resolver in APP_RESOLVERS:
        try:
            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:
            # Raised if the page is not managed by an apphook
            pass
    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
assigned.
"""
        tried = []
        match = self.regex.search(path)
        if match:
            new_path = path[match.end():]
            for pattern in self.url_patterns:
                try:
                    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']])
                else:
                    if sub_match:
                        return pattern.page_id
                    tried.append(pattern.regex.pattern)
            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)
            resolver.page_id = page_id
            # see lines 243 and 236 of urlresolvers.py to understand the next line
            resolver._urlconf_module = recurse_patterns(regex, pattern.url_patterns, page_id)
        else:
            # Re-do the RegexURLPattern with the new regular expression
            resolver = RegexURLPattern(regex, pattern.callback,
                pattern.default_args, pattern.name)
            resolver.page_id = page_id
        newpatterns.append(resolver)
    return newpatterns

def _flatten_patterns(patterns):
    flat = []
    for pattern in patterns:
        if isinstance(pattern, RegexURLResolver):
            flat += _flatten_patterns(pattern.url_patterns)
        else:
            flat.append(pattern)
    return flat

def get_app_urls(urls):
    for urlconf in urls:
        if isinstance(urlconf, basestring):
            mod = import_module(urlconf)
            if not hasattr(mod, 'urlpatterns'):
                raise ImproperlyConfigured(
                    "URLConf `%s` has no urlpatterns attribute" % urlconf)
            yield getattr(mod, 'urlpatterns')
        else:
            yield urlconf
    

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 pattern_list in get_app_urls(app.urls):
        if not path.endswith('/'):
            path += '/'
        page_id = title.page.id
        patterns += recurse_patterns(path, pattern_list, page_id)
    patterns = _flatten_patterns(patterns)
    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
language.

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
    try:
        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
    try:
        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 = {}
    else:
        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]
            else:
                path = title.slug
            if use_namespaces:
                mixid = "%s:%s:%s" % (path + "/", title.application_urls, title.language)
            else:
                mixid = "%s:%s" % (path + "/", title.application_urls)
        else:
            if title.language in home_slugs:
                path = title.path.split(home_slugs[title.language] + "/", 1)[-1]
            else:
                path = title.path
            if use_namespaces:
                mixid = "%s:%s:%s" % (path + "/", title.application_urls, title.language)
            else:
                mixid = "%s:%s" % (path + "/", title.application_urls)
        if mixid in included:
            # don't add the same thing twice
            continue
        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)
        else:
            hooked_applications += get_patterns_for_title(path, title)
        included.append(mixid)
    # 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
            app_patterns.append(resolver)
            APP_RESOLVERS.append(resolver)
    else:
        extra_patterns = patterns('', *hooked_applications)
        resolver = AppRegexURLResolver(r'', 'app_resolver')
        resolver.url_patterns = extra_patterns
        app_patterns.append(resolver)
        APP_RESOLVERS.append(resolver)
    return app_patterns
Something went wrong with that request. Please try again.