Пример #1
0
def recurse_patterns(path,
                     pattern_list,
                     page_id,
                     default_args=None,
                     nested=False):
    """
    Recurse over a list of to-be-hooked patterns for a given path prefix
    """
    newpatterns = []
    for pattern in pattern_list:
        app_pat = getattr(pattern, 'pattern', pattern).regex.pattern
        # make sure we don't get patterns that start with more than one '^'!
        app_pat = app_pat.lstrip('^')
        path = path.lstrip('^')
        regex = r'^%s%s' % (path,
                            app_pat) if not nested else r'^%s' % (app_pat)
        if isinstance(pattern, URLResolver):
            # include default_args
            args = pattern.default_kwargs
            if default_args:
                args.update(default_args)
            # see lines 243 and 236 of urlresolvers.py to understand the next line
            urlconf_module = recurse_patterns(regex,
                                              pattern.url_patterns,
                                              page_id,
                                              args,
                                              nested=True)
            # this is an 'include', recurse!
            regex_pattern = regex
            if not DJANGO_1_11:
                regex_pattern = RegexPattern(regex)
            resolver = URLResolver(regex_pattern, urlconf_module,
                                   pattern.default_kwargs, pattern.app_name,
                                   pattern.namespace)
        else:
            # Re-do the URLPattern with the new regular expression
            args = pattern.default_args
            if default_args:
                args.update(default_args)

            regex_pattern = regex
            if not DJANGO_1_11:
                regex_pattern = RegexPattern(regex,
                                             name=pattern.name,
                                             is_endpoint=True)
            resolver = URLPattern(regex_pattern, pattern.callback, args,
                                  pattern.name)
        resolver.page_id = page_id
        newpatterns.append(resolver)
    return newpatterns
Пример #2
0
def _get_app_patterns(site):
    """
    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.

    If the app is still configured, but is no longer installed/available, then
    this method returns a degenerate patterns object: patterns('')
    """
    from cms.models import Title

    included = []

    # we don't have a request here so get_page_queryset() can't be used,
    # so use public() queryset.
    # This can be done because url patterns are used just in frontend
    title_qs = Title.objects.public().filter(page__node__site=site)

    hooked_applications = OrderedDict()

    # Loop over all titles with an application hooked to them
    titles = (title_qs.exclude(page__application_urls=None)
              .exclude(page__application_urls='')
              .order_by('-page__node__path').select_related())
    # TODO: Need to be fixed for django-treebeard when forward ported to 3.1
    for title in titles:
        path = title.path
        mix_id = "%s:%s:%s" % (
            path + "/", title.page.application_urls, title.language)
        if mix_id in included:
            # don't add the same thing twice
            continue
        if not settings.APPEND_SLASH:
            path += '/'
        app = apphook_pool.get_apphook(title.page.application_urls)
        if not app:
            continue
        if title.page_id not in hooked_applications:
            hooked_applications[title.page_id] = {}
        app_ns = app.app_name, title.page.application_namespace
        with override(title.language):
            hooked_applications[title.page_id][title.language] = (
                app_ns, get_patterns_for_title(path, title), app)
        included.append(mix_id)
        # Build the app patterns to be included in the cms urlconfs
    app_patterns = []
    for page_id in hooked_applications.keys():
        resolver = None
        for lang in hooked_applications[page_id].keys():
            (app_ns, inst_ns), current_patterns, app = hooked_applications[page_id][lang]  # nopyflakes
            if not resolver:
                regex_pattern = RegexPattern(r'') if not DJANGO_1_11 else r''
                resolver = AppRegexURLResolver(
                    regex_pattern, 'app_resolver', app_name=app_ns, namespace=inst_ns)
                resolver.page_id = page_id
            if app.permissions:
                _set_permissions(current_patterns, app.exclude_permissions)

            resolver.url_patterns_dict[lang] = current_patterns
        app_patterns.append(resolver)
        APP_RESOLVERS.append(resolver)
    return app_patterns