From 5b261a327c43894d82ff3d5ce6435bd85ffc7869 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Wed, 31 Aug 2016 00:07:54 +0200
Subject: [PATCH] Change url schema so mandatory args are handled by flask and
 converted links to url_for

---
 server.py             | 14 ++++----------
 templates/base.html   | 18 +++++++++---------
 templates/index.html  |  2 +-
 templates/macros.html | 12 ++++++------
 templates/play.html   |  2 +-
 5 files changed, 21 insertions(+), 27 deletions(-)

diff --git a/server.py b/server.py
index 31bf7f8..68d7249 100755
--- a/server.py
+++ b/server.py
@@ -81,11 +81,8 @@ def videos():
 def faq():
 	return render_template('faq.html')
 
-@app.route('/play')
-def play():
-	if not 'lectureid' in request.args:
-		return redirect(url_for('videos'))
-	id = request.args.get('lectureid')
+@app.route('/play/<int:id>')
+def play(id):
 	lectures = query('SELECT * FROM lectures WHERE id = ? AND (? OR visible)', id, ismod())
 	videos = query('SELECT * FROM videos WHERE lecture_id = ? AND (? OR visible)', id, ismod())
 	if not lectures:
@@ -112,11 +109,8 @@ def search():
 			'WHERE (? OR (coursevisible AND listed AND visible)) GROUP BY id ORDER BY _score DESC, time DESC LIMIT 30', ismod())
 	return render_template('search.html', searchtext=request.args['q'], courses=courses, lectures=lectures)
 
-@app.route('/course')
-def course():
-	if not 'courseid' in request.args:
-		return redirect(url_for('videos'))
-	id = request.args['courseid']
+@app.route('/course/<id>')
+def course(id):
 	courses = query('SELECT * FROM courses WHERE handle = ? AND (? OR visible)', id, ismod())
 	if not courses:
 		flash('Diese Veranstaltung existiert nicht!')
diff --git a/templates/base.html b/templates/base.html
index 7a3abd5..7996514 100644
--- a/templates/base.html
+++ b/templates/base.html
@@ -5,23 +5,23 @@
 	<head>
 		<title>Video AG</title>
 		<meta http-equiv="X-UA-Compatible" content="IE=edge">
-		<script src="static/jquery.js"></script>
-		<link rel="stylesheet" type="text/css" href="static/style.css">
-		<link rel="icon" type="image/png" href="static/favicon.png">
+		<script src="{{url_for('static', filename='jquery.js')}}"></script>
+		<link rel="stylesheet" type="text/css" href="{{url_for('static', filename='style.css')}}">
+		<link rel="icon" type="image/png" href="{{url_for('static', filename='favicon.png')}}">
 		<meta http-equiv="content-language" content="de-DE">
 		<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
 		<meta charset="UTF-8">
 		<meta name="viewport" content="width=device-width, initial-scale=1">
-		<link href="static/bootstrap/bootstrap.css" rel="stylesheet">
-		<script src="static/bootstrap/bootstrap.js"></script>
-		{%if ismod() %}<script src="static/moderator.js"></script>{% endif %}
+		<link href="{{url_for('static', filename='bootstrap/bootstrap.css')}}" rel="stylesheet">
+		<script src="{{url_for('static', filename='bootstrap/bootstrap.js')}}"></script>
+		{%if ismod() %}<script src="{{url_for('static', filename='moderator.js')}}"></script>{% endif %}
 	</head>
 	<body>
 		<nav class="navbar navbar-default navbar-static-top">
 			<div class="container-fluid">
 				<div class="row">
 					<div class="col-xs-1 hidden-xs hidden-sm">
-						<a href="/"><img src="/static/logo.png" style="width: 80px; padding: 5px"></a>
+						<a href="/"><img src="{{url_for('static', filename='logo.png')}}" style="width: 80px; padding: 5px"></a>
 					</div>
 					<div class="col-xs-12 col-md-11">
 						<div class="row">
@@ -63,12 +63,12 @@
 													{
 														html:true,
 														title:'Login für Moderatoren',
-														content:'<form method="post" action="login"><input placeholder="User" name="user" type="text"><br><input placeholder="Password" name="password" type="password"><br><input type="hidden" name="ref" value="{{ request.url|e }}"><input type="submit" value="Login"></form>'
+														content:'<form method="post" action="{{url_for('login')}}"><input placeholder="User" name="user" type="text"><br><input placeholder="Password" name="password" type="password"><br><input type="hidden" name="ref" value="{{ request.url|e }}"><input type="submit" value="Login"></form>'
 													}
 											)
 										</script>
 										{% else %}
-										<a href="/logout?ref={{ request.url|urlencode }}">
+										<a href="{{url_for('logout')}}?ref={{ request.url|urlencode }}">
 											{{ session.user.givenName }}
 											<span class="glyphicon glyphicon-log-out"></span>
 										</a>
diff --git a/templates/index.html b/templates/index.html
index 6ad3b97..ac45441 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -10,7 +10,7 @@
 			</div>
 			<div class="panel-body">
 				<p>Wir machen Vorlesungsvideos, damit du dir deine Vorlesungen angucken kannst, wann, wo und so oft <strong>du</strong> willst ;)</p>
-				<p><strong>Probleme?</strong><a href="/faq"> Hier gehts zur FAQ</a></p>
+				<p><strong>Probleme?</strong><a href="{{url_for('faq')}}"> Hier gehts zur FAQ</a></p>
 				<p>Wenn du die Videos nützlich fandest, schreib doch bitte den Dozenten eine kurze E-Mail. Waren die Videos grauenhaft? Kritik an uns.</p>
 				<p>Wenn du mitmachen willst, Fragen oder Anregungen hast, oder nur mal schauen möchtest, komm zu unserem AG-Treffen oder schreib uns eine E-Mail.
 				Insbesondere freuen wir uns über Studis der Mathematik und Physik, die ihre Vorlesungen filmen wollen.</p>
diff --git a/templates/macros.html b/templates/macros.html
index 53b1981..4ecd4e4 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -1,7 +1,7 @@
 {% macro preview(lecture) %}
 
 <li class="list-group-item">
-	<a class="hidden-xs" href="/play?lectureid={{ lecture['id'] }}" title="{{ lecture['coursetitle'] }}">
+	<a class="hidden-xs" href="{{url_for('play', id=lecture['id'])}}" title="{{ lecture['coursetitle'] }}">
 		<div class="row">
 			<img class="col-xs-4" src="{{ videoprefix }}/{{ lecture['titlefile'] }}" alt="Vorschaubild">
 			<div class="col-xs-4">
@@ -17,7 +17,7 @@
 			</div>
 		</div>
 	</a>
-	<a class="visible-xs" href="/play?lectureid={{ lecture['id'] }}" title="{{ lecture['coursetitle'] }}">
+	<a class="visible-xs" href="{{url_for('play', id=lecture['id'])}}" title="{{ lecture['coursetitle'] }}">
 		<div class="row">
 			<img class="col-xs-12" src="{{ videoprefix }}/{{ lecture['titlefile'] }}" alt="Vorschaubild">
 		</div>
@@ -50,8 +50,8 @@
 {% endmacro %}
 
 {% macro player(lecture, videos) %}
-<script src="static/mediaelementjs/mediaelement-and-player.min.js"></script>
-<link rel="stylesheet" href="static/mediaelementjs/mediaelementplayer.css" />
+<script src="{{url_for('static', filename='/mediaelementjs/mediaelement-and-player.min.js')}}"></script>
+<link rel="stylesheet" href="{{url_for('static', filename='mediaelementjs/mediaelementplayer.css')}}" />
 <video class="mejs-player" width="640" height="360" style="width: 100%; height: 100%;">
 	{% for v in videos %}
 		<source type="video/mp4" src="{{ videoprefix }}/{{ v.path }}" />
@@ -68,7 +68,7 @@
 {% macro course_list_item(course,show_semester=False) %}
 <li class="list-group-item {% if (not course.visible) or (not course.listed) %}list-group-item-danger{% endif %}">
 	<div class="row">
-		<a href=/course?courseid={{course.handle}}>
+		<a href="{{url_for('course', id=course.handle)}}">
 			{% if show_semester %}
 				<span class="col-xs-1">
 					{{ course.semester }}
@@ -147,7 +147,7 @@ $('#embedcodebtn').popover(
 				<span class="dropdown">
 					{{ video_download_btn(videos) }}
 				</span>
-				<a href="/play?lectureid={{lecture.id}}" class="btn btn-default {% if videos|length is equalto 0 %}disabled{% endif %}">
+				<a href="{{url_for('play', id=lecture.id)}}" class="btn btn-default {% if videos|length is equalto 0 %}disabled{% endif %}">
 					<span class="glyphicon glyphicon-play"></span>
 					Play
 				</a>
diff --git a/templates/play.html b/templates/play.html
index 76eef77..4b78d0f 100644
--- a/templates/play.html
+++ b/templates/play.html
@@ -11,7 +11,7 @@
 	</div>
 	<div class="panel-body">
 		<p class="col-xs-12" style="padding: 0px;">
-			<span><a href="/course?courseid={{course.handle}}" class="btn btn-default" >Zur Veranstaltungsseite</a><span>
+			<span><a href="{{url_for('course', id=course.handle)}}" class="btn btn-default" >Zur Veranstaltungsseite</a><span>
 			<span class="pull-right">
 				<span>{{ video_embed_btn(lecture.id) }}</span>
 				<span>{{video_download_btn(videos)}}</span>
-- 
GitLab