From d7307a6c44deb74a9b877d4f68aae33aef7a982b Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 14 Aug 2019 13:40:52 -0700 Subject: [PATCH] Centralize flask.render_template calls to simplify common args. --- frontends/etiquette_flask/backend/common.py | 9 +++++++++ .../backend/endpoints/album_endpoints.py | 10 ++++------ .../backend/endpoints/basic_endpoints.py | 3 +-- .../backend/endpoints/bookmark_endpoints.py | 3 +-- .../backend/endpoints/photo_endpoints.py | 11 ++++------- .../backend/endpoints/tag_endpoints.py | 5 ++--- .../backend/endpoints/user_endpoints.py | 8 +++----- 7 files changed, 24 insertions(+), 25 deletions(-) diff --git a/frontends/etiquette_flask/backend/common.py b/frontends/etiquette_flask/backend/common.py index 13dfb84..cc0f8b8 100644 --- a/frontends/etiquette_flask/backend/common.py +++ b/frontends/etiquette_flask/backend/common.py @@ -108,6 +108,15 @@ def P_user(username): def P_user_id(user_id): return P.get_user(id=user_id) +def render_template(request, *args, **kwargs): + session = session_manager.get(request) + ret = flask.render_template( + session=session, + theme=request.args.get('theme'), + *args, + **kwargs, + ) + return ret def back_url(): return request.args.get('goto') or request.referrer or '/' diff --git a/frontends/etiquette_flask/backend/endpoints/album_endpoints.py b/frontends/etiquette_flask/backend/endpoints/album_endpoints.py index 3d0a9da..8954487 100644 --- a/frontends/etiquette_flask/backend/endpoints/album_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/album_endpoints.py @@ -19,11 +19,10 @@ session_manager = common.session_manager @session_manager.give_token def get_album_html(album_id): album = common.P_album(album_id) - session = session_manager.get(request) - response = flask.render_template( + response = common.render_template( + request, 'album.html', album=album, - session=session, view=request.args.get('view', 'grid'), ) return response @@ -174,11 +173,10 @@ def get_albums_core(): @session_manager.give_token def get_albums_html(): albums = get_albums_core() - session = session_manager.get(request) - response = flask.render_template( + response = common.render_template( + request, 'album.html', albums=albums, - session=session, view=request.args.get('view', 'grid'), ) return response diff --git a/frontends/etiquette_flask/backend/endpoints/basic_endpoints.py b/frontends/etiquette_flask/backend/endpoints/basic_endpoints.py index 9242f2d..5eff51c 100644 --- a/frontends/etiquette_flask/backend/endpoints/basic_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/basic_endpoints.py @@ -13,8 +13,7 @@ session_manager = common.session_manager @session_manager.give_token def root(): motd = random.choice(common.P.config['motd_strings']) - session = session_manager.get(request) - return flask.render_template('root.html', motd=motd, session=session) + return common.render_template(request, 'root.html', motd=motd) @site.route('/favicon.ico') @site.route('/favicon.png') diff --git a/frontends/etiquette_flask/backend/endpoints/bookmark_endpoints.py b/frontends/etiquette_flask/backend/endpoints/bookmark_endpoints.py index 87176b2..bfab2a9 100644 --- a/frontends/etiquette_flask/backend/endpoints/bookmark_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/bookmark_endpoints.py @@ -38,9 +38,8 @@ def post_bookmark_edit(bookmark_id): @site.route('/bookmarks') @session_manager.give_token def get_bookmarks_html(): - session = session_manager.get(request) bookmarks = list(common.P.get_bookmarks()) - return flask.render_template('bookmarks.html', bookmarks=bookmarks, session=session) + return common.render_template(request, 'bookmarks.html', bookmarks=bookmarks) @site.route('/bookmarks.json') @session_manager.give_token diff --git a/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py b/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py index c455003..adaa678 100644 --- a/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py @@ -23,8 +23,7 @@ photo_download_zip_tokens = cacheclass.Cache(maxlen=100) @session_manager.give_token def get_photo_html(photo_id): photo = common.P_photo(photo_id, response_type='html') - session = session_manager.get(request) - return flask.render_template('photo.html', photo=photo, session=session) + return common.render_template(request, 'photo.html', photo=photo) @site.route('/photo/.json') @session_manager.give_token @@ -201,8 +200,7 @@ def post_batch_photos_unset_searchhidden(): @site.route('/clipboard') @session_manager.give_token def get_clipboard_page(): - session = session_manager.get(request) - return flask.render_template('clipboard.html', session=session) + return common.render_template(request, 'clipboard.html') @site.route('/batch/photos/photo_card', methods=['POST']) @decorators.required_fields(['photo_ids'], forbid_whitespace=True) @@ -428,14 +426,13 @@ def get_search_core(): def get_search_html(): search_results = get_search_core() search_kwargs = search_results['search_kwargs'] - session = session_manager.get(request) - response = flask.render_template( + response = common.render_template( + request, 'search.html', next_page_url=search_results['next_page_url'], prev_page_url=search_results['prev_page_url'], photos=search_results['photos'], search_kwargs=search_kwargs, - session=session, total_tags=search_results['total_tags'], warnings=search_results['warnings'], ) diff --git a/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py b/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py index d92f4cc..692392c 100644 --- a/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py @@ -91,7 +91,6 @@ def get_tags_html(specific_tag_name=None): response = flask.redirect(new_url) return response - session = session_manager.get(request) include_synonyms = request.args.get('synonyms') include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms) @@ -101,10 +100,10 @@ def get_tags_html(specific_tag_name=None): tags = [specific_tag] tags = etiquette.tag_export.easybake(tags, include_synonyms=False, with_objects=True) - response = flask.render_template( + response = common.render_template( + request, 'tags.html', include_synonyms=include_synonyms, - session=session, specific_tag=specific_tag, tags=tags, ) diff --git a/frontends/etiquette_flask/backend/endpoints/user_endpoints.py b/frontends/etiquette_flask/backend/endpoints/user_endpoints.py index 5a362d7..d7e3925 100644 --- a/frontends/etiquette_flask/backend/endpoints/user_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/user_endpoints.py @@ -17,8 +17,7 @@ session_manager = common.session_manager @session_manager.give_token def get_user_html(username): user = common.P_user(username, response_type='html') - session = session_manager.get(request) - return flask.render_template('user.html', user=user, session=session) + return common.render_template(request, 'user.html', user=user) @site.route('/user/.json') @session_manager.give_token @@ -44,10 +43,9 @@ def get_user_id_redirect(user_id): @site.route('/login', methods=['GET']) @session_manager.give_token def get_login(): - session = session_manager.get(request) - response = flask.render_template( + response = common.render_template( + request, 'login.html', - session=session, min_username_length=common.P.config['user']['min_username_length'], min_password_length=common.P.config['user']['min_password_length'], )