-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract top navigation menu structure from wagtail Page tree. Extract link for root page from HomePage object. Detect current page in main menu and set style as `active`. Render only children that have "Show in menu" flag set. Show translated menu items by using `caption` if available.
- Loading branch information
Showing
2 changed files
with
30 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,39 @@ | ||
import re | ||
|
||
from django import template | ||
from django.utils import translation | ||
from django.conf import settings | ||
|
||
from home.models import HomePage | ||
|
||
|
||
register = template.Library() | ||
|
||
|
||
@register.inclusion_tag('cpm_generic/tags/mainmenu.html') | ||
def mainmenu(request): | ||
def get_menu_pages(full_path): | ||
homepage = HomePage.objects.get(slug='home') | ||
children = list(homepage.get_children().live().in_menu().specific()) | ||
return [(page, page.url == full_path) for page in [homepage] + children] | ||
|
||
|
||
def get_language_paths(full_path): | ||
curr_lang = translation.get_language().split('-')[0] | ||
full_path = request.get_full_path() | ||
match = re.match(r'^/%s(?P<path>/.*)$' % curr_lang, full_path) | ||
path = match.group('path') if match else full_path | ||
prefix = '/%s/' % curr_lang | ||
path = full_path | ||
if path.startswith(prefix): | ||
path = path[len(prefix):] | ||
|
||
languages = [ | ||
return [ | ||
( | ||
code, | ||
name, | ||
'/%s%s' % (code, path), | ||
'/%s/%s' % (code, path), | ||
code == curr_lang | ||
) for code, name in settings.LANGUAGES | ||
] | ||
|
||
return {'languages': languages} | ||
|
||
@register.inclusion_tag('cpm_generic/tags/mainmenu.html') | ||
def mainmenu(request): | ||
full_path = request.get_full_path() | ||
return {'languages': get_language_paths(full_path), | ||
'menupages': get_menu_pages(full_path)} |