Skip to content
Snippets Groups Projects
Commit b0cd3e89 authored by Julian Rother's avatar Julian Rother
Browse files

Add simple DB connection and adopt templates to use it

parent 2ffe2814
No related branches found
No related tags found
No related merge requests found
#!/bin/python #!/bin/python
from flask import Flask, render_template from flask import Flask, render_template, g
import mysql.connector
import config
app = Flask(__name__) 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('/') @app.route('/')
def index(): 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') @app.route('/videos')
def videos(): def videos():
......
...@@ -36,7 +36,7 @@ ...@@ -36,7 +36,7 @@
<div class="panel-body"> <div class="panel-body">
<ul class="list-group videopreview"> <ul class="list-group videopreview">
{% for i in latestvideos %} {% for i in latestvideos %}
{{ preview(i['talk'],i['event']) }} {{ preview(i) }}
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>
......
{% macro preview(talk,event) %} {% macro preview(lecture) %}
<li class="list-group-item"> <li class="list-group-item">
<div class="row"> <div class="row">
<a href="?view=player&amp;lectureid={{ talk['id'] }}#content" title="{{ event['title'] }}"> <a href="?view=player&amp;lectureid={{ lecture['id'] }}#content" title="{{ lecture['coursetitle'] }}">
<img class="col-xs-4" src="{{ talk['thumbnail'] }}" alt="Vorschaubild"> <img class="col-xs-4" src="https://videoag.fsmpi.rwth-aachen.de/{{ lecture['titlefile'] }}" alt="Vorschaubild">
<div class="col-xs-4"> <div class="col-xs-4">
<span>{{ event['title'] }}</span> <span>{{ lecture['coursetitle'] }}</span>
<span>{{ talk['date'] }}</span> <span>{{ lecture['time'] }}</span>
<p>{{ talk['title'] }}</p> <p>{{ lecture['title'] }}</p>
</div> </div>
<div class="col-xs-4"> <div class="col-xs-4">
<div class="comment">{{ talk['comment'] }}</div> <div class="comment">{{ lecture['comment'] }}</div>
<div class="comment">Gehalten von {{ talk['speaker']|default('unbekannt') }} </div> {% if lecture['speaker'] %}
<div class="comment">Gehalten von {{ lecture['speaker'] }} </div>
{% endif %}
</div> </div>
</a> </a>
</div> </div>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment