From 5f1f10d0aa16236bef631ef17ea6d04d68c3eb7b Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Mon, 29 Aug 2016 21:50:32 +0200
Subject: [PATCH] Implemented navbar items as decorators

---
 server.py             | 10 ++++++++++
 templates/base.html   | 13 +++----------
 templates/faq.html    |  1 -
 templates/index.html  |  1 -
 templates/videos.html |  2 --
 5 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/server.py b/server.py
index c914df7..4ff1396 100755
--- a/server.py
+++ b/server.py
@@ -136,7 +136,15 @@ def mod_required(func):
 			return func(*args, **kwargs)
 	return decorator
 
+app.jinja_env.globals['navbar'] = []
+def register_navbar(name, icon=None):
+	def wrapper(func):
+		app.jinja_env.globals['navbar'].append((func.__name__, name, icon))
+		return func
+	return wrapper
+
 @app.route('/')
+@register_navbar('Home', icon='home')
 def index():
 	return render_template('index.html', latestvideos=query('''
 				SELECT lectures.*, max(videos.time_updated) AS lastvidtime, courses.short, courses.downloadable, courses.title AS coursetitle
@@ -150,6 +158,7 @@ def index():
 			''', ismod()))
 
 @app.route('/videos')
+@register_navbar('Videos', icon='film')
 def videos():
 	courses = query('SELECT * FROM courses WHERE (? OR (visible AND listed))', ismod())
 	for course in courses:
@@ -161,6 +170,7 @@ def videos():
 	return render_template('videos.html', courses=courses, groupedby=groupedby)
 
 @app.route('/faq')
+@register_navbar('FAQ', icon='question-sign')
 def faq():
 	return render_template('faq.html')
 
diff --git a/templates/base.html b/templates/base.html
index a6ad221..2c7eb73 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -1,10 +1,3 @@
-{% set navigation_bar = [
-('/', 'index', 'Home', 'home'),
-('/videos', 'videos', 'Videos', 'film'),
-('/faq', 'faq', 'FAQ', 'question-sign')
-] -%}
-{% set active_page = active_page|default('none') -%}
-
 {% set page_border = page_border|default(1) -%}
 
 <!DOCTYPE html>
@@ -53,9 +46,9 @@
 						<div class="row">
 							<div class="col-xs-12 col-sm-8">
 								<ul class="nav nav-pills">
-									{% for href, id, caption, gly in navigation_bar %}
-									 <li{% if id == active_page %} class="active"{% endif %}>
-										 <a href="{{ href|e }}">{% if gly != '' %}<span class="glyphicon glyphicon-{{ gly }}"></span> {% endif %}{{ caption }}</a>
+									{% for endpoint, caption, gly in navbar %}
+									 <li{% if endpoint == request.endpoint %} class="active"{% endif %}>
+										 <a href="{{ url_for(endpoint)|e }}">{% if gly != '' %}<span class="glyphicon glyphicon-{{ gly }}"></span> {% endif %}{{ caption }}</a>
 									</li>
 									{% endfor %}
 									<li class="navbar-right">
diff --git a/templates/faq.html b/templates/faq.html
index 737585a..29519ca 100644
--- a/templates/faq.html
+++ b/templates/faq.html
@@ -1,6 +1,5 @@
 {% from 'macros.html' import preview %}
 {% extends "base.html" %}
-{% set active_page = "faq" %}
 {% block content %}
 <div class="alert alert-warning alert-dismissible" role="alert" id="kontakt">
 	Unter <a href="mailto:video@fsmpi.rwth-aachen.de">video@fsmpi.rwth-aachen.de</a> stehen wir für alle Fragen bereit.
diff --git a/templates/index.html b/templates/index.html
index 32134c5..6ad3b97 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -1,6 +1,5 @@
 {% from 'macros.html' import preview %}
 {% extends "base.html" %}
-{% set active_page = "index" %}
 {% set page_border = 0 %}
 {% block content %}
 <div class="row">
diff --git a/templates/videos.html b/templates/videos.html
index 931c1d6..57feef3 100644
--- a/templates/videos.html
+++ b/templates/videos.html
@@ -1,6 +1,4 @@
 {% from 'macros.html' import course_list_item %}
-{% set active_page = 'videos' -%}
-
 {% extends "base.html" %}
 {% block content %}
 <div class="row">
-- 
GitLab