-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
menu_tags.py
555 lines (487 loc) · 20.8 KB
/
menu_tags.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
from __future__ import unicode_literals
from copy import copy
from django.template import Library
from django.utils.translation import ugettext_lazy as _
from wagtail.wagtailcore.models import Page
from .. import app_settings, get_main_menu_model, get_flat_menu_model
from ..models import MenuFromRootPage, MenuItem
flat_menus_fbtdsm = app_settings.FLAT_MENUS_FALL_BACK_TO_DEFAULT_SITE_MENUS
register = Library()
def validate_supplied_values(tag, max_levels=None, use_specific=None,
parent_page=None, menuitem_or_page=None):
if max_levels is not None:
if max_levels not in (1, 2, 3, 4, 5):
raise ValueError(_(
"The `%s` tag expects `max_levels` to be an integer value "
"between 1 and 5. Please review your template.") % tag)
if use_specific is not None:
if use_specific not in (0, 1, 2, 3):
raise ValueError(_(
"The `%s` tag expects `use_specific` to be an integer value "
"between 0 and 3. Please review your template.") % tag)
if parent_page is not None:
if not isinstance(parent_page, Page):
raise ValueError(_(
"The `%s` tag expects `parent_page` to be a `Page` instance. "
"A value of type `%s` was supplied.") %
(tag, parent_page.__class__))
if menuitem_or_page is not None:
if not isinstance(menuitem_or_page, (Page, MenuItem)):
raise ValueError(_(
"The `%s` tag expects `menuitem_or_page` to be a `Page` or "
"`MenuItem` instance. A value of type `%s` was supplied.") %
(tag, menuitem_or_page.__class__))
def get_attrs_from_context(context, guess_tree_position=True):
"""
Gets a bunch of useful things from the context/request and returns them as
a tuple for use in most menu tags.
"""
request = context['request']
site = request.site
wagtailmenus_vals = context.get('wagtailmenus_vals')
current_page = wagtailmenus_vals.get('current_page')
section_root = wagtailmenus_vals.get('section_root')
ancestor_ids = wagtailmenus_vals.get('current_page_ancestor_ids')
return (request, site, current_page, section_root, ancestor_ids)
@register.simple_tag(takes_context=True)
def main_menu(
context, max_levels=None, use_specific=None, apply_active_classes=True,
allow_repeating_parents=True, show_multiple_levels=True,
template=app_settings.DEFAULT_MAIN_MENU_TEMPLATE,
sub_menu_template=app_settings.DEFAULT_SUB_MENU_TEMPLATE
):
validate_supplied_values('main_menu', max_levels=max_levels,
use_specific=use_specific)
# Variabalise relevant attributes from context
request, site, current_page, root, ancestor_ids = get_attrs_from_context(
context, guess_tree_position=True)
# Find a matching menu
menu = get_main_menu_model().get_for_site(site)
if not show_multiple_levels:
max_levels = 1
if max_levels is not None:
menu.set_max_levels(max_levels)
if use_specific is not None:
menu.set_use_specific(use_specific)
context.update({
'menu_items': prime_menu_items(
menu_items=menu.top_level_items,
current_site=site,
current_page=current_page,
current_page_ancestor_ids=ancestor_ids,
request_path=request.path,
use_specific=menu.use_specific,
original_menu_tag='main_menu',
menu_instance=menu,
check_for_children=menu.max_levels > 1,
allow_repeating_parents=allow_repeating_parents,
apply_active_classes=apply_active_classes
),
'main_menu': menu,
'use_specific': menu.use_specific,
'max_levels': menu.max_levels,
'apply_active_classes': apply_active_classes,
'allow_repeating_parents': allow_repeating_parents,
'current_level': 1,
'current_template': template,
'sub_menu_template': sub_menu_template,
'original_menu_tag': 'main_menu',
'section_root': root,
'current_ancestor_ids': ancestor_ids
})
t = context.template.engine.get_template(template)
return t.render(context)
@register.simple_tag(takes_context=True)
def flat_menu(
context, handle, max_levels=None, use_specific=None,
show_menu_heading=False, apply_active_classes=False,
allow_repeating_parents=True, show_multiple_levels=True,
template=app_settings.DEFAULT_FLAT_MENU_TEMPLATE,
sub_menu_template=app_settings.DEFAULT_SUB_MENU_TEMPLATE,
fall_back_to_default_site_menus=flat_menus_fbtdsm,
):
validate_supplied_values('flat_menu', max_levels=max_levels,
use_specific=use_specific)
# Variabalise relevant attributes from context
request, site, current_page, root, ancestor_ids = get_attrs_from_context(
context, guess_tree_position=False)
# Find a matching menu
menu = get_flat_menu_model().get_for_site(
handle, site, fall_back_to_default_site_menus
)
if not menu:
# No menu was found matching `handle`, so gracefully render nothing.
return ''
if not show_multiple_levels:
max_levels = 1
if max_levels is not None:
menu.set_max_levels(max_levels)
if use_specific is not None:
menu.set_use_specific(use_specific)
context.update({
'menu_items': prime_menu_items(
menu_items=menu.top_level_items,
current_site=site,
current_page=current_page,
current_page_ancestor_ids=ancestor_ids,
request_path=request.path,
use_specific=menu.use_specific,
original_menu_tag='flat_menu',
menu_instance=menu,
check_for_children=menu.max_levels > 1,
allow_repeating_parents=allow_repeating_parents,
apply_active_classes=apply_active_classes
),
'matched_menu': menu,
'menu_handle': handle,
'menu_heading': menu.heading,
'use_specific': menu.use_specific,
'max_levels': menu.max_levels,
'show_menu_heading': show_menu_heading,
'apply_active_classes': apply_active_classes,
'allow_repeating_parents': allow_repeating_parents,
'current_level': 1,
'current_template': template,
'sub_menu_template': sub_menu_template,
'original_menu_tag': 'flat_menu',
'current_ancestor_ids': ancestor_ids
})
t = context.template.engine.get_template(template)
return t.render(context)
def get_sub_menu_items_for_page(
page, request, current_site, current_page, ancestor_ids, menu_instance,
use_specific, apply_active_classes, allow_repeating_parents,
current_level=1, max_levels=2, original_menu_tag=''
):
# The menu items will be the children of the provided `page`
children_pages = menu_instance.get_children_for_page(page)
# Call `prime_menu_items` to prepare the children pages for output. This
# will add `href`, `text`, `active_class` and `has_children_in_menu`
# attributes to each item, to use in menu templates.
menu_items = prime_menu_items(
menu_items=children_pages,
current_site=current_site,
current_page=current_page,
current_page_ancestor_ids=ancestor_ids,
request_path=request.path,
use_specific=use_specific,
original_menu_tag=original_menu_tag,
menu_instance=menu_instance,
check_for_children=current_level < max_levels,
allow_repeating_parents=allow_repeating_parents,
apply_active_classes=apply_active_classes
)
"""
If `page` has a `modify_submenu_items` method, send the primed
menu_items list to that for further modification (e.g. adding a copy of
`page` as the first item, using fields from `MenuPage`)
"""
if (
use_specific and (
hasattr(page, 'modify_submenu_items') or
hasattr(page.specific_class, 'modify_submenu_items')
)
):
if type(page) is Page:
page = page.specific
menu_items = page.modify_submenu_items(
menu_items=menu_items,
current_page=current_page,
current_ancestor_ids=ancestor_ids,
current_site=current_site,
allow_repeating_parents=allow_repeating_parents,
apply_active_classes=apply_active_classes,
original_menu_tag=original_menu_tag,
menu_instance=menu_instance
)
return page, menu_items
@register.simple_tag(takes_context=True)
def sub_menu(
context, menuitem_or_page, stop_at_this_level=None, use_specific=None,
allow_repeating_parents=None, apply_active_classes=None,
template=None
):
"""
Retrieve the children pages for the `menuitem_or_page` provided, turn them
into menu items, and render them to a template.
"""
validate_supplied_values('sub_menu', use_specific=use_specific,
menuitem_or_page=menuitem_or_page)
# Variabalise relevant attributes from context
request, site, current_page, root, ancestor_ids = get_attrs_from_context(
context, guess_tree_position=False)
max_levels = context.get(
'max_levels', app_settings.DEFAULT_CHILDREN_MENU_MAX_LEVELS)
previous_level = context.get('current_level', 2)
current_level = previous_level + 1
if use_specific is None:
use_specific = context.get(
'use_specific', app_settings.USE_SPECIFIC_AUTO)
if apply_active_classes is None:
apply_active_classes = context.get('apply_active_classes', True)
if allow_repeating_parents is None:
allow_repeating_parents = context.get('allow_repeating_parents', True)
if template is None:
template = context.get(
'sub_menu_template', app_settings.DEFAULT_SUB_MENU_TEMPLATE)
original_menu_tag = context.get('original_menu_tag', 'sub_menu')
if original_menu_tag == 'main_menu':
menu_instance = context.get('main_menu')
elif original_menu_tag == 'flat_menu':
menu_instance = context.get('matched_menu')
else:
menu_instance = context.get('menu_instance')
# Identify the Page that we need to get children for
if isinstance(menuitem_or_page, Page):
parent_page = menuitem_or_page
else:
parent_page = menuitem_or_page.link_page
parent_page, menu_items = get_sub_menu_items_for_page(
page=parent_page,
request=request,
current_site=site,
current_page=current_page,
ancestor_ids=ancestor_ids,
menu_instance=menu_instance,
use_specific=use_specific,
original_menu_tag=original_menu_tag,
current_level=current_level,
max_levels=max_levels,
apply_active_classes=apply_active_classes,
allow_repeating_parents=allow_repeating_parents
)
context = copy(context)
context.update({
'parent_page': parent_page,
'menu_items': menu_items,
'apply_active_classes': apply_active_classes,
'allow_repeating_parents': allow_repeating_parents,
'current_level': current_level,
'max_levels': max_levels,
'current_template': template,
'original_menu_tag': original_menu_tag
})
t = context.template.engine.get_template(template)
return t.render(context)
@register.simple_tag(takes_context=True)
def section_menu(
context, show_section_root=True, show_multiple_levels=True,
apply_active_classes=True, allow_repeating_parents=True,
max_levels=app_settings.DEFAULT_SECTION_MENU_MAX_LEVELS,
template=app_settings.DEFAULT_SECTION_MENU_TEMPLATE,
sub_menu_template=app_settings.DEFAULT_SUB_MENU_TEMPLATE,
use_specific=app_settings.DEFAULT_SECTION_MENU_USE_SPECIFIC,
):
"""Render a section menu for the current section."""
validate_supplied_values('section_menu', max_levels=max_levels,
use_specific=use_specific)
# Variabalise relevant attributes from context
request, site, current_page, root, ancestor_ids = get_attrs_from_context(
context, guess_tree_position=True)
# If the section root couldn't be identified, render nothing.
if root is None:
return ''
if not show_multiple_levels:
max_levels = 1
# Create a menu instance that can fetch all pages at once and return
# for subpages for each branch as they are needed
menu_instance = MenuFromRootPage(root, max_levels, use_specific)
section_root, menu_items = get_sub_menu_items_for_page(
page=root,
request=request,
current_site=site,
current_page=current_page,
ancestor_ids=ancestor_ids,
menu_instance=menu_instance,
use_specific=use_specific,
original_menu_tag='section_menu',
current_level=1,
max_levels=max_levels,
apply_active_classes=apply_active_classes,
allow_repeating_parents=allow_repeating_parents
)
"""
We want `section_root` to have the same attributes as primed menu
items, so it can be used in the same way in a template if required.
"""
setattr(section_root, 'text', section_root.title)
setattr(section_root, 'href', section_root.relative_url(site))
if apply_active_classes:
active_class = app_settings.ACTIVE_ANCESTOR_CLASS
if current_page and section_root.pk == current_page.pk:
# `section_root` is the current page, so should probably have
# the 'active' class. But, not if there's a repeated item in
# menu_items that already has the 'active' class.
active_class = app_settings.ACTIVE_CLASS
if allow_repeating_parents and use_specific and menu_items:
if menu_items[0].active_class == app_settings.ACTIVE_CLASS:
active_class = app_settings.ACTIVE_ANCESTOR_CLASS
setattr(section_root, 'active_class', active_class)
context = copy(context)
context.update({
'section_root': section_root,
'menu_instance': menu_instance,
'menu_items': menu_items,
'show_section_root': show_section_root,
'apply_active_classes': apply_active_classes,
'allow_repeating_parents': allow_repeating_parents,
'current_level': 1,
'max_levels': max_levels,
'current_template': template,
'sub_menu_template': sub_menu_template,
'original_menu_tag': 'section_menu',
'current_ancestor_ids': ancestor_ids,
'use_specific': use_specific
})
t = context.template.engine.get_template(template)
return t.render(context)
@register.simple_tag(takes_context=True)
def children_menu(
context, parent_page=None, allow_repeating_parents=True,
apply_active_classes=False,
max_levels=app_settings.DEFAULT_CHILDREN_MENU_MAX_LEVELS,
template=app_settings.DEFAULT_CHILDREN_MENU_TEMPLATE,
sub_menu_template=app_settings.DEFAULT_SUB_MENU_TEMPLATE,
use_specific=app_settings.DEFAULT_CHILDREN_MENU_USE_SPECIFIC,
):
request, site, current_page, root, ancestor_ids = get_attrs_from_context(
context, guess_tree_position=False)
validate_supplied_values(
'children_menu', max_levels=max_levels, use_specific=use_specific,
parent_page=parent_page)
# Use current page as parent_page if no value supplied
if parent_page is None:
parent_page = context.get('self')
if not parent_page:
return ''
# Create a menu instance that can fetch all pages at once and return
# for subpages for each branch as they are needed
menu_instance = MenuFromRootPage(parent_page, max_levels, use_specific)
parent_page, menu_items = get_sub_menu_items_for_page(
page=parent_page,
request=request,
current_site=site,
current_page=current_page,
ancestor_ids=ancestor_ids,
menu_instance=menu_instance,
use_specific=use_specific,
original_menu_tag='children_menu',
current_level=1,
max_levels=max_levels,
apply_active_classes=apply_active_classes,
allow_repeating_parents=allow_repeating_parents
)
context.update({
'parent_page': parent_page,
'menu_instance': menu_instance,
'menu_items': menu_items,
'apply_active_classes': apply_active_classes,
'allow_repeating_parents': allow_repeating_parents,
'current_level': 1,
'max_levels': max_levels,
'original_menu_tag': 'children_menu',
'current_template': template,
'sub_menu_template': sub_menu_template,
'use_specific': use_specific
})
t = context.template.engine.get_template(template)
return t.render(context)
def prime_menu_items(
menu_items, current_site, current_page, current_page_ancestor_ids,
request_path, use_specific, original_menu_tag, menu_instance,
check_for_children=False, allow_repeating_parents=True,
apply_active_classes=True
):
"""
Prepare a list of `MenuItem` or `Page` objects for rendering to a menu
template.
"""
primed_menu_items = []
for item in menu_items:
try:
"""
`menu_items` is a list of `MenuItem` objects from
`Menu.top_level_items`. Any `link_page` values will have been
replaced with specific pages if necessary
"""
page = item.link_page
menuitem = item
setattr(item, 'text', item.menu_text)
except AttributeError:
"""
`menu_items` is a list of `Page` objects
"""
page = item
menuitem = None
text = getattr(
page, app_settings.PAGE_FIELD_FOR_MENU_ITEM_TEXT, page.title
)
setattr(item, 'text', text)
if page:
"""
First we work out whether this item should be flagged as needing
a sub-menu. It can be expensive, so we try to only do the working
out when absolutely necessary.
"""
has_children_in_menu = False
if (
check_for_children and
page.depth >= app_settings.SECTION_ROOT_DEPTH and
(menuitem is None or menuitem.allow_subnav)
):
if (
use_specific and (
hasattr(page, 'has_submenu_items') or
hasattr(page.specific_class, 'has_submenu_items')
)
):
if type(page) is Page:
page = page.specific
"""
If the page has a `has_submenu_items` method, give
responsibilty for determining `has_children_in_menu`
to that.
"""
has_children_in_menu = page.has_submenu_items(
current_page=current_page,
allow_repeating_parents=allow_repeating_parents,
original_menu_tag=original_menu_tag,
menu_instance=menu_instance
)
else:
has_children_in_menu = menu_instance.page_has_children(
page)
setattr(item, 'has_children_in_menu', has_children_in_menu)
if apply_active_classes:
active_class = ''
if(current_page and page.pk == current_page.pk):
# This is the current page, so the menu item should
# probably have the 'active' class
active_class = app_settings.ACTIVE_CLASS
if (
allow_repeating_parents and use_specific and
has_children_in_menu
):
if type(page) is Page:
page = page.specific
if getattr(page, 'repeat_in_subnav', False):
active_class = app_settings.ACTIVE_ANCESTOR_CLASS
elif page.pk in current_page_ancestor_ids:
active_class = app_settings.ACTIVE_ANCESTOR_CLASS
setattr(item, 'active_class', active_class)
elif page is None:
"""
This is a `MenuItem` for a custom URL. It can be classed as
'active' if the URL matches the request.path.
"""
if apply_active_classes and item.link_url == request_path:
setattr(item, 'active_class', app_settings.ACTIVE_CLASS)
# In case the specific page was fetched during the above operations
# We'll set `MenuItem.link_page` to that specific page.
if menuitem and page:
menuitem.link_page = page
# Both `Page` and `MenuItem` objects have a `relative_url` method
# we can use to calculate a value for the `href` attribute.
setattr(item, 'href', item.relative_url(current_site))
primed_menu_items.append(item)
return primed_menu_items