-
Tor-Einar Skog authoredTor-Einar Skog authored
views.py 4.67 KiB
#
# Copyright (c) 2015 NIBIO <http://www.nibio.no/>.
#
# This file is part of VIPSWeb.
# VIPSWeb is free software: you can redistribute it and/or modify
# it under the terms of the NIBIO Open Source License as published by
# NIBIO, either version 1 of the License, or (at your option) any
# later version.
#
# VIPSWeb 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
# NIBIO Open Source License for more details.
#
# You should have received a copy of the NIBIO Open Source License
# along with VIPSWeb. If not, see <http://www.nibio.no/licenses/>.
#
from django.shortcuts import render
from django.core.exceptions import ObjectDoesNotExist
from django.http import Http404
from information.models import InformationLocale, Information
from django.urls import reverse
def index(request):
# Check that root element exists
try:
root_information = Information.objects.filter(parent_information__isnull=True)
return detail(request,root_information[0].id)
except:
# Fallback: Return an empty page
context = {}
return render(request, 'information/index.html', context)
def detail(request, information_id):
try:
information_locale = InformationLocale.get_information_locale_with_fallback(information_id, request.LANGUAGE_CODE)
# Is there a parent?
#parent_information = information_locale.information.parent_information
# We get all the children too
#children_locales = []
#for child in information_locale.information.children.all():
# children_locales.append(InformationLocale.get_information_locale_with_fallback(child.pk))
context = {
'information_locale' : information_locale,
#'parent_information' : parent_information,
#'children_locales':children_locales,
'breadcrumb': get_breadcrumb(information_locale.information, request.LANGUAGE_CODE),
'menu_html': get_menu_html(None, information_id, request.LANGUAGE_CODE)
}
return render(request, 'information/detail.html', context)
except ObjectDoesNotExist:
raise Http404("No information page found with id=%s" % information_id)
def get_breadcrumb(information, language):
active_id= information.pk
breadcrumb = []#[]
while information != None:
information_locale = InformationLocale.get_information_locale_with_fallback(information.pk, language)
children_locales = []
for child in information_locale.information.children.all():
children_locales.append(InformationLocale.get_information_locale_with_fallback(child.pk))
breadcrumb.append({
"id":information_locale.information.pk,
"title":information_locale.headline,
"active" : information.pk == active_id,
"children_locales" : children_locales
})
information = information.parent_information
return breadcrumb
menu_template = """
<nav>
<ul class="nav">
%s
</ul>
</nav>
"""
link_title = """<a href="%s">%s</a>"""
toggleable_item_template="""
<li class="%s">
%s
<ul class="nav">
%s
</ul>
</li>
"""
simple_item_template = """<li class="%s">%s</li>"""
## Recursive method
def get_menu_html(parent_information, active_information_pk, language):
# We start with backtracking from the active information object
if(parent_information == None):
items = Information.objects.filter(parent_information__isnull=True)
else:
items = Information.objects.filter(parent_information=parent_information)
items_html = []
for info in items:
info_locale = InformationLocale.get_information_locale_with_fallback(info.pk, language)
if info.pk == int(active_information_pk):
active_class = "active"
title = """<span class="active">%s</span>""" % info_locale.headline
else:
active_class = ""
title = link_title % (reverse('information:detail', kwargs={"information_id":info.pk}), info_locale.headline)
child_menu_html = get_menu_html(info, active_information_pk, language)
# If item has no child items, create simple item
if child_menu_html == "":
items_html.append(simple_item_template % (active_class, title))
# Else create complex item
else:
items_html.append(toggleable_item_template % (active_class, title, child_menu_html))
return "".join(items_html)