diff --git a/server.py b/server.py index ed188b7b0363e30f556bc378c990bb53d5517b5d..cdb4a8968673d74de3c62a32b1103f8e0bad7c4f 100755 --- a/server.py +++ b/server.py @@ -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())