diff --git a/cpm_generic/templates/cpm_generic/tags/mainmenu.html b/cpm_generic/templates/cpm_generic/tags/mainmenu.html index e8789e6..017995d 100644 --- a/cpm_generic/templates/cpm_generic/tags/mainmenu.html +++ b/cpm_generic/templates/cpm_generic/tags/mainmenu.html @@ -8,8 +8,15 @@ {% endfor %} diff --git a/cpm_generic/templatetags/cpm_generic_tags.py b/cpm_generic/templatetags/cpm_generic_tags.py index 9d498d3..af1a7e0 100644 --- a/cpm_generic/templatetags/cpm_generic_tags.py +++ b/cpm_generic/templatetags/cpm_generic_tags.py @@ -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/.*)$' % 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)}