From ef668c5d3b974e89a8ac65e9d64b334d5199a79b Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Sun, 25 Jun 2023 00:02:20 -0700 Subject: [PATCH] Add endpoint /batch/photos/generate_thumbnail. --- .../backend/endpoints/photo_endpoints.py | 31 +++++++++++--- frontends/etiquette_flask/static/js/api.js | 14 ++++++- .../etiquette_flask/templates/clipboard.html | 40 +++++++++++++++++++ 3 files changed, 77 insertions(+), 8 deletions(-) diff --git a/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py b/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py index fcf9db3..e47ceea 100644 --- a/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py +++ b/frontends/etiquette_flask/backend/endpoints/photo_endpoints.py @@ -67,7 +67,7 @@ def get_file(photo_id, basename=None): @site.route('/photo//thumbnail') @site.route('/photo//thumbnail/') @common.permission_manager.basic_decorator -@flasktools.cached_endpoint(max_age=common.BROWSER_CACHE_DURATION) +@flasktools.cached_endpoint(max_age=common.BROWSER_CACHE_DURATION, etag_function=lambda: common.P.last_commit_id) def get_thumbnail(photo_id, basename=None): photo_id = photo_id.split('.')[0] photo = common.P_photo(photo_id, response_type='html') @@ -186,16 +186,35 @@ def post_batch_photos_remove_tag(): # Photo metadata operations ######################################################################## +def post_photo_generate_thumbnail_core(photo_ids, special={}): + if isinstance(photo_ids, str): + photo_ids = stringtools.comma_space_split(photo_ids) + + with common.P.transaction: + photos = list(common.P_photos(photo_ids, response_type='json')) + + for photo in photos: + photo._uncache() + photo = common.P_photo(photo.id, response_type='json') + try: + photo.generate_thumbnail() + except Exception: + log.warning(traceback.format_exc()) + + return flasktools.json_response({}) + @site.route('/photo//generate_thumbnail', methods=['POST']) def post_photo_generate_thumbnail(photo_id): common.permission_manager.basic() special = request.form.to_dict() + response = post_photo_generate_thumbnail_core(photo_ids=photo_id, special=special) + return response - with common.P.transaction: - photo = common.P_photo(photo_id, response_type='json') - photo.generate_thumbnail(**special) - - response = flasktools.json_response({}) +@site.route('/batch/photos/generate_thumbnail', methods=['POST']) +def post_batch_photos_generate_thumbnail(): + common.permission_manager.basic() + special = request.form.to_dict() + response = post_photo_generate_thumbnail_core(photo_ids=request.form['photo_ids'], special=special) return response def post_photo_refresh_metadata_core(photo_ids): diff --git a/frontends/etiquette_flask/static/js/api.js b/frontends/etiquette_flask/static/js/api.js index e70eb72..bcf980b 100644 --- a/frontends/etiquette_flask/static/js/api.js +++ b/frontends/etiquette_flask/static/js/api.js @@ -236,7 +236,17 @@ function batch_add_tag(photo_ids, tagname, callback) return http.post({ url: "/batch/photos/add_tag", data: {"photo_ids": photo_ids.join(","), "tagname": tagname}, - add_remove_tag_callback: callback, + callback: callback, + }); +} + +api.photos.batch_generate_thumbnail = +function batch_generate_thumbnail(photo_ids, callback) +{ + return http.post({ + url: "/batch/photos/generate_thumbnail", + data: {"photo_ids": photo_ids.join(",")}, + callback: callback, }); } @@ -256,7 +266,7 @@ function batch_remove_tag(photo_ids, tagname, callback) return http.post({ url: "/batch/photos/remove_tag", data: {"photo_ids": photo_ids.join(","), "tagname": tagname}, - add_remove_tag_callback: callback, + callback: callback, }); } diff --git a/frontends/etiquette_flask/templates/clipboard.html b/frontends/etiquette_flask/templates/clipboard.html index a1f0def..ab27f02 100644 --- a/frontends/etiquette_flask/templates/clipboard.html +++ b/frontends/etiquette_flask/templates/clipboard.html @@ -122,6 +122,10 @@ +
+ +
+
@@ -369,6 +373,42 @@ function refresh_metadata_form() //////////////////////////////////////////////////////////////////////////////// +const generate_thumbnail_button = document.getElementById("generate_thumbnail_button"); + +function generate_thumbnail_callback(response) +{ + window[generate_thumbnail_button.dataset.spinnerCloser](); + if (! response.meta.json_ok) + { + alert(JSON.stringify(response)); + return; + } + if ("error_type" in response.data) + { + const message_area = document.getElementById("message_area"); + const message_positivity = "message_negative"; + const message_text = response.data.error_message; + common.create_message_bubble(message_area, message_positivity, message_text, 8000); + } + else + { + common.refresh(); + } +} + +function generate_thumbnail_form() +{ + if (photo_clipboard.clipboard.size == 0) + { + return spinners.BAIL; + } + + const photo_ids = Array.from(photo_clipboard.clipboard); + api.photos.batch_generate_thumbnail(photo_ids, generate_thumbnail_callback); +} + +//////////////////////////////////////////////////////////////////////////////// + function set_unset_searchhidden_callback(response) { if (! response.meta.json_ok)