2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
2021-02-26 02:57:41 +00:00
|
|
|
import os
|
2022-01-11 09:00:36 +00:00
|
|
|
import subprocess
|
2018-01-12 03:40:56 +00:00
|
|
|
import traceback
|
|
|
|
import urllib.parse
|
|
|
|
|
2018-08-15 06:02:06 +00:00
|
|
|
from voussoirkit import cacheclass
|
2021-06-05 04:49:45 +00:00
|
|
|
from voussoirkit import flasktools
|
2021-12-22 00:55:03 +00:00
|
|
|
from voussoirkit import pathclass
|
2020-11-16 06:18:40 +00:00
|
|
|
from voussoirkit import stringtools
|
2021-12-22 00:55:46 +00:00
|
|
|
from voussoirkit import vlogging
|
|
|
|
|
|
|
|
log = vlogging.get_logger(__name__)
|
2018-08-15 06:02:06 +00:00
|
|
|
|
2018-11-05 03:27:20 +00:00
|
|
|
import etiquette
|
|
|
|
|
2018-07-19 01:37:21 +00:00
|
|
|
from .. import common
|
2018-11-04 21:30:08 +00:00
|
|
|
from .. import helpers
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
site = common.site
|
|
|
|
session_manager = common.session_manager
|
2018-08-15 06:02:06 +00:00
|
|
|
photo_download_zip_tokens = cacheclass.Cache(maxlen=100)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
# Individual photos ################################################################################
|
|
|
|
|
2018-02-17 22:59:02 +00:00
|
|
|
@site.route('/photo/<photo_id>')
|
2018-01-12 03:40:56 +00:00
|
|
|
def get_photo_html(photo_id):
|
|
|
|
photo = common.P_photo(photo_id, response_type='html')
|
2019-08-14 20:40:52 +00:00
|
|
|
return common.render_template(request, 'photo.html', photo=photo)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-02-17 22:59:02 +00:00
|
|
|
@site.route('/photo/<photo_id>.json')
|
2018-01-12 03:40:56 +00:00
|
|
|
def get_photo_json(photo_id):
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
2021-01-01 20:56:05 +00:00
|
|
|
photo = photo.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
photo = flasktools.json_response(photo)
|
2018-01-12 03:40:56 +00:00
|
|
|
return photo
|
|
|
|
|
|
|
|
@site.route('/file/<photo_id>')
|
2018-04-20 02:23:10 +00:00
|
|
|
@site.route('/file/<photo_id>/<basename>')
|
|
|
|
def get_file(photo_id, basename=None):
|
2018-01-12 03:40:56 +00:00
|
|
|
photo_id = photo_id.split('.')[0]
|
|
|
|
photo = common.P.get_photo(photo_id)
|
|
|
|
|
|
|
|
do_download = request.args.get('download', False)
|
2021-09-05 08:17:05 +00:00
|
|
|
do_download = stringtools.truthystring(do_download)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
use_original_filename = request.args.get('original_filename', False)
|
2021-09-05 08:17:05 +00:00
|
|
|
use_original_filename = stringtools.truthystring(use_original_filename)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
if do_download:
|
|
|
|
if use_original_filename:
|
|
|
|
download_as = photo.basename
|
|
|
|
else:
|
|
|
|
download_as = photo.id + photo.dot_extension
|
|
|
|
|
|
|
|
download_as = etiquette.helpers.remove_path_badchars(download_as)
|
|
|
|
download_as = urllib.parse.quote(download_as)
|
2018-02-17 23:19:36 +00:00
|
|
|
response = flask.make_response(common.send_file(photo.real_path.absolute_path))
|
2018-01-12 03:40:56 +00:00
|
|
|
response.headers['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'%s' % download_as
|
|
|
|
return response
|
|
|
|
else:
|
2018-02-17 23:19:36 +00:00
|
|
|
return common.send_file(photo.real_path.absolute_path, override_mimetype=photo.mimetype)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/thumbnail/<photo_id>')
|
|
|
|
def get_thumbnail(photo_id):
|
|
|
|
photo_id = photo_id.split('.')[0]
|
2020-02-27 01:50:36 +00:00
|
|
|
photo = common.P_photo(photo_id, response_type='html')
|
2018-01-12 03:40:56 +00:00
|
|
|
if photo.thumbnail:
|
|
|
|
path = photo.thumbnail
|
|
|
|
else:
|
|
|
|
flask.abort(404, 'That file doesnt have a thumbnail')
|
|
|
|
return common.send_file(path)
|
|
|
|
|
2020-02-27 02:57:29 +00:00
|
|
|
# Photo create and delete ##########################################################################
|
|
|
|
|
|
|
|
@site.route('/photo/<photo_id>/delete', methods=['POST'])
|
|
|
|
def post_photo_delete(photo_id):
|
|
|
|
delete_file = request.form.get('delete_file', False)
|
2021-09-05 08:17:05 +00:00
|
|
|
delete_file = stringtools.truthystring(delete_file)
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
|
|
|
photo.delete(delete_file=delete_file)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2020-02-27 02:57:29 +00:00
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# Photo tag operations #############################################################################
|
|
|
|
|
2018-02-21 01:23:51 +00:00
|
|
|
def post_photo_add_remove_tag_core(photo_ids, tagname, add_or_remove):
|
|
|
|
if isinstance(photo_ids, str):
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(photo_ids)
|
2018-02-21 01:23:51 +00:00
|
|
|
|
2018-05-02 01:09:35 +00:00
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
2018-01-12 03:40:56 +00:00
|
|
|
tag = common.P_tag(tagname, response_type='json')
|
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
for photo in photos:
|
|
|
|
if add_or_remove == 'add':
|
|
|
|
photo.add_tag(tag)
|
|
|
|
elif add_or_remove == 'remove':
|
|
|
|
photo.remove_tag(tag)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-02-22 23:23:57 +00:00
|
|
|
response = {'action': add_or_remove, 'tagname': tag.name}
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/photo/<photo_id>/add_tag', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['tagname'], forbid_whitespace=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
def post_photo_add_tag(photo_id):
|
|
|
|
'''
|
|
|
|
Add a tag to this photo.
|
|
|
|
'''
|
2018-02-21 01:23:51 +00:00
|
|
|
response = post_photo_add_remove_tag_core(
|
|
|
|
photo_ids=photo_id,
|
|
|
|
tagname=request.form['tagname'],
|
|
|
|
add_or_remove='add',
|
|
|
|
)
|
|
|
|
return response
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2021-04-29 20:12:37 +00:00
|
|
|
@site.route('/photo/<photo_id>/copy_tags', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['other_photo'], forbid_whitespace=True)
|
2021-04-29 20:12:37 +00:00
|
|
|
def post_photo_copy_tags(photo_id):
|
|
|
|
'''
|
|
|
|
Copy the tags from another photo.
|
|
|
|
'''
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
|
|
|
other = common.P_photo(request.form['other_photo'], response_type='json')
|
|
|
|
photo.copy_tags(other)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response([tag.jsonify(minimal=True) for tag in photo.get_tags()])
|
2021-04-29 20:12:37 +00:00
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
@site.route('/photo/<photo_id>/remove_tag', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['tagname'], forbid_whitespace=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
def post_photo_remove_tag(photo_id):
|
|
|
|
'''
|
|
|
|
Remove a tag from this photo.
|
|
|
|
'''
|
2018-02-21 01:23:51 +00:00
|
|
|
response = post_photo_add_remove_tag_core(
|
|
|
|
photo_ids=photo_id,
|
|
|
|
tagname=request.form['tagname'],
|
|
|
|
add_or_remove='remove',
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/batch/photos/add_tag', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids', 'tagname'], forbid_whitespace=True)
|
2018-02-21 01:23:51 +00:00
|
|
|
def post_batch_photos_add_tag():
|
|
|
|
response = post_photo_add_remove_tag_core(
|
|
|
|
photo_ids=request.form['photo_ids'],
|
|
|
|
tagname=request.form['tagname'],
|
|
|
|
add_or_remove='add',
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/batch/photos/remove_tag', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids', 'tagname'], forbid_whitespace=True)
|
2018-02-21 01:23:51 +00:00
|
|
|
def post_batch_photos_remove_tag():
|
|
|
|
response = post_photo_add_remove_tag_core(
|
|
|
|
photo_ids=request.form['photo_ids'],
|
|
|
|
tagname=request.form['tagname'],
|
|
|
|
add_or_remove='remove',
|
|
|
|
)
|
|
|
|
return response
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
# Photo metadata operations ########################################################################
|
|
|
|
|
2020-01-12 22:52:03 +00:00
|
|
|
@site.route('/photo/<photo_id>/generate_thumbnail', methods=['POST'])
|
|
|
|
def post_photo_generate_thumbnail(photo_id):
|
|
|
|
special = request.form.to_dict()
|
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
|
|
|
photo.generate_thumbnail(**special)
|
2020-01-12 22:52:03 +00:00
|
|
|
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response({})
|
2020-01-12 22:52:03 +00:00
|
|
|
return response
|
|
|
|
|
2018-02-24 20:52:36 +00:00
|
|
|
def post_photo_refresh_metadata_core(photo_ids):
|
|
|
|
if isinstance(photo_ids, str):
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(photo_ids)
|
2018-02-24 20:52:36 +00:00
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
2018-02-24 20:52:36 +00:00
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
for photo in photos:
|
|
|
|
photo._uncache()
|
|
|
|
photo = common.P_photo(photo.id, response_type='json')
|
2018-02-24 20:52:36 +00:00
|
|
|
try:
|
2022-07-16 06:00:07 +00:00
|
|
|
photo.reload_metadata()
|
|
|
|
except pathclass.NotFile:
|
|
|
|
flask.abort(404)
|
|
|
|
if photo.thumbnail is None:
|
|
|
|
try:
|
|
|
|
photo.generate_thumbnail()
|
|
|
|
except Exception:
|
|
|
|
log.warning(traceback.format_exc())
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-02-24 20:52:36 +00:00
|
|
|
@site.route('/photo/<photo_id>/refresh_metadata', methods=['POST'])
|
|
|
|
def post_photo_refresh_metadata(photo_id):
|
|
|
|
response = post_photo_refresh_metadata_core(photo_ids=photo_id)
|
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/batch/photos/refresh_metadata', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids'], forbid_whitespace=True)
|
2018-02-24 20:52:36 +00:00
|
|
|
def post_batch_photos_refresh_metadata():
|
|
|
|
response = post_photo_refresh_metadata_core(photo_ids=request.form['photo_ids'])
|
|
|
|
return response
|
|
|
|
|
2020-09-10 03:51:15 +00:00
|
|
|
@site.route('/photo/<photo_id>/set_searchhidden', methods=['POST'])
|
|
|
|
def post_photo_set_searchhidden(photo_id):
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
|
|
|
photo.set_searchhidden(True)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2020-09-10 03:51:15 +00:00
|
|
|
|
|
|
|
@site.route('/photo/<photo_id>/unset_searchhidden', methods=['POST'])
|
|
|
|
def post_photo_unset_searchhidden(photo_id):
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
|
|
|
photo.set_searchhidden(False)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2020-09-10 03:51:15 +00:00
|
|
|
|
2020-09-10 02:27:22 +00:00
|
|
|
def post_batch_photos_searchhidden_core(photo_ids, searchhidden):
|
2018-03-10 01:10:27 +00:00
|
|
|
if isinstance(photo_ids, str):
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(photo_ids)
|
2018-03-10 01:10:27 +00:00
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
2018-03-10 01:10:27 +00:00
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
for photo in photos:
|
|
|
|
photo.set_searchhidden(searchhidden)
|
2018-03-10 01:10:27 +00:00
|
|
|
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2018-03-10 01:10:27 +00:00
|
|
|
|
2021-02-26 02:57:41 +00:00
|
|
|
@site.route('/photo/<photo_id>/show_in_folder', methods=['POST'])
|
|
|
|
def post_photo_show_in_folder(photo_id):
|
|
|
|
if not request.is_localhost:
|
|
|
|
flask.abort(403)
|
|
|
|
|
|
|
|
photo = common.P_photo(photo_id, response_type='json')
|
|
|
|
if os.name == 'nt':
|
2022-01-11 09:00:36 +00:00
|
|
|
command = f'explorer.exe /select,"{photo.real_path.absolute_path}"'
|
|
|
|
subprocess.Popen(command, shell=True)
|
|
|
|
return flasktools.json_response({})
|
|
|
|
else:
|
|
|
|
command = ['xdg-open', photo.real_path.parent.absolute_path]
|
|
|
|
subprocess.Popen(command, shell=True)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2021-02-26 02:57:41 +00:00
|
|
|
|
|
|
|
flask.abort(501)
|
|
|
|
|
2018-03-10 01:10:27 +00:00
|
|
|
@site.route('/batch/photos/set_searchhidden', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids'], forbid_whitespace=True)
|
2018-03-10 01:10:27 +00:00
|
|
|
def post_batch_photos_set_searchhidden():
|
2020-09-16 02:47:07 +00:00
|
|
|
photo_ids = request.form['photo_ids']
|
|
|
|
response = post_batch_photos_searchhidden_core(photo_ids=photo_ids, searchhidden=True)
|
2018-03-10 01:10:27 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/batch/photos/unset_searchhidden', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids'], forbid_whitespace=True)
|
2018-03-10 01:10:27 +00:00
|
|
|
def post_batch_photos_unset_searchhidden():
|
2020-09-16 02:47:07 +00:00
|
|
|
photo_ids = request.form['photo_ids']
|
|
|
|
response = post_batch_photos_searchhidden_core(photo_ids=photo_ids, searchhidden=False)
|
2018-03-10 01:10:27 +00:00
|
|
|
return response
|
|
|
|
|
2018-02-18 03:12:34 +00:00
|
|
|
# Clipboard ########################################################################################
|
|
|
|
|
|
|
|
@site.route('/clipboard')
|
|
|
|
def get_clipboard_page():
|
2019-08-14 20:40:52 +00:00
|
|
|
return common.render_template(request, 'clipboard.html')
|
2018-02-18 03:12:34 +00:00
|
|
|
|
2021-05-29 15:46:25 +00:00
|
|
|
@site.route('/batch/photos', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids'], forbid_whitespace=True)
|
2021-05-29 15:46:25 +00:00
|
|
|
def post_batch_photos():
|
|
|
|
'''
|
|
|
|
Return a list of photo.jsonify() for each requested photo id.
|
|
|
|
'''
|
|
|
|
photo_ids = request.form['photo_ids']
|
|
|
|
|
|
|
|
photo_ids = stringtools.comma_space_split(photo_ids)
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
|
|
|
|
|
|
|
photos = [photo.jsonify() for photo in photos]
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response(photos)
|
2021-05-29 15:46:25 +00:00
|
|
|
return response
|
|
|
|
|
2018-02-20 07:44:24 +00:00
|
|
|
@site.route('/batch/photos/photo_card', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids'], forbid_whitespace=True)
|
2018-02-20 07:44:24 +00:00
|
|
|
def post_batch_photos_photo_cards():
|
2018-02-21 01:23:51 +00:00
|
|
|
photo_ids = request.form['photo_ids']
|
2018-02-18 03:12:34 +00:00
|
|
|
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(photo_ids)
|
2018-05-02 01:09:35 +00:00
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
2018-02-18 03:12:34 +00:00
|
|
|
|
|
|
|
# Photo filenames are prevented from having colons, so using it as a split
|
|
|
|
# delimiter should be safe.
|
|
|
|
template = '''
|
2021-01-29 01:01:00 +00:00
|
|
|
{% import "cards.html" as cards %}
|
2018-02-18 03:12:34 +00:00
|
|
|
{% for photo in photos %}
|
|
|
|
{{photo.id}}:
|
2021-01-29 01:01:00 +00:00
|
|
|
{{cards.create_photo_card(photo)}}
|
2018-02-18 03:12:34 +00:00
|
|
|
:SPLITME:
|
|
|
|
{% endfor %}
|
|
|
|
'''
|
|
|
|
html = flask.render_template_string(template, photos=photos)
|
|
|
|
divs = [div.strip() for div in html.split(':SPLITME:')]
|
|
|
|
divs = [div for div in divs if div]
|
|
|
|
divs = [div.split(':', 1) for div in divs]
|
|
|
|
divs = {photo_id.strip(): photo_card.strip() for (photo_id, photo_card) in divs}
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response(divs)
|
2018-02-18 03:12:34 +00:00
|
|
|
return response
|
|
|
|
|
2018-08-15 06:02:06 +00:00
|
|
|
# Zipping ##########################################################################################
|
|
|
|
|
|
|
|
@site.route('/batch/photos/download_zip/<zip_token>', methods=['GET'])
|
|
|
|
def get_batch_photos_download_zip(zip_token):
|
|
|
|
'''
|
|
|
|
After the user has generated their zip token, they can retrieve
|
|
|
|
that zip file.
|
|
|
|
'''
|
|
|
|
zip_token = zip_token.split('.')[0]
|
|
|
|
try:
|
|
|
|
photo_ids = photo_download_zip_tokens[zip_token]
|
|
|
|
except KeyError:
|
|
|
|
flask.abort(404)
|
|
|
|
|
|
|
|
# Let's re-validate those IDs just in case anything has changed.
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
|
|
|
if not photos:
|
|
|
|
flask.abort(400)
|
|
|
|
|
|
|
|
streamed_zip = etiquette.helpers.zip_photos(photos)
|
|
|
|
download_as = zip_token + '.zip'
|
|
|
|
download_as = urllib.parse.quote(download_as)
|
|
|
|
|
|
|
|
outgoing_headers = {
|
|
|
|
'Content-Type': 'application/octet-stream',
|
|
|
|
'Content-Disposition': f'attachment; filename*=UTF-8\'\'{download_as}',
|
|
|
|
}
|
|
|
|
return flask.Response(streamed_zip, headers=outgoing_headers)
|
|
|
|
|
|
|
|
@site.route('/batch/photos/download_zip', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_ids'], forbid_whitespace=True)
|
2018-08-15 06:02:06 +00:00
|
|
|
def post_batch_photos_download_zip():
|
|
|
|
'''
|
|
|
|
Initiating file downloads via POST requests is a bit clunky and unreliable,
|
|
|
|
so the way this works is we generate a token representing the photoset
|
|
|
|
that they want, and then they can retrieve the zip itself via GET.
|
|
|
|
'''
|
|
|
|
photo_ids = request.form['photo_ids']
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(photo_ids)
|
2018-08-15 06:02:06 +00:00
|
|
|
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
|
|
|
if not photos:
|
|
|
|
flask.abort(400)
|
|
|
|
|
|
|
|
photo_ids = [p.id for p in photos]
|
|
|
|
|
2018-10-20 09:23:12 +00:00
|
|
|
zip_token = 'etiquette_' + etiquette.helpers.hash_photoset(photos)
|
2018-08-15 06:02:06 +00:00
|
|
|
photo_download_zip_tokens[zip_token] = photo_ids
|
|
|
|
|
|
|
|
response = {'zip_token': zip_token}
|
2021-10-02 06:05:46 +00:00
|
|
|
response = flasktools.json_response(response)
|
2018-08-15 06:02:06 +00:00
|
|
|
return response
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# Search ###########################################################################################
|
|
|
|
|
|
|
|
def get_search_core():
|
2022-11-07 06:58:36 +00:00
|
|
|
search = common.P.search(
|
|
|
|
area=request.args.get('area'),
|
|
|
|
width=request.args.get('width'),
|
|
|
|
height=request.args.get('height'),
|
|
|
|
aspectratio=request.args.get('aspectratio'),
|
|
|
|
bytes=request.args.get('bytes'),
|
|
|
|
duration=request.args.get('duration'),
|
|
|
|
bitrate=request.args.get('bitrate'),
|
|
|
|
|
|
|
|
filename=request.args.get('filename'),
|
|
|
|
extension_not=request.args.get('extension_not'),
|
|
|
|
extension=request.args.get('extension'),
|
|
|
|
mimetype=request.args.get('mimetype'),
|
|
|
|
sha256=request.args.get('sha256'),
|
|
|
|
|
|
|
|
author=request.args.get('author'),
|
|
|
|
created=request.args.get('created'),
|
|
|
|
has_albums=request.args.get('has_albums'),
|
|
|
|
has_thumbnail=request.args.get('has_thumbnail'),
|
|
|
|
is_searchhidden=request.args.get('is_searchhidden', False),
|
|
|
|
|
|
|
|
has_tags=request.args.get('has_tags'),
|
|
|
|
tag_musts=request.args.get('tag_musts'),
|
|
|
|
tag_mays=request.args.get('tag_mays'),
|
|
|
|
tag_forbids=request.args.get('tag_forbids'),
|
|
|
|
tag_expression=request.args.get('tag_expression'),
|
|
|
|
|
|
|
|
limit=request.args.get('limit'),
|
|
|
|
offset=request.args.get('offset'),
|
|
|
|
orderby=request.args.get('orderby'),
|
|
|
|
|
|
|
|
yield_albums=request.args.get('yield_albums', False),
|
|
|
|
yield_photos=request.args.get('yield_photos', True),
|
|
|
|
)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2022-11-07 06:58:36 +00:00
|
|
|
# The site enforces a maximum value which the PhotoDB does not.
|
|
|
|
search.kwargs.limit = etiquette.searchhelpers.normalize_limit(search.kwargs.limit)
|
|
|
|
if search.kwargs.limit is None:
|
|
|
|
search.kwargs.limit = 50
|
|
|
|
else:
|
|
|
|
search.kwargs.limit = min(search.kwargs.limit, 1000)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2022-11-07 06:58:36 +00:00
|
|
|
search.results = list(search.results)
|
2021-01-01 20:17:15 +00:00
|
|
|
warnings = [
|
|
|
|
w.error_message if hasattr(w, 'error_message') else str(w)
|
2022-11-07 06:58:36 +00:00
|
|
|
for w in search.warning_bag.warnings
|
2021-01-01 20:17:15 +00:00
|
|
|
]
|
|
|
|
|
2022-11-07 06:58:36 +00:00
|
|
|
# Web UI users aren't allowed to use within_directory anyway, so don't
|
|
|
|
# show it to them.
|
|
|
|
del search.kwargs.within_directory
|
|
|
|
return search
|
|
|
|
|
|
|
|
@site.route('/search_embed')
|
|
|
|
def get_search_embed():
|
|
|
|
search = get_search_core()
|
|
|
|
response = common.render_template(
|
|
|
|
request,
|
|
|
|
'search_embed.html',
|
|
|
|
results=search.results,
|
|
|
|
search_kwargs=search.kwargs,
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/search')
|
|
|
|
def get_search_html():
|
|
|
|
search = get_search_core()
|
|
|
|
search.kwargs.view = request.args.get('view', 'grid')
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# TAGS ON THIS PAGE
|
|
|
|
total_tags = set()
|
2022-11-07 06:58:36 +00:00
|
|
|
for result in search.results:
|
2020-04-03 06:27:47 +00:00
|
|
|
if isinstance(result, etiquette.objects.Photo):
|
|
|
|
total_tags.update(result.get_tags())
|
2018-07-20 05:42:21 +00:00
|
|
|
total_tags = sorted(total_tags, key=lambda t: t.name)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
# PREV-NEXT PAGE URLS
|
2022-11-07 06:58:36 +00:00
|
|
|
offset = search.kwargs.offset or 0
|
2018-01-12 03:40:56 +00:00
|
|
|
original_params = request.args.to_dict()
|
2022-11-07 06:58:36 +00:00
|
|
|
original_params['limit'] = search.kwargs.limit
|
2020-03-29 00:38:10 +00:00
|
|
|
|
2022-11-07 06:58:36 +00:00
|
|
|
if search.more_after_limit:
|
2018-01-12 03:40:56 +00:00
|
|
|
next_params = original_params.copy()
|
2022-11-07 06:58:36 +00:00
|
|
|
next_params['offset'] = offset + search.kwargs.limit
|
2018-11-04 21:30:08 +00:00
|
|
|
next_params = helpers.dict_to_params(next_params)
|
2018-01-12 03:40:56 +00:00
|
|
|
next_page_url = '/search' + next_params
|
|
|
|
else:
|
|
|
|
next_page_url = None
|
|
|
|
|
2022-11-07 06:58:36 +00:00
|
|
|
if search.kwargs.limit and offset > 0:
|
2018-01-12 03:40:56 +00:00
|
|
|
prev_params = original_params.copy()
|
2022-11-07 06:58:36 +00:00
|
|
|
prev_offset = max(0, offset - search.kwargs.limit)
|
2020-03-29 00:15:58 +00:00
|
|
|
if prev_offset > 0:
|
|
|
|
prev_params['offset'] = prev_offset
|
2020-04-03 04:37:50 +00:00
|
|
|
else:
|
|
|
|
prev_params.pop('offset', None)
|
2018-11-04 21:30:08 +00:00
|
|
|
prev_params = helpers.dict_to_params(prev_params)
|
2018-01-12 03:40:56 +00:00
|
|
|
prev_page_url = '/search' + prev_params
|
|
|
|
else:
|
|
|
|
prev_page_url = None
|
|
|
|
|
2019-08-14 20:40:52 +00:00
|
|
|
response = common.render_template(
|
|
|
|
request,
|
2018-01-12 03:40:56 +00:00
|
|
|
'search.html',
|
2022-11-07 06:58:36 +00:00
|
|
|
next_page_url=next_page_url,
|
|
|
|
prev_page_url=prev_page_url,
|
|
|
|
results=search.results,
|
|
|
|
search_kwargs=search.kwargs,
|
|
|
|
total_tags=total_tags,
|
|
|
|
warnings=search.warning_bag.jsonify(),
|
2018-01-12 03:40:56 +00:00
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2022-07-20 03:05:15 +00:00
|
|
|
@site.route('/search.atom')
|
|
|
|
def get_search_atom():
|
2022-11-07 06:58:36 +00:00
|
|
|
search = get_search_core()
|
2022-07-20 03:05:15 +00:00
|
|
|
soup = etiquette.helpers.make_atom_feed(
|
2022-11-07 06:58:36 +00:00
|
|
|
search.results,
|
2022-11-11 19:39:04 +00:00
|
|
|
feed_id='/search' + request.query_string.decode('utf-8'),
|
2022-07-20 03:05:15 +00:00
|
|
|
feed_title='etiquette search',
|
|
|
|
feed_link=request.url.replace('/search.atom', '/search'),
|
|
|
|
)
|
2022-11-11 19:39:04 +00:00
|
|
|
response = flasktools.atom_response(soup)
|
2022-07-20 03:05:15 +00:00
|
|
|
return response
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
@site.route('/search.json')
|
|
|
|
def get_search_json():
|
2022-11-07 06:58:36 +00:00
|
|
|
search = get_search_core()
|
|
|
|
response = search.jsonify()
|
|
|
|
return flasktools.json_response(response)
|
2021-06-04 04:28:05 +00:00
|
|
|
|
|
|
|
# Swipe ############################################################################################
|
|
|
|
|
|
|
|
@site.route('/swipe')
|
|
|
|
def get_swipe():
|
|
|
|
response = common.render_template(request, 'swipe.html')
|
|
|
|
return response
|