Commit d5189334 authored by Julian Rother's avatar Julian Rother
Browse files

Introduce handle_errors decorator

parent 613902c6
......@@ -67,9 +67,20 @@ def render_endpoint(endpoint, flashtext=None, **kargs):
request.url_rule = Rule(request.path, endpoint=endpoint)
return app.view_functions[endpoint](**kargs)
def handle_errors(endpoint, text, code, *errors, **epargs):
def wrapper(func):
@wraps(func)
def decorator(*args, **kwargs):
try:
return func(*args, **kwargs)
except errors:
return render_endpoint(endpoint, text, **epargs), code
return decorator
return wrapper
@app.errorhandler(404)
def handle_not_found(e):
return render_endpoint('index', 'Diese Seite existiert nicht!')
return render_endpoint('index', 'Diese Seite existiert nicht!'), 404
@app.route('/')
@register_navbar('Home', icon='home')
......@@ -99,13 +110,12 @@ def videos():
@app.route('/course/<id>')
@app.route('/course/<int:numid>')
@handle_errors('videos', 'Diese Veranstaltung existiert nicht!', 404, IndexError)
def course(numid=None, id=None):
if numid:
courses = query('SELECT * FROM courses WHERE id = ? AND (? OR visible)', numid, ismod())
else:
courses = query('SELECT * FROM courses WHERE handle = ? AND (? OR visible)', id, ismod())
if not courses:
return render_endpoint('index', 'Diese Veranstaltung existiert nicht!'), 404
lectures = query('SELECT * FROM lectures WHERE course_id = ? AND (? OR visible)', courses[0]['id'], ismod())
videos = query('''
SELECT videos.*, (videos.downloadable AND courses.downloadable) as downloadable, formats.description AS format_description
......@@ -124,11 +134,10 @@ def faq():
return render_template('faq.html')
@app.route('/play/<int:id>')
@handle_errors('videos', 'Diese Vorlesung existiert nicht!', 404, IndexError)
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:
return render_endpoint('videos', 'Diese Vorlesung existiert nicht!'), 404
if not videos:
flash('Zu dieser Vorlesung wurden noch keine Videos veröffentlicht!')
courses = query('SELECT * FROM courses WHERE id = ? AND (? OR (visible AND listed))', lectures[0]['course_id'], ismod())
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment