2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
2021-02-26 02:57:41 +00:00
|
|
|
import os
|
2018-01-12 03:40:56 +00:00
|
|
|
import urllib.parse
|
|
|
|
|
2021-06-05 04:49:45 +00:00
|
|
|
from voussoirkit import flasktools
|
2021-09-01 02:23:56 +00:00
|
|
|
from voussoirkit import gentools
|
2020-11-16 06:18:40 +00:00
|
|
|
from voussoirkit import stringtools
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
import etiquette
|
|
|
|
|
2018-07-19 01:37:21 +00:00
|
|
|
from .. import common
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
site = common.site
|
|
|
|
session_manager = common.session_manager
|
|
|
|
|
|
|
|
# Individual albums ################################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>')
|
|
|
|
def get_album_html(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='html')
|
2019-08-14 20:40:52 +00:00
|
|
|
response = common.render_template(
|
|
|
|
request,
|
2018-01-12 03:40:56 +00:00
|
|
|
'album.html',
|
|
|
|
album=album,
|
|
|
|
view=request.args.get('view', 'grid'),
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>.json')
|
|
|
|
def get_album_json(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2021-01-01 20:56:05 +00:00
|
|
|
album = album.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(album)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/album/<album_id>.zip')
|
|
|
|
def get_album_zip(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='html')
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
recursive = request.args.get('recursive', True)
|
2021-09-05 08:17:05 +00:00
|
|
|
recursive = stringtools.truthystring(recursive)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-08-15 05:58:26 +00:00
|
|
|
streamed_zip = etiquette.helpers.zip_album(album, recursive=recursive)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
if album.title:
|
2018-08-15 05:58:26 +00:00
|
|
|
download_as = f'album {album.id} - {album.title}.zip'
|
2018-01-12 03:40:56 +00:00
|
|
|
else:
|
2018-08-15 05:58:26 +00:00
|
|
|
download_as = f'album {album.id}.zip'
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
download_as = etiquette.helpers.remove_path_badchars(download_as)
|
|
|
|
download_as = urllib.parse.quote(download_as)
|
|
|
|
outgoing_headers = {
|
|
|
|
'Content-Type': 'application/octet-stream',
|
2018-08-15 05:58:26 +00:00
|
|
|
'Content-Disposition': f'attachment; filename*=UTF-8\'\'{download_as}',
|
2018-01-12 03:40:56 +00:00
|
|
|
}
|
2018-08-15 05:58:26 +00:00
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
return flask.Response(streamed_zip, headers=outgoing_headers)
|
|
|
|
|
2018-09-23 20:27:06 +00:00
|
|
|
@site.route('/album/<album_id>/add_child', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['child_id'], forbid_whitespace=True)
|
2018-09-23 20:27:06 +00:00
|
|
|
def post_album_add_child(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-11-16 06:18:40 +00:00
|
|
|
child_ids = stringtools.comma_space_split(request.form['child_id'])
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
children = list(common.P_albums(child_ids, response_type='json'))
|
|
|
|
print(children)
|
|
|
|
album.add_children(children)
|
|
|
|
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-09-23 20:27:06 +00:00
|
|
|
|
|
|
|
@site.route('/album/<album_id>/remove_child', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['child_id'], forbid_whitespace=True)
|
2018-09-23 20:27:06 +00:00
|
|
|
def post_album_remove_child(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-11-16 06:18:40 +00:00
|
|
|
child_ids = stringtools.comma_space_split(request.form['child_id'])
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
children = list(common.P_albums(child_ids, response_type='json'))
|
|
|
|
album.remove_children(children)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-09-23 20:27:06 +00:00
|
|
|
|
2021-01-21 01:55:13 +00:00
|
|
|
@site.route('/album/<album_id>/remove_thumbnail_photo', methods=['POST'])
|
|
|
|
def post_album_remove_thumbnail_photo(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
album.set_thumbnail_photo(None)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(album.jsonify())
|
2021-01-21 01:55:13 +00:00
|
|
|
|
2019-06-15 09:44:46 +00:00
|
|
|
@site.route('/album/<album_id>/refresh_directories', methods=['POST'])
|
|
|
|
def post_album_refresh_directories(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
for directory in album.get_associated_directories():
|
|
|
|
if not directory.is_dir:
|
|
|
|
continue
|
|
|
|
digest = common.P.digest_directory(directory, new_photo_ratelimit=0.1)
|
|
|
|
gentools.run(digest)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2019-06-15 09:44:46 +00:00
|
|
|
|
2021-01-21 01:03:52 +00:00
|
|
|
@site.route('/album/<album_id>/set_thumbnail_photo', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_id'], forbid_whitespace=True)
|
2021-01-21 01:03:52 +00:00
|
|
|
def post_album_set_thumbnail_photo(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
photo = common.P_photo(request.form['photo_id'], response_type='json')
|
|
|
|
album.set_thumbnail_photo(photo)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(album.jsonify())
|
2021-01-21 01:03:52 +00:00
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# Album photo operations ###########################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/add_photo', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_id'], forbid_whitespace=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
def post_album_add_photo(album_id):
|
|
|
|
'''
|
|
|
|
Add a photo or photos to this album.
|
|
|
|
'''
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(request.form['photo_id'])
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
|
|
|
album.add_photos(photos)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/album/<album_id>/remove_photo', methods=['POST'])
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.required_fields(['photo_id'], forbid_whitespace=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
def post_album_remove_photo(album_id):
|
|
|
|
'''
|
|
|
|
Remove a photo or photos from this album.
|
|
|
|
'''
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(request.form['photo_id'])
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
|
|
|
album.remove_photos(photos)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
# Album tag operations #############################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/add_tag', methods=['POST'])
|
|
|
|
def post_album_add_tag(album_id):
|
|
|
|
'''
|
|
|
|
Apply a tag to every photo in the album.
|
|
|
|
'''
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2018-01-12 03:40:56 +00:00
|
|
|
response = {}
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
|
|
|
|
tag = request.form['tagname'].strip()
|
|
|
|
try:
|
|
|
|
tag = common.P_tag(tag, response_type='json')
|
|
|
|
except etiquette.exceptions.NoSuchTag as exc:
|
|
|
|
response = exc.jsonify()
|
|
|
|
return flasktools.json_response(response, status=404)
|
|
|
|
recursive = request.form.get('recursive', False)
|
|
|
|
recursive = stringtools.truthystring(recursive)
|
|
|
|
album.add_tag_to_all(tag, nested_children=recursive)
|
2018-01-12 03:40:56 +00:00
|
|
|
response['action'] = 'add_tag'
|
|
|
|
response['tagname'] = tag.name
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
# Album metadata operations ########################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/edit', methods=['POST'])
|
|
|
|
def post_album_edit(album_id):
|
|
|
|
'''
|
|
|
|
Edit the title / description.
|
|
|
|
'''
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2018-01-12 03:40:56 +00:00
|
|
|
title = request.form.get('title', None)
|
|
|
|
description = request.form.get('description', None)
|
2022-07-16 06:00:07 +00:00
|
|
|
|
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
album.edit(title=title, description=description)
|
|
|
|
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify(minimal=True)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2021-02-26 02:57:41 +00:00
|
|
|
@site.route('/album/<album_id>/show_in_folder', methods=['POST'])
|
|
|
|
def post_album_show_in_folder(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2021-02-26 02:57:41 +00:00
|
|
|
if not request.is_localhost:
|
|
|
|
flask.abort(403)
|
|
|
|
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
directories = album.get_associated_directories()
|
|
|
|
if len(directories) != 1:
|
|
|
|
flask.abort(400)
|
|
|
|
directory = directories.pop()
|
|
|
|
|
|
|
|
if os.name == 'nt':
|
|
|
|
command = f'start explorer.exe "{directory.absolute_path}"'
|
|
|
|
os.system(command)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|
2021-02-26 02:57:41 +00:00
|
|
|
|
|
|
|
flask.abort(501)
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# Album listings ###################################################################################
|
|
|
|
|
2020-10-04 01:37:08 +00:00
|
|
|
@site.route('/all_albums.json')
|
2023-02-04 21:49:42 +00:00
|
|
|
@common.permission_manager.basic_decorator
|
2021-09-03 19:45:07 +00:00
|
|
|
@flasktools.cached_endpoint(max_age=15)
|
2020-10-04 01:37:08 +00:00
|
|
|
def get_all_album_names():
|
2021-06-05 04:00:49 +00:00
|
|
|
all_albums = {album.id: album.display_name for album in common.P.get_albums()}
|
2021-01-05 21:05:18 +00:00
|
|
|
response = {'albums': all_albums}
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2020-10-04 01:37:08 +00:00
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
@site.route('/albums')
|
|
|
|
def get_albums_html():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-09-29 02:49:01 +00:00
|
|
|
albums = list(common.P.get_root_albums())
|
|
|
|
albums.sort(key=lambda x: x.display_name.lower())
|
2019-08-14 20:40:52 +00:00
|
|
|
response = common.render_template(
|
|
|
|
request,
|
2018-11-13 06:15:59 +00:00
|
|
|
'album.html',
|
|
|
|
albums=albums,
|
|
|
|
view=request.args.get('view', 'grid'),
|
|
|
|
)
|
|
|
|
return response
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
@site.route('/albums.json')
|
|
|
|
def get_albums_json():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-09-29 02:49:01 +00:00
|
|
|
albums = list(common.P.get_albums())
|
|
|
|
albums.sort(key=lambda x: x.display_name.lower())
|
|
|
|
albums = [album.jsonify(include_photos=False) for album in albums]
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(albums)
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
# Album create and delete ##########################################################################
|
|
|
|
|
|
|
|
@site.route('/albums/create_album', methods=['POST'])
|
|
|
|
def post_albums_create():
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2018-01-12 03:40:56 +00:00
|
|
|
title = request.form.get('title', None)
|
|
|
|
description = request.form.get('description', None)
|
2018-09-23 22:13:31 +00:00
|
|
|
parent_id = request.form.get('parent_id', None)
|
|
|
|
if parent_id is not None:
|
2020-02-27 01:50:36 +00:00
|
|
|
parent = common.P_album(parent_id, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2018-03-18 22:28:26 +00:00
|
|
|
user = session_manager.get(request).user
|
|
|
|
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P.new_album(title=title, description=description, author=user)
|
|
|
|
if parent_id is not None:
|
|
|
|
parent.add_child(album)
|
2018-03-18 22:28:26 +00:00
|
|
|
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify(minimal=False)
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response(response)
|
2018-09-23 20:27:06 +00:00
|
|
|
|
|
|
|
@site.route('/album/<album_id>/delete', methods=['POST'])
|
|
|
|
def post_album_delete(album_id):
|
2023-02-04 21:49:42 +00:00
|
|
|
common.permission_manager.basic()
|
2022-07-16 06:00:07 +00:00
|
|
|
with common.P.transaction:
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
album.delete()
|
2021-10-02 06:05:46 +00:00
|
|
|
return flasktools.json_response({})
|