From b0cd3e8993a34da0ef0a296b1829925ff45a56c5 Mon Sep 17 00:00:00 2001
From: Julian Rother <julianr@fsmpi.rwth-aachen.de>
Date: Thu, 11 Aug 2016 18:32:33 +0200
Subject: [PATCH] Add simple DB connection and adopt templates to use it

---
 server.py             | 24 ++++++++++++++++++++++--
 templates/index.html  |  2 +-
 templates/macros.html | 18 ++++++++++--------
 3 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/server.py b/server.py
index 70b7342..27f0952 100755
--- a/server.py
+++ b/server.py
@@ -1,10 +1,30 @@
 #!/bin/python
-from flask import Flask, render_template
+from flask import Flask, render_template, g
+import mysql.connector
+import config
+
 app = Flask(__name__)
 
+def query(operation, *params):
+	if 'db' not in g or not g.db.is_connected():
+		g.db = mysql.connector.connect(user=config.db_user, password=config.db_passwd, host=config.db_host, database=config.db_db)
+	cur = g.db.cursor(dictionary=True)
+	cur.execute(operation, params)
+	return cur.fetchall()
+
 @app.route('/')
 def index():
-	return render_template('index.html', latestvideos=[{'talk':{'title':'talktitle','thumbnail':'https://videoag.fsmpi.rwth-aachen.de/pub/thumbnails/16ss-hyp-160802-Geophysical_models_IV.jpg','id':123,'comment':'comment','date':'date'},'event':{'title':'eventtitle'}},{'talk':{'title':'talktitle','thumbnail':'https://videoag.fsmpi.rwth-aachen.de/pub/thumbnails/16ss-hyp-160802-Geophysical_models_IV.jpg','id':123,'comment':'comment','date':'date'},'event':{'title':'eventtitle'}},{'talk':{'title':'talktitle','thumbnail':'https://videoag.fsmpi.rwth-aachen.de/pub/thumbnails/16ss-hyp-160802-Geophysical_models_IV.jpg','id':123,'comment':'comment','date':'date'},'event':{'title':'eventtitle'}},{'talk':{'title':'talktitle','thumbnail':'https://videoag.fsmpi.rwth-aachen.de/pub/thumbnails/16ss-hyp-160802-Geophysical_models_IV.jpg','id':123,'comment':'comment','date':'date'},'event':{'title':'eventtitle'}}])
+	return render_template('index.html', latestvideos=query('''
+				SELECT lectures.*, max(videos.time_updated) AS lastvidtime, courses.downloadable, courses.title AS coursetitle
+				FROM lectures
+				LEFT JOIN videos ON (videos.lecture_id = lectures.id)
+				LEFT JOIN courses on (courses.id = lectures.course_id)
+				WHERE (videos.time_updated >= SUBDATE(NOW(), 14))
+					AND (%s OR (courses.visible AND courses.listed AND lectures.visible AND videos.visible))
+				GROUP BY videos.lecture_id
+				ORDER BY lastvidtime DESC
+				LIMIT 5
+			''', False))
 
 @app.route('/videos')
 def videos():
diff --git a/templates/index.html b/templates/index.html
index 3b2ec05..55573b4 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -36,7 +36,7 @@
 				<div class="panel-body">
 					<ul class="list-group videopreview">
 						{% for i in latestvideos %}
-							{{ preview(i['talk'],i['event']) }}
+							{{ preview(i) }}
 						{% endfor %}
 					</ul>
 				</div>
diff --git a/templates/macros.html b/templates/macros.html
index c625fa0..6312f7a 100644
--- a/templates/macros.html
+++ b/templates/macros.html
@@ -1,16 +1,18 @@
-{% macro preview(talk,event) %}
+{% macro preview(lecture) %}
 <li class="list-group-item">
 	<div class="row">
-		<a href="?view=player&amp;lectureid={{ talk['id'] }}#content" title="{{ event['title'] }}">
-			<img class="col-xs-4" src="{{ talk['thumbnail'] }}" alt="Vorschaubild">
+		<a href="?view=player&amp;lectureid={{ lecture['id'] }}#content" title="{{ lecture['coursetitle'] }}">
+			<img class="col-xs-4" src="https://videoag.fsmpi.rwth-aachen.de/{{ lecture['titlefile'] }}" alt="Vorschaubild">
 			<div class="col-xs-4">
-				<span>{{ event['title'] }}</span>
-				<span>{{ talk['date'] }}</span>
-				<p>{{ talk['title'] }}</p>
+				<span>{{ lecture['coursetitle'] }}</span>
+				<span>{{ lecture['time'] }}</span>
+				<p>{{ lecture['title'] }}</p>
 			</div>
 			<div class="col-xs-4">
-				<div class="comment">{{ talk['comment']  }}</div>
-				<div class="comment">Gehalten von {{ talk['speaker']|default('unbekannt') }} </div>
+				<div class="comment">{{ lecture['comment']  }}</div>
+				{% if lecture['speaker'] %}
+					<div class="comment">Gehalten von {{ lecture['speaker'] }} </div>
+				{% endif %}
 			</div>
 		</a>
 	</div>
-- 
GitLab