2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
2021-02-26 02:57:41 +00:00
|
|
|
import os
|
2020-10-04 01:37:08 +00:00
|
|
|
import time
|
2018-01-12 03:40:56 +00:00
|
|
|
import urllib.parse
|
|
|
|
|
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
|
|
|
from .. import decorators
|
|
|
|
from .. import jsonify
|
|
|
|
|
|
|
|
site = common.site
|
|
|
|
session_manager = common.session_manager
|
|
|
|
|
|
|
|
# Individual albums ################################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>')
|
|
|
|
def get_album_html(album_id):
|
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):
|
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()
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(album)
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>.zip')
|
|
|
|
def get_album_zip(album_id):
|
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)
|
|
|
|
recursive = etiquette.helpers.truthystring(recursive)
|
|
|
|
|
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'])
|
|
|
|
@decorators.required_fields(['child_id'], forbid_whitespace=True)
|
|
|
|
def post_album_add_child(album_id):
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2020-09-28 06:21:12 +00:00
|
|
|
|
2020-11-16 06:18:40 +00:00
|
|
|
child_ids = stringtools.comma_space_split(request.form['child_id'])
|
2020-09-28 06:21:12 +00:00
|
|
|
children = list(common.P_albums(child_ids, response_type='json'))
|
|
|
|
print(children)
|
|
|
|
album.add_children(children, commit=True)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2018-09-23 20:27:06 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/remove_child', methods=['POST'])
|
|
|
|
@decorators.required_fields(['child_id'], forbid_whitespace=True)
|
|
|
|
def post_album_remove_child(album_id):
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2020-09-28 06:21:12 +00:00
|
|
|
|
2020-11-16 06:18:40 +00:00
|
|
|
child_ids = stringtools.comma_space_split(request.form['child_id'])
|
2020-09-28 06:21:12 +00:00
|
|
|
children = list(common.P_albums(child_ids, response_type='json'))
|
|
|
|
album.remove_children(children, commit=True)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2018-09-23 20:27:06 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
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):
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
|
|
|
album.set_thumbnail_photo(None)
|
|
|
|
common.P.commit(message='album remove thumbnail photo endpoint')
|
|
|
|
return jsonify.make_json_response(album.jsonify())
|
|
|
|
|
2019-06-15 09:44:46 +00:00
|
|
|
@site.route('/album/<album_id>/refresh_directories', methods=['POST'])
|
|
|
|
def post_album_refresh_directories(album_id):
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2019-06-15 09:44:46 +00:00
|
|
|
for directory in album.get_associated_directories():
|
2021-01-19 17:59:15 +00:00
|
|
|
if not directory.is_dir:
|
|
|
|
continue
|
2020-09-15 20:47:06 +00:00
|
|
|
digest = common.P.digest_directory(directory, new_photo_ratelimit=0.1)
|
|
|
|
etiquette.helpers.run_generator(digest)
|
2019-06-15 09:44:46 +00:00
|
|
|
common.P.commit(message='refresh album directories endpoint')
|
|
|
|
return jsonify.make_json_response({})
|
|
|
|
|
2021-01-21 01:03:52 +00:00
|
|
|
@site.route('/album/<album_id>/set_thumbnail_photo', methods=['POST'])
|
|
|
|
@decorators.required_fields(['photo_id'], forbid_whitespace=True)
|
|
|
|
def post_album_set_thumbnail_photo(album_id):
|
|
|
|
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)
|
|
|
|
common.P.commit(message='album set thumbnail photo endpoint')
|
|
|
|
return jsonify.make_json_response(album.jsonify())
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
# Album photo operations ###########################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/add_photo', methods=['POST'])
|
|
|
|
@decorators.required_fields(['photo_id'], forbid_whitespace=True)
|
|
|
|
def post_album_add_photo(album_id):
|
|
|
|
'''
|
|
|
|
Add a photo or photos to this album.
|
|
|
|
'''
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(request.form['photo_id'])
|
2020-02-27 01:50:36 +00:00
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
2020-02-20 08:34:28 +00:00
|
|
|
album.add_photos(photos, commit=True)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/remove_photo', methods=['POST'])
|
|
|
|
@decorators.required_fields(['photo_id'], forbid_whitespace=True)
|
|
|
|
def post_album_remove_photo(album_id):
|
|
|
|
'''
|
|
|
|
Remove a photo or photos from this album.
|
|
|
|
'''
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
|
2020-11-16 06:18:40 +00:00
|
|
|
photo_ids = stringtools.comma_space_split(request.form['photo_id'])
|
2020-02-27 01:50:36 +00:00
|
|
|
photos = list(common.P_photos(photo_ids, response_type='json'))
|
2020-02-20 08:34:28 +00:00
|
|
|
album.remove_photos(photos, commit=True)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify()
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
'''
|
|
|
|
response = {}
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
tag = request.form['tagname'].strip()
|
|
|
|
try:
|
2020-02-27 01:50:36 +00:00
|
|
|
tag = common.P_tag(tag, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
except etiquette.exceptions.NoSuchTag as exc:
|
2021-01-01 20:56:05 +00:00
|
|
|
response = exc.jsonify()
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(response, status=404)
|
|
|
|
recursive = request.form.get('recursive', False)
|
|
|
|
recursive = etiquette.helpers.truthystring(recursive)
|
2020-02-20 08:34:28 +00:00
|
|
|
album.add_tag_to_all(tag, nested_children=recursive, commit=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
response['action'] = 'add_tag'
|
|
|
|
response['tagname'] = tag.name
|
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
|
|
|
# Album metadata operations ########################################################################
|
|
|
|
|
|
|
|
@site.route('/album/<album_id>/edit', methods=['POST'])
|
|
|
|
def post_album_edit(album_id):
|
|
|
|
'''
|
|
|
|
Edit the title / description.
|
|
|
|
'''
|
2020-02-27 01:50:36 +00:00
|
|
|
album = common.P_album(album_id, response_type='json')
|
2018-01-12 03:40:56 +00:00
|
|
|
|
|
|
|
title = request.form.get('title', None)
|
|
|
|
description = request.form.get('description', None)
|
2020-02-20 08:34:28 +00:00
|
|
|
album.edit(title=title, description=description, commit=True)
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify(minimal=True)
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
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):
|
|
|
|
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)
|
|
|
|
return jsonify.make_json_response({})
|
|
|
|
|
|
|
|
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')
|
2021-01-05 20:43:39 +00:00
|
|
|
@decorators.cached_endpoint(max_age=0)
|
2020-10-04 01:37:08 +00:00
|
|
|
def get_all_album_names():
|
|
|
|
all_albums = {album.display_name: album.id for album in common.P.get_albums()}
|
2021-01-05 21:05:18 +00:00
|
|
|
response = {'albums': all_albums}
|
2020-10-04 01:37:08 +00:00
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
def get_albums_core():
|
|
|
|
albums = list(common.P.get_root_albums())
|
|
|
|
albums.sort(key=lambda x: x.display_name.lower())
|
|
|
|
return albums
|
|
|
|
|
|
|
|
@site.route('/albums')
|
|
|
|
def get_albums_html():
|
|
|
|
albums = get_albums_core()
|
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():
|
|
|
|
albums = get_albums_core()
|
2021-01-01 20:56:05 +00:00
|
|
|
albums = [album.jsonify(minimal=True) for album in albums]
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_json_response(albums)
|
|
|
|
|
|
|
|
# Album create and delete ##########################################################################
|
|
|
|
|
|
|
|
@site.route('/albums/create_album', methods=['POST'])
|
|
|
|
def post_albums_create():
|
|
|
|
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
|
|
|
|
|
|
|
|
album = common.P.new_album(title=title, description=description, author=user)
|
2018-09-23 22:13:31 +00:00
|
|
|
if parent_id is not None:
|
2018-01-12 03:40:56 +00:00
|
|
|
parent.add_child(album)
|
2020-02-20 08:34:28 +00:00
|
|
|
common.P.commit('create album endpoint')
|
2018-03-18 22:28:26 +00:00
|
|
|
|
2021-01-01 20:56:05 +00:00
|
|
|
response = album.jsonify(minimal=False)
|
2018-01-12 03:40:56 +00:00
|
|
|
return jsonify.make_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):
|
|
|
|
album = common.P_album(album_id, response_type='json')
|
2020-02-20 08:34:28 +00:00
|
|
|
album.delete(commit=True)
|
2018-09-23 20:27:06 +00:00
|
|
|
return jsonify.make_json_response({})
|