2018-01-12 03:40:56 +00:00
|
|
|
import flask; from flask import request
|
|
|
|
import urllib.parse
|
|
|
|
|
|
|
|
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')
|
2018-01-12 03:40:56 +00:00
|
|
|
album = etiquette.jsonify.album(album)
|
|
|
|
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')
|
|
|
|
child = common.P_album(request.form['child_id'], response_type='json')
|
2020-02-20 08:34:28 +00:00
|
|
|
album.add_child(child, commit=True)
|
2018-09-23 20:27:06 +00:00
|
|
|
response = etiquette.jsonify.album(child)
|
|
|
|
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')
|
|
|
|
child = common.P_album(request.form['child_id'], response_type='json')
|
2020-02-20 08:34:28 +00:00
|
|
|
album.remove_child(child, commit=True)
|
2018-09-23 20:27:06 +00:00
|
|
|
response = etiquette.jsonify.album(child)
|
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
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():
|
2020-02-20 06:20:21 +00:00
|
|
|
common.P.digest_directory(directory, new_photo_ratelimit=0.1)
|
2019-06-15 09:44:46 +00:00
|
|
|
common.P.commit(message='refresh album directories endpoint')
|
|
|
|
return jsonify.make_json_response({})
|
|
|
|
|
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.
|
|
|
|
'''
|
|
|
|
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
|
|
|
|
|
|
|
photo_ids = etiquette.helpers.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)
|
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.
|
|
|
|
'''
|
|
|
|
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
|
|
|
|
|
|
|
photo_ids = etiquette.helpers.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)
|
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:
|
|
|
|
response = etiquette.jsonify.exception(exc)
|
|
|
|
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)
|
2018-01-12 03:40:56 +00:00
|
|
|
response = etiquette.jsonify.album(album, minimal=True)
|
|
|
|
return jsonify.make_json_response(response)
|
|
|
|
|
|
|
|
# Album listings ###################################################################################
|
|
|
|
|
|
|
|
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()
|
|
|
|
albums = [etiquette.jsonify.album(album, minimal=True) for album in albums]
|
|
|
|
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
|
|
|
|
2018-01-12 03:40:56 +00:00
|
|
|
response = etiquette.jsonify.album(album, minimal=False)
|
|
|
|
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({})
|