1
1
# -*- coding: utf-8 -*-
2
2
from __future__ import with_statement
3
3
4
- import sys
5
-
6
4
from django .conf import settings
7
5
from django .conf .urls import patterns
8
6
from django .contrib .sites .models import Site
@@ -45,8 +43,9 @@ def applications_page_check(request, current_page=None, path=None):
45
43
page_id = resolver .resolve_page_id (path )
46
44
# yes, it is application page
47
45
page = Page .objects .public ().get (id = page_id )
48
- # If current page was matched, then we have some override for content
49
- # from cms, but keep current page. Otherwise return page to which was application assigned.
46
+ # If current page was matched, then we have some override for
47
+ # content from cms, but keep current page. Otherwise return page
48
+ # to which was application assigned.
50
49
return page
51
50
except Resolver404 :
52
51
# Raised if the page is not managed by an apphook
@@ -86,20 +85,20 @@ def resolve_page_id(self, path):
86
85
else :
87
86
try :
88
87
sub_match = pattern .resolve (new_path )
89
- except Resolver404 :
90
- exc = sys . exc_info () [0 ]
91
- if 'tried' in exc . args [ 0 ] :
92
- tried .extend ([[pattern ] + t for t in exc . args [ 0 ][ 'tried' ] ])
93
- elif 'path' in exc . args [ 0 ] :
94
- tried .extend ([[ pattern ] + t for t in exc . args [ 0 ][ 'path' ] ])
88
+ except Resolver404 as e :
89
+ tried_match = e . args [0 ]. get ( 'tried' )
90
+ if tried_match is not None :
91
+ tried .extend ([[pattern ] + t for t in tried_match ])
92
+ else :
93
+ tried .extend ([pattern ])
95
94
else :
96
95
if sub_match :
97
96
return pattern .page_id
98
97
tried .append (pattern .regex .pattern )
99
98
raise Resolver404 ({'tried' : tried , 'path' : new_path })
100
99
101
100
102
- def recurse_patterns (path , pattern_list , page_id , default_args = None ):
101
+ def recurse_patterns (path , pattern_list , page_id , default_args = None , nested = False ):
103
102
"""
104
103
Recurse over a list of to-be-hooked patterns for a given path prefix
105
104
"""
@@ -109,7 +108,7 @@ def recurse_patterns(path, pattern_list, page_id, default_args=None):
109
108
# make sure we don't get patterns that start with more than one '^'!
110
109
app_pat = app_pat .lstrip ('^' )
111
110
path = path .lstrip ('^' )
112
- regex = r'^%s%s' % (path , app_pat )
111
+ regex = r'^%s%s' % (path , app_pat ) if not nested else r'^%s' % ( app_pat )
113
112
if isinstance (pattern , RegexURLResolver ):
114
113
# this is an 'include', recurse!
115
114
resolver = RegexURLResolver (regex , 'cms_appresolver' ,
@@ -120,7 +119,7 @@ def recurse_patterns(path, pattern_list, page_id, default_args=None):
120
119
if default_args :
121
120
args .update (default_args )
122
121
# see lines 243 and 236 of urlresolvers.py to understand the next line
123
- resolver ._urlconf_module = recurse_patterns (regex , pattern .url_patterns , page_id , args )
122
+ resolver ._urlconf_module = recurse_patterns (regex , pattern .url_patterns , page_id , args , nested = True )
124
123
else :
125
124
# Re-do the RegexURLPattern with the new regular expression
126
125
args = pattern .default_args
@@ -133,14 +132,15 @@ def recurse_patterns(path, pattern_list, page_id, default_args=None):
133
132
return newpatterns
134
133
135
134
136
- def _flatten_patterns (patterns ):
137
- flat = []
135
+ def _set_permissions (patterns , exclude_permissions ):
138
136
for pattern in patterns :
139
137
if isinstance (pattern , RegexURLResolver ):
140
- flat += _flatten_patterns (pattern .url_patterns )
138
+ if pattern .namespace in exclude_permissions :
139
+ continue
140
+ _set_permissions (pattern .url_patterns , exclude_permissions )
141
141
else :
142
- flat . append ( pattern )
143
- return flat
142
+ from cms . utils . decorators import cms_perms
143
+ pattern . _callback = cms_perms ( pattern . callback )
144
144
145
145
146
146
def get_app_urls (urls ):
@@ -167,7 +167,6 @@ def get_patterns_for_title(path, title):
167
167
path += '/'
168
168
page_id = title .page .id
169
169
url_patterns += recurse_patterns (path , pattern_list , page_id )
170
- url_patterns = _flatten_patterns (url_patterns )
171
170
return url_patterns
172
171
173
172
@@ -228,9 +227,8 @@ def get_app_patterns():
228
227
resolver = AppRegexURLResolver (r'' , 'app_resolver' , app_name = app_ns , namespace = inst_ns )
229
228
resolver .page_id = page_id
230
229
if app .permissions :
231
- from cms .utils .decorators import cms_perms
232
- for pat in current_patterns :
233
- pat ._callback = cms_perms (pat .callback )
230
+ _set_permissions (current_patterns , app .exclude_permissions )
231
+
234
232
extra_patterns = patterns ('' , * current_patterns )
235
233
resolver .url_patterns_dict [lang ] = extra_patterns
236
234
app_patterns .append (resolver )
0 commit comments