Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement main menu #29

Merged
merged 13 commits into from
Aug 8, 2016
11 changes: 9 additions & 2 deletions cpm_generic/templates/cpm_generic/tags/mainmenu.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,15 @@
{% endfor %}
</ul>
<ul class="nav pull-right">
<li class="active">
<a href="/">Home</a>
{% for page, is_active in menupages %}
<li{% if is_active %} class="active"{% endif %}>
{% if page.caption %}
<a href="{{ page.url }}">{{ page.caption }}</a>
{% else %}
<a href="{{ page.url }}">{{ page.title }}</a>
{% endif %}
</li>
{% endfor %}

<li class="divider-vertical"></li>
</ul>
30 changes: 21 additions & 9 deletions cpm_generic/templatetags/cpm_generic_tags.py
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)}