From 02db1f39718b9464f2b7916fe85428d32e7eee40 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 26 Feb 2020 17:50:36 -0800 Subject: [PATCH] Make response_type required, explicit in all calls. --- frontends/etiquette_flask/backend/common.py | 5 ++- .../backend/endpoints/album_endpoints.py | 32 +++++++++---------- .../backend/endpoints/photo_endpoints.py | 2 +- .../backend/endpoints/tag_endpoints.py | 14 ++++---- 4 files changed, 28 insertions(+), 25 deletions(-) diff --git a/frontends/etiquette_flask/backend/common.py b/frontends/etiquette_flask/backend/common.py index fd81fc0..cc2661f 100644 --- a/frontends/etiquette_flask/backend/common.py +++ b/frontends/etiquette_flask/backend/common.py @@ -50,7 +50,10 @@ file_cache_manager = caching.FileCacheManager( ) def P_wrapper(function): - def P_wrapped(thingid, response_type='html'): + def P_wrapped(thingid, response_type): + if response_type not in {'html', 'json'}: + raise TypeError(f'response_type should be html or json, not {response_type}.') + try: return function(thingid) diff --git a/frontends/etiquette_flask/backend/endpoints/album_endpoints.py b/frontends/etiquette_flask/backend/endpoints/album_endpoints.py index 078bb42..c964eb8 100644 --- a/frontends/etiquette_flask/backend/endpoints/album_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/album_endpoints.py @@ -16,7 +16,7 @@ session_manager = common.session_manager @site.route('/album/') @session_manager.give_token def get_album_html(album_id): - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='html') response = common.render_template( request, 'album.html', @@ -28,13 +28,13 @@ def get_album_html(album_id): @site.route('/album/.json') @session_manager.give_token def get_album_json(album_id): - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='json') album = etiquette.jsonify.album(album) return jsonify.make_json_response(album) @site.route('/album/.zip') def get_album_zip(album_id): - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='html') recursive = request.args.get('recursive', True) recursive = etiquette.helpers.truthystring(recursive) @@ -59,8 +59,8 @@ def get_album_zip(album_id): @decorators.catch_etiquette_exception @decorators.required_fields(['child_id'], forbid_whitespace=True) def post_album_add_child(album_id): - album = common.P_album(album_id) - child = common.P_album(request.form['child_id']) + album = common.P_album(album_id, response_type='json') + child = common.P_album(request.form['child_id'], response_type='json') album.add_child(child, commit=True) response = etiquette.jsonify.album(child) return jsonify.make_json_response(response) @@ -69,8 +69,8 @@ def post_album_add_child(album_id): @decorators.catch_etiquette_exception @decorators.required_fields(['child_id'], forbid_whitespace=True) def post_album_remove_child(album_id): - album = common.P_album(album_id) - child = common.P_album(request.form['child_id']) + album = common.P_album(album_id, response_type='json') + child = common.P_album(request.form['child_id'], response_type='json') album.remove_child(child, commit=True) response = etiquette.jsonify.album(child) return jsonify.make_json_response(response) @@ -78,7 +78,7 @@ def post_album_remove_child(album_id): @site.route('/album//refresh_directories', methods=['POST']) @decorators.catch_etiquette_exception def post_album_refresh_directories(album_id): - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='json') for directory in album.get_associated_directories(): common.P.digest_directory(directory, new_photo_ratelimit=0.1) common.P.commit(message='refresh album directories endpoint') @@ -95,10 +95,10 @@ def post_album_add_photo(album_id): Add a photo or photos to this album. ''' response = {} - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='json') photo_ids = etiquette.helpers.comma_space_split(request.form['photo_id']) - photos = list(common.P_photos(photo_ids)) + photos = list(common.P_photos(photo_ids, response_type='json')) album.add_photos(photos, commit=True) return jsonify.make_json_response(response) @@ -111,10 +111,10 @@ def post_album_remove_photo(album_id): Remove a photo or photos from this album. ''' response = {} - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='json') photo_ids = etiquette.helpers.comma_space_split(request.form['photo_id']) - photos = list(common.P_photos(photo_ids)) + photos = list(common.P_photos(photo_ids, response_type='json')) album.remove_photos(photos, commit=True) return jsonify.make_json_response(response) @@ -128,11 +128,11 @@ def post_album_add_tag(album_id): Apply a tag to every photo in the album. ''' response = {} - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='json') tag = request.form['tagname'].strip() try: - tag = common.P_tag(tag) + tag = common.P_tag(tag, response_type='json') except etiquette.exceptions.NoSuchTag as exc: response = etiquette.jsonify.exception(exc) return jsonify.make_json_response(response, status=404) @@ -152,7 +152,7 @@ def post_album_edit(album_id): ''' Edit the title / description. ''' - album = common.P_album(album_id) + album = common.P_album(album_id, response_type='json') title = request.form.get('title', None) description = request.form.get('description', None) @@ -196,7 +196,7 @@ def post_albums_create(): description = request.form.get('description', None) parent_id = request.form.get('parent_id', None) if parent_id is not None: - parent = common.P_album(parent_id) + parent = common.P_album(parent_id, response_type='json') user = session_manager.get(request).user diff --git a/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py b/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py index 19e057b..3d921d2 100644 --- a/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py @@ -61,7 +61,7 @@ def get_file(photo_id, basename=None): @site.route('/thumbnail/') def get_thumbnail(photo_id): photo_id = photo_id.split('.')[0] - photo = common.P_photo(photo_id) + photo = common.P_photo(photo_id, response_type='html') if photo.thumbnail: path = photo.thumbnail else: diff --git a/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py b/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py index 39aa6a3..a790d12 100644 --- a/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/tag_endpoints.py @@ -33,7 +33,7 @@ def get_tag_id_redirect(tag_id): @site.route('/tag//edit', methods=['POST']) @decorators.catch_etiquette_exception def post_tag_edit(specific_tag): - tag = common.P_tag(specific_tag) + tag = common.P_tag(specific_tag, response_type='json') name = request.form.get('name', '').strip() if name: tag.rename(name) @@ -50,8 +50,8 @@ def post_tag_edit(specific_tag): @session_manager.give_token @decorators.required_fields(['child_name'], forbid_whitespace=True) def post_tag_add_child(tagname): - parent = common.P_tag(tagname) - child = common.P_tag(request.form['child_name']) + parent = common.P_tag(tagname, response_type='json') + child = common.P_tag(request.form['child_name'], response_type='json') parent.add_child(child, commit=True) response = {'action': 'add_child', 'tagname': f'{parent.name}.{child.name}'} return jsonify.make_json_response(response) @@ -61,8 +61,8 @@ def post_tag_add_child(tagname): @session_manager.give_token @decorators.required_fields(['child_name'], forbid_whitespace=True) def post_tag_remove_child(tagname): - parent = common.P_tag(tagname) - child = common.P_tag(request.form['child_name']) + parent = common.P_tag(tagname, response_type='json') + child = common.P_tag(request.form['child_name'], response_type='json') parent.remove_child(child, commit=True) response = {'action': 'remove_child', 'tagname': f'{parent.name}.{child.name}'} return jsonify.make_json_response(response) @@ -159,7 +159,7 @@ def post_tag_easybake(): @decorators.catch_etiquette_exception @session_manager.give_token def post_tag_delete(tagname): - tag = common.P_tag(tagname) + tag = common.P_tag(tagname, response_type='json') tag.delete(commit=True) response = {'action': 'delete_tag', 'tagname': tag.name} return jsonify.make_json_response(response) @@ -171,7 +171,7 @@ def post_tag_delete(tagname): def post_tag_remove_synonym(tagname): syn_name = request.form['syn_name'] - master_tag = common.P_tag(tagname) + master_tag = common.P_tag(tagname, response_type='json') master_tag.remove_synonym(syn_name, commit=True) response = {'action': 'delete_synonym', 'synonym': syn_name}