1
1
import copy
2
2
import re
3
+ import sys
3
4
4
5
from django .conf import settings
6
+ from django .utils .text import slugify
5
7
6
8
try :
7
9
from django .apps import apps
@@ -119,19 +121,12 @@ def process(c, request, name=None):
119
121
if curitem is not None :
120
122
curitem .selected = True
121
123
122
- def filter_visible (items ):
123
- return [
124
- filter_visible_children (item )
125
- for item in items
126
- if item .visible
127
- ]
128
-
129
- def filter_visible_children (item ):
130
- item .children = filter_visible (item .children )
131
- return item
132
-
133
124
# return only visible items
134
- visible = filter_visible (items )
125
+ visible = [
126
+ item
127
+ for item in items
128
+ if item .visible
129
+ ]
135
130
136
131
# determine if we should apply 'selected' to parents when one of their
137
132
# children is the 'selected' menu
@@ -178,10 +173,8 @@ def __init__(self, title, url, children=[], weight=1, check=None,
178
173
179
174
self .url = url
180
175
self .title = title
181
- self ._title = None
182
176
self .visible = visible
183
177
self .children = children
184
- self ._children = None
185
178
self .weight = weight
186
179
self .check = check
187
180
self .slug = slug
@@ -193,39 +186,57 @@ def __init__(self, title, url, children=[], weight=1, check=None,
193
186
for k in kwargs :
194
187
setattr (self , k , kwargs [k ])
195
188
196
- # if title is a callable store a reference to it for later
197
- # then we'll process it at runtime
198
- if callable (title ):
199
- self .title = ""
200
- self ._title = title
201
-
202
189
def process (self , request ):
203
190
"""
204
191
process determines if this item should visible, if its selected, etc...
205
192
"""
206
- self .check_check (request )
193
+ # evaluate our check
194
+ if callable (self .check ):
195
+ self .visible = self .check (request )
196
+
197
+ # if we're not visible we return since we don't need to do anymore processing
207
198
if not self .visible :
208
199
return
209
200
210
- # evaluate title
211
- self .check_title (request )
201
+ # evaluate our title
202
+ if callable (self .title ):
203
+ self .title = self .title (request )
204
+
205
+ # if no title is set turn it into a slug
206
+ if self .slug is None :
207
+ # in python3 we don't need to convert to unicode, in python2 slugify
208
+ # requires a unicode string
209
+ if sys .version_info > (3 , 0 ):
210
+ self .slug = slugify (self .title )
211
+ else :
212
+ self .slug = slugify (unicode (self .title ))
212
213
213
214
# evaluate children
214
- visible_children = []
215
- self .check_children (request )
215
+ if callable (self .children ):
216
+ children = list (self .children (request ))
217
+ else :
218
+ children = list (self .children )
216
219
217
- for child in self .children :
220
+ for child in children :
221
+ child .parent = self
218
222
child .process (request )
219
- if child .visible :
220
- visible_children .append (child )
221
223
224
+ self .children = [
225
+ child
226
+ for child in children
227
+ if child .visible
228
+ ]
229
+ self .children .sort (key = lambda child : child .weight )
230
+
231
+ # if we have no children and MENU_HIDE_EMPTY then we are not visible and should return
222
232
hide_empty = getattr (settings , 'MENU_HIDE_EMPTY' , False )
223
- if hide_empty and not self .check and not len ( visible_children ) :
233
+ if hide_empty and len ( self .children ) == 0 :
224
234
self .visible = False
225
235
return
226
236
237
+ # find out if one of our children is selected, and mark it as such
227
238
curitem = None
228
- for item in visible_children :
239
+ for item in self . children :
229
240
item .selected = False
230
241
231
242
if item .match_url (request ):
@@ -246,36 +257,3 @@ def match_url(self, request):
246
257
elif re .match ("%s" % self .url , request .path ):
247
258
matched = True
248
259
return matched
249
-
250
- def check_children (self , request ):
251
- """
252
- Check children against the given request
253
- """
254
- if callable (self ._children ):
255
- children = self ._children (request )
256
- elif callable (self .children ):
257
- children = self .children (request )
258
- self ._children = self .children
259
- else :
260
- children = self .children
261
-
262
- children = [child for child in children ]
263
- children .sort (key = lambda child : child .weight )
264
- for child in children :
265
- child .parent = self
266
-
267
- self .children = children
268
-
269
- def check_check (self , request ):
270
- """
271
- Set our visibility based on our check against the given request
272
- """
273
- if callable (self .check ):
274
- self .visible = self .check (request )
275
-
276
- def check_title (self , request ):
277
- if callable (self ._title ):
278
- self .title = self ._title (request )
279
- if self .slug is None :
280
- self .slug = re .sub (r'[^a-zA-Z0-9\-]+' , '_' ,
281
- self .title .lower ()).strip ('_' )
0 commit comments