combo/combo/public/menu.py

54 lines
2.0 KiB
Python

# combo - content management system
# Copyright (C) 2015 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.template.loader import get_template
def render_menu(context, level=0, depth=1):
context = get_menu_context(context, level=level, depth=depth)
template = get_template('combo/menu.html')
return template.render(context)
def get_menu_context(context, level=0, current_page=None, depth=1):
context['depth'] = depth
if current_page is None:
current_page = context['page']
page_of_level = current_page.get_page_of_level(level)
if level == -1:
# get children
elements = current_page.get_children()
elif page_of_level is None:
if level > 0:
parent_page = current_page.get_page_of_level(level-1)
elements = parent_page.get_children()
else:
context['menuitems'] = []
return context
else:
elements = page_of_level.get_siblings()
menuitems = []
for element in elements:
if element.exclude_from_navigation:
continue
if not context.get('render_skeleton'):
if not element.is_visible(context['request'].user):
continue
menuitem = {'page': element}
if element == page_of_level:
menuitem['selected'] = True
menuitems.append(menuitem)
context['menuitems'] = menuitems
return context