2016-09-18 08:33:46 +00:00
|
|
|
import flask
|
|
|
|
from flask import request
|
|
|
|
import json
|
|
|
|
import mimetypes
|
|
|
|
import os
|
|
|
|
import random
|
2017-03-23 05:54:44 +00:00
|
|
|
import traceback
|
2016-12-25 02:34:34 +00:00
|
|
|
import urllib.parse
|
2016-09-18 08:33:46 +00:00
|
|
|
import warnings
|
2016-12-21 01:44:22 +00:00
|
|
|
import zipstream
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
import etiquette
|
|
|
|
import etiquette_flask
|
2017-02-05 03:55:13 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
from voussoirkit import pathclass
|
2017-02-05 03:55:13 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
|
|
|
|
root_dir = pathclass.Path(__file__).parent
|
|
|
|
|
|
|
|
TEMPLATE_DIR = root_dir.with_child('templates')
|
|
|
|
STATIC_DIR = root_dir.with_child('static')
|
|
|
|
FAVICON_PATH = STATIC_DIR.with_child('favicon.png')
|
2017-02-05 03:55:13 +00:00
|
|
|
|
|
|
|
site = flask.Flask(
|
|
|
|
__name__,
|
2017-05-02 04:23:16 +00:00
|
|
|
template_folder=TEMPLATE_DIR.absolute_path,
|
|
|
|
static_folder=STATIC_DIR.absolute_path,
|
2017-02-05 03:55:13 +00:00
|
|
|
)
|
2016-09-18 08:33:46 +00:00
|
|
|
site.config.update(
|
|
|
|
SEND_FILE_MAX_AGE_DEFAULT=180,
|
|
|
|
TEMPLATES_AUTO_RELOAD=True,
|
|
|
|
)
|
2016-10-10 03:50:13 +00:00
|
|
|
site.jinja_env.add_extension('jinja2.ext.do')
|
2017-02-05 02:30:02 +00:00
|
|
|
site.jinja_env.trim_blocks = True
|
|
|
|
site.jinja_env.lstrip_blocks = True
|
2016-11-06 04:24:43 +00:00
|
|
|
site.debug = True
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
P = etiquette.photodb.PhotoDB()
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
session_manager = etiquette_flask.sessions.SessionManager()
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
|
|
|
|
|
2016-12-18 13:12:14 +00:00
|
|
|
def back_url():
|
|
|
|
return request.args.get('goto') or request.referrer or '/'
|
|
|
|
|
2016-10-18 05:13:12 +00:00
|
|
|
def create_tag(easybake_string):
|
|
|
|
notes = P.easybake(easybake_string)
|
|
|
|
notes = [{'action': action, 'tagname': tagname} for (action, tagname) in notes]
|
|
|
|
return notes
|
|
|
|
|
|
|
|
def delete_tag(tag):
|
|
|
|
tag = tag.split('.')[-1].split('+')[0]
|
|
|
|
tag = P.get_tag(tag)
|
|
|
|
|
|
|
|
tag.delete()
|
|
|
|
return {'action': 'delete_tag', 'tagname': tag.name}
|
|
|
|
|
|
|
|
def delete_synonym(synonym):
|
|
|
|
synonym = synonym.split('+')[-1].split('.')[-1]
|
|
|
|
|
2017-03-05 06:27:24 +00:00
|
|
|
try:
|
|
|
|
master_tag = P.get_tag(synonym)
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.NoSuchTag as e:
|
|
|
|
raise etiquette.exceptions.NoSuchSynonym(*e.given_args, **e.given_kwargs)
|
2016-10-18 05:13:12 +00:00
|
|
|
master_tag.remove_synonym(synonym)
|
2017-02-26 08:33:26 +00:00
|
|
|
|
2016-10-18 05:13:12 +00:00
|
|
|
return {'action':'delete_synonym', 'synonym': synonym}
|
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
def P_wrapper(function):
|
|
|
|
def P_wrapped(thingid, response_type='html'):
|
2017-03-05 04:07:30 +00:00
|
|
|
try:
|
|
|
|
return function(thingid)
|
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.EtiquetteException as e:
|
|
|
|
if isinstance(e, etiquette.exceptions.NoSuch):
|
2017-03-05 05:56:23 +00:00
|
|
|
status = 404
|
|
|
|
else:
|
|
|
|
status = 400
|
|
|
|
|
2017-03-05 04:07:30 +00:00
|
|
|
if response_type == 'html':
|
2017-03-05 05:56:23 +00:00
|
|
|
flask.abort(status, e.error_message)
|
2017-03-05 04:07:30 +00:00
|
|
|
else:
|
2017-05-02 04:23:16 +00:00
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
response = etiquette_flask.jsonify.make_json_response(response, status=status)
|
2017-03-05 04:07:30 +00:00
|
|
|
flask.abort(response)
|
|
|
|
|
|
|
|
except Exception as e:
|
2017-03-23 05:54:44 +00:00
|
|
|
traceback.print_exc()
|
|
|
|
if response_type == 'html':
|
|
|
|
flask.abort(500)
|
|
|
|
else:
|
2017-05-02 04:23:16 +00:00
|
|
|
flask.abort(etiquette.jsonify.make_json_response({}, status=500))
|
2017-02-26 08:33:26 +00:00
|
|
|
|
|
|
|
return P_wrapped
|
|
|
|
|
|
|
|
@P_wrapper
|
2016-09-18 08:33:46 +00:00
|
|
|
def P_album(albumid):
|
2017-03-05 04:07:30 +00:00
|
|
|
return P.get_album(albumid)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
@P_wrapper
|
2016-09-18 08:33:46 +00:00
|
|
|
def P_photo(photoid):
|
2017-03-05 04:07:30 +00:00
|
|
|
return P.get_photo(photoid)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
@P_wrapper
|
2016-09-18 08:33:46 +00:00
|
|
|
def P_tag(tagname):
|
2017-03-05 04:07:30 +00:00
|
|
|
return P.get_tag(tagname)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
@P_wrapper
|
2017-02-05 02:30:02 +00:00
|
|
|
def P_user(username):
|
2017-03-05 04:07:30 +00:00
|
|
|
return P.get_user(username=username)
|
2017-02-05 02:30:02 +00:00
|
|
|
|
2017-02-28 07:05:43 +00:00
|
|
|
def send_file(filepath, override_mimetype=None):
|
2016-09-18 08:33:46 +00:00
|
|
|
'''
|
|
|
|
Range-enabled file sending.
|
|
|
|
'''
|
2016-10-10 03:50:13 +00:00
|
|
|
try:
|
|
|
|
file_size = os.path.getsize(filepath)
|
|
|
|
except FileNotFoundError:
|
|
|
|
flask.abort(404)
|
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
outgoing_headers = {}
|
2017-02-28 07:05:43 +00:00
|
|
|
if override_mimetype is not None:
|
|
|
|
mimetype = override_mimetype
|
|
|
|
else:
|
|
|
|
mimetype = mimetypes.guess_type(filepath)[0]
|
2017-02-28 07:39:06 +00:00
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
if mimetype is not None:
|
|
|
|
if 'text/' in mimetype:
|
|
|
|
mimetype += '; charset=utf-8'
|
|
|
|
outgoing_headers['Content-Type'] = mimetype
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
if 'range' in request.headers:
|
|
|
|
desired_range = request.headers['range'].lower()
|
|
|
|
desired_range = desired_range.split('bytes=')[-1]
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
int_helper = lambda x: int(x) if x.isdigit() else None
|
|
|
|
if '-' in desired_range:
|
|
|
|
(desired_min, desired_max) = desired_range.split('-')
|
|
|
|
range_min = int_helper(desired_min)
|
|
|
|
range_max = int_helper(desired_max)
|
|
|
|
else:
|
|
|
|
range_min = int_helper(desired_range)
|
|
|
|
|
|
|
|
if range_min is None:
|
|
|
|
range_min = 0
|
|
|
|
if range_max is None:
|
|
|
|
range_max = file_size
|
|
|
|
|
|
|
|
# because ranges are 0-indexed
|
|
|
|
range_max = min(range_max, file_size - 1)
|
|
|
|
range_min = max(range_min, 0)
|
|
|
|
|
|
|
|
range_header = 'bytes {min}-{max}/{outof}'.format(
|
|
|
|
min=range_min,
|
|
|
|
max=range_max,
|
|
|
|
outof=file_size,
|
|
|
|
)
|
|
|
|
outgoing_headers['Content-Range'] = range_header
|
|
|
|
status = 206
|
2016-09-18 08:33:46 +00:00
|
|
|
else:
|
2016-10-10 03:50:13 +00:00
|
|
|
range_max = file_size - 1
|
2016-09-18 08:33:46 +00:00
|
|
|
range_min = 0
|
2016-10-10 03:50:13 +00:00
|
|
|
status = 200
|
2016-09-18 08:33:46 +00:00
|
|
|
|
|
|
|
outgoing_headers['Accept-Ranges'] = 'bytes'
|
|
|
|
outgoing_headers['Content-Length'] = (range_max - range_min) + 1
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
if request.method == 'HEAD':
|
|
|
|
outgoing_data = bytes()
|
|
|
|
else:
|
2017-05-02 04:23:16 +00:00
|
|
|
outgoing_data = etiquette.helpers.read_filebytes(
|
2016-12-17 02:53:12 +00:00
|
|
|
filepath,
|
|
|
|
range_min=range_min,
|
|
|
|
range_max=range_max,
|
|
|
|
chunk_size=P.config['file_read_chunk'],
|
|
|
|
)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
response = flask.Response(
|
|
|
|
outgoing_data,
|
2016-10-10 03:50:13 +00:00
|
|
|
status=status,
|
2016-09-18 08:33:46 +00:00
|
|
|
headers=outgoing_headers,
|
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
|
|
|
####################################################################################################
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-12-18 13:12:14 +00:00
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
@site.route('/')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-09-18 08:33:46 +00:00
|
|
|
def root():
|
2016-12-17 02:53:12 +00:00
|
|
|
motd = random.choice(P.config['motd_strings'])
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
|
|
|
return flask.render_template('root.html', motd=motd, session=session)
|
|
|
|
|
|
|
|
@site.route('/login', methods=['GET'])
|
|
|
|
@session_manager.give_token
|
|
|
|
def get_login():
|
|
|
|
session = session_manager.get(request)
|
|
|
|
return flask.render_template('login.html', session=session)
|
|
|
|
|
|
|
|
@site.route('/register', methods=['GET'])
|
|
|
|
def get_register():
|
|
|
|
return flask.redirect('/login')
|
|
|
|
|
|
|
|
@site.route('/login', methods=['POST'])
|
|
|
|
@session_manager.give_token
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['username', 'password'])
|
2016-12-18 13:12:14 +00:00
|
|
|
def post_login():
|
|
|
|
if session_manager.get(request):
|
2017-05-02 04:23:16 +00:00
|
|
|
e = etiquette.exceptions.AlreadySignedIn()
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=403)
|
2016-12-18 13:12:14 +00:00
|
|
|
|
|
|
|
username = request.form['username']
|
|
|
|
password = request.form['password']
|
|
|
|
try:
|
2017-04-21 01:31:48 +00:00
|
|
|
# Consideration: Should the server hash the password to discourage
|
|
|
|
# information (user exists) leak via response time?
|
|
|
|
# Currently I think not, because they can check if the account
|
|
|
|
# page 404s anyway.
|
2016-12-21 05:33:14 +00:00
|
|
|
user = P.get_user(username=username)
|
2016-12-18 13:12:14 +00:00
|
|
|
user = P.login(user.id, password)
|
2017-05-02 04:23:16 +00:00
|
|
|
except (etiquette.exceptions.NoSuchUser, etiquette.exceptions.WrongLogin):
|
|
|
|
e = etiquette.exceptions.WrongLogin()
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=422)
|
|
|
|
session = etiquette_flask.sessions.Session(request, user)
|
2016-12-18 13:12:14 +00:00
|
|
|
session_manager.add(session)
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response({})
|
2016-12-18 13:12:14 +00:00
|
|
|
|
|
|
|
@site.route('/register', methods=['POST'])
|
|
|
|
@session_manager.give_token
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['username', 'password_1', 'password_2'])
|
2016-12-18 13:12:14 +00:00
|
|
|
def post_register():
|
|
|
|
if session_manager.get(request):
|
2017-05-02 04:23:16 +00:00
|
|
|
e = etiquette.exceptions.AlreadySignedIn()
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=403)
|
2016-12-18 13:12:14 +00:00
|
|
|
|
|
|
|
username = request.form['username']
|
|
|
|
password_1 = request.form['password_1']
|
|
|
|
password_2 = request.form['password_2']
|
|
|
|
|
|
|
|
if password_1 != password_2:
|
2017-03-08 05:15:15 +00:00
|
|
|
response = {
|
|
|
|
'error_type': 'PASSWORDS_DONT_MATCH',
|
|
|
|
'error_message': 'Passwords do not match.',
|
|
|
|
}
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=422)
|
2016-12-18 13:12:14 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
user = P.register_user(username, password_1)
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.EtiquetteException as e:
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=400)
|
2016-12-18 13:12:14 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
session = etiquette_flask.sessions.Session(request, user)
|
2016-12-18 13:12:14 +00:00
|
|
|
session_manager.add(session)
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response({})
|
2016-12-18 13:12:14 +00:00
|
|
|
|
|
|
|
@site.route('/logout', methods=['GET', 'POST'])
|
|
|
|
@session_manager.give_token
|
|
|
|
def logout():
|
|
|
|
session_manager.remove(request)
|
|
|
|
response = flask.Response('redirect', status=302, headers={'Location': back_url()})
|
|
|
|
return response
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
@site.route('/favicon.ico')
|
|
|
|
@site.route('/favicon.png')
|
|
|
|
def favicon():
|
2017-05-02 04:23:16 +00:00
|
|
|
return flask.send_file(FAVICON_PATH.absolute_path)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_album_core(albumid):
|
2016-09-18 08:33:46 +00:00
|
|
|
album = P_album(albumid)
|
2016-10-10 03:50:13 +00:00
|
|
|
return album
|
|
|
|
|
|
|
|
@site.route('/album/<albumid>')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_album_html(albumid):
|
|
|
|
album = get_album_core(albumid)
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
2016-09-18 08:33:46 +00:00
|
|
|
response = flask.render_template(
|
|
|
|
'album.html',
|
|
|
|
album=album,
|
2016-12-18 13:12:14 +00:00
|
|
|
session=session,
|
2016-11-27 09:06:11 +00:00
|
|
|
view=request.args.get('view', 'grid'),
|
2016-09-18 08:33:46 +00:00
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2016-10-21 03:15:24 +00:00
|
|
|
@site.route('/album/<albumid>.json')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_album_json(albumid):
|
|
|
|
album = get_album_core(albumid)
|
2017-05-02 04:23:16 +00:00
|
|
|
album = etiquette.jsonify.album(album)
|
2016-12-21 00:33:40 +00:00
|
|
|
album['sub_albums'] = [P_album(x) for x in album['sub_albums']]
|
|
|
|
album['sub_albums'].sort(key=lambda x: (x.title or x.id).lower())
|
2017-05-02 04:23:16 +00:00
|
|
|
album['sub_albums'] = [etiquette.jsonify.album(x, minimal=True) for x in album['sub_albums']]
|
|
|
|
return etiquette_flask.jsonify.make_json_response(album)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2016-12-21 01:44:22 +00:00
|
|
|
@site.route('/album/<albumid>.zip')
|
|
|
|
def get_album_zip(albumid):
|
2016-11-06 00:58:37 +00:00
|
|
|
album = P_album(albumid)
|
2016-12-21 01:44:22 +00:00
|
|
|
|
|
|
|
recursive = request.args.get('recursive', True)
|
2017-05-02 04:23:16 +00:00
|
|
|
recursive = etiquette.helpers.truthystring(recursive)
|
2017-02-05 02:30:02 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
arcnames = etiquette.helpers.album_zip_filenames(album, recursive=recursive)
|
2016-12-21 01:44:22 +00:00
|
|
|
|
|
|
|
streamed_zip = zipstream.ZipFile()
|
|
|
|
for (real_filepath, arcname) in arcnames.items():
|
|
|
|
streamed_zip.write(real_filepath, arcname=arcname)
|
|
|
|
|
2017-02-05 02:30:02 +00:00
|
|
|
# Add the album metadata as an {id}.txt file within each directory.
|
2017-05-02 04:23:16 +00:00
|
|
|
directories = etiquette.helpers.album_zip_directories(album, recursive=recursive)
|
2016-12-21 03:53:06 +00:00
|
|
|
for (inner_album, directory) in directories.items():
|
|
|
|
text = []
|
|
|
|
if inner_album.title:
|
2017-03-04 07:44:43 +00:00
|
|
|
text.append('Title: ' + inner_album.title)
|
2016-12-21 03:53:06 +00:00
|
|
|
if inner_album.description:
|
2017-03-04 07:44:43 +00:00
|
|
|
text.append('Description: ' + inner_album.description)
|
2016-12-21 03:53:06 +00:00
|
|
|
if not text:
|
|
|
|
continue
|
|
|
|
text = '\r\n\r\n'.join(text)
|
|
|
|
streamed_zip.writestr(
|
2017-03-04 07:44:43 +00:00
|
|
|
arcname=os.path.join(directory, 'album %s.txt' % inner_album.id),
|
2016-12-21 03:53:06 +00:00
|
|
|
data=text.encode('utf-8'),
|
|
|
|
)
|
2016-12-21 01:44:22 +00:00
|
|
|
|
|
|
|
if album.title:
|
2017-03-04 07:44:43 +00:00
|
|
|
download_as = 'album %s - %s.zip' % (album.id, album.title)
|
2016-12-21 01:44:22 +00:00
|
|
|
else:
|
2017-03-04 07:44:43 +00:00
|
|
|
download_as = 'album %s.zip' % album.id
|
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
download_as = etiquette.helpers.normalize_filepath(download_as)
|
2016-12-25 02:34:34 +00:00
|
|
|
download_as = urllib.parse.quote(download_as)
|
2016-12-21 01:44:22 +00:00
|
|
|
outgoing_headers = {
|
|
|
|
'Content-Type': 'application/octet-stream',
|
2016-12-25 02:34:34 +00:00
|
|
|
'Content-Disposition': 'attachment; filename*=UTF-8\'\'%s' % download_as,
|
2016-12-21 01:44:22 +00:00
|
|
|
|
|
|
|
}
|
2016-11-06 00:58:37 +00:00
|
|
|
return flask.Response(streamed_zip, headers=outgoing_headers)
|
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
|
|
|
def get_albums_core():
|
2017-03-20 01:48:41 +00:00
|
|
|
albums = list(P.get_root_albums())
|
2017-03-11 01:08:38 +00:00
|
|
|
albums.sort(key=lambda x: x.display_name.lower())
|
2016-11-06 04:24:43 +00:00
|
|
|
return albums
|
|
|
|
|
|
|
|
@site.route('/albums')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-11-06 04:24:43 +00:00
|
|
|
def get_albums_html():
|
|
|
|
albums = get_albums_core()
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
|
|
|
return flask.render_template('albums.html', albums=albums, session=session)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-10-21 03:15:24 +00:00
|
|
|
@site.route('/albums.json')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-21 03:15:24 +00:00
|
|
|
def get_albums_json():
|
2016-11-06 04:24:43 +00:00
|
|
|
albums = get_albums_core()
|
2017-05-02 04:23:16 +00:00
|
|
|
albums = [etiquette.jsonify.album(album, minimal=True) for album in albums]
|
|
|
|
return etiquette_flask.jsonify.make_json_response(albums)
|
2016-10-21 03:15:24 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2016-11-29 04:18:44 +00:00
|
|
|
@site.route('/bookmarks')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-11-29 04:18:44 +00:00
|
|
|
def get_bookmarks():
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
2017-02-05 02:30:02 +00:00
|
|
|
bookmarks = list(P.get_bookmarks())
|
|
|
|
return flask.render_template('bookmarks.html', bookmarks=bookmarks, session=session)
|
2016-11-29 04:18:44 +00:00
|
|
|
|
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
@site.route('/file/<photoid>')
|
|
|
|
def get_file(photoid):
|
|
|
|
photoid = photoid.split('.')[0]
|
|
|
|
photo = P.get_photo(photoid)
|
|
|
|
|
|
|
|
do_download = request.args.get('download', False)
|
2017-05-02 04:23:16 +00:00
|
|
|
do_download = etiquette.helpers.truthystring(do_download)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
|
|
|
use_original_filename = request.args.get('original_filename', False)
|
2017-05-02 04:23:16 +00:00
|
|
|
use_original_filename = etiquette.helpers.truthystring(use_original_filename)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
|
|
|
if do_download:
|
|
|
|
if use_original_filename:
|
|
|
|
download_as = photo.basename
|
|
|
|
else:
|
2016-12-25 02:34:34 +00:00
|
|
|
download_as = photo.id + photo.dot_extension
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
download_as = etiquette.helpers.normalize_filepath(download_as)
|
2016-12-25 02:34:34 +00:00
|
|
|
download_as = urllib.parse.quote(download_as)
|
2016-09-18 08:33:46 +00:00
|
|
|
response = flask.make_response(send_file(photo.real_filepath))
|
2016-12-25 02:34:34 +00:00
|
|
|
response.headers['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'%s' % download_as
|
2016-09-18 08:33:46 +00:00
|
|
|
return response
|
|
|
|
else:
|
2017-02-28 07:05:43 +00:00
|
|
|
return send_file(photo.real_filepath, override_mimetype=photo.mimetype)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
@site.route('/photo/<photoid>', methods=['GET'])
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_photo_html(photoid):
|
2017-03-05 05:56:23 +00:00
|
|
|
photo = P_photo(photoid, response_type='html')
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
|
|
|
return flask.render_template('photo.html', photo=photo, session=session)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
|
|
|
@site.route('/photo/<photoid>.json', methods=['GET'])
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_photo_json(photoid):
|
2017-03-05 05:56:23 +00:00
|
|
|
photo = P_photo(photoid, response_type='json')
|
2017-05-02 04:23:16 +00:00
|
|
|
photo = etiquette.jsonify.photo(photo)
|
|
|
|
photo = etiquette_flask.jsonify.make_json_response(photo)
|
2016-10-10 03:50:13 +00:00
|
|
|
return photo
|
|
|
|
|
|
|
|
def get_search_core():
|
2017-05-02 04:23:16 +00:00
|
|
|
warning_bag = etiquette.objects.WarningBag()
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
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')
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
filename_terms = request.args.get('filename')
|
|
|
|
extension = request.args.get('extension')
|
|
|
|
extension_not = request.args.get('extension_not')
|
|
|
|
mimetype = request.args.get('mimetype')
|
2016-12-24 03:49:51 +00:00
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
limit = request.args.get('limit')
|
|
|
|
# This is being pre-processed because the site enforces a maximum value
|
|
|
|
# which the PhotoDB api does not.
|
2017-05-02 04:23:16 +00:00
|
|
|
limit = etiquette.searchhelpers.normalize_limit(limit, warning_bag=warning_bag)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
if limit is None:
|
|
|
|
limit = 50
|
2016-12-24 03:49:51 +00:00
|
|
|
else:
|
2016-12-25 01:13:45 +00:00
|
|
|
limit = min(limit, 100)
|
|
|
|
|
|
|
|
offset = request.args.get('offset')
|
|
|
|
|
|
|
|
authors = request.args.get('author')
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
orderby = request.args.get('orderby')
|
|
|
|
area = request.args.get('area')
|
|
|
|
width = request.args.get('width')
|
|
|
|
height = request.args.get('height')
|
|
|
|
ratio = request.args.get('ratio')
|
|
|
|
bytes = request.args.get('bytes')
|
|
|
|
duration = request.args.get('duration')
|
|
|
|
created = request.args.get('created')
|
2016-09-18 08:33:46 +00:00
|
|
|
|
|
|
|
# These are in a dictionary so I can pass them to the page template.
|
|
|
|
search_kwargs = {
|
|
|
|
'area': area,
|
|
|
|
'width': width,
|
|
|
|
'height': height,
|
|
|
|
'ratio': ratio,
|
|
|
|
'bytes': bytes,
|
2016-10-10 03:50:13 +00:00
|
|
|
'duration': duration,
|
2016-09-18 08:33:46 +00:00
|
|
|
|
2016-12-24 03:49:51 +00:00
|
|
|
'authors': authors,
|
2016-09-18 08:33:46 +00:00
|
|
|
'created': created,
|
2016-12-25 01:13:45 +00:00
|
|
|
'extension': extension,
|
|
|
|
'extension_not': extension_not,
|
2016-10-30 01:46:23 +00:00
|
|
|
'filename': filename_terms,
|
2016-09-18 08:33:46 +00:00
|
|
|
'has_tags': has_tags,
|
2016-12-25 01:13:45 +00:00
|
|
|
'mimetype': mimetype,
|
2016-09-18 08:33:46 +00:00
|
|
|
'tag_musts': tag_musts,
|
|
|
|
'tag_mays': tag_mays,
|
|
|
|
'tag_forbids': tag_forbids,
|
|
|
|
'tag_expression': tag_expression,
|
|
|
|
|
|
|
|
'limit': limit,
|
|
|
|
'offset': offset,
|
|
|
|
'orderby': orderby,
|
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
'warning_bag': warning_bag,
|
|
|
|
'give_back_parameters': True
|
2016-09-18 08:33:46 +00:00
|
|
|
}
|
2016-11-06 00:58:37 +00:00
|
|
|
#print(search_kwargs)
|
2016-12-25 01:13:45 +00:00
|
|
|
search_generator = P.search(**search_kwargs)
|
|
|
|
# Because of the giveback, first element is cleaned up kwargs
|
|
|
|
search_kwargs = next(search_generator)
|
|
|
|
|
|
|
|
# The search has converted many arguments into sets or other types.
|
|
|
|
# Convert them back into something that will display nicely on the search form.
|
|
|
|
join_helper = lambda x: ', '.join(x) if x else None
|
|
|
|
search_kwargs['extension'] = join_helper(search_kwargs['extension'])
|
|
|
|
search_kwargs['extension_not'] = join_helper(search_kwargs['extension_not'])
|
|
|
|
search_kwargs['mimetype'] = join_helper(search_kwargs['mimetype'])
|
2017-03-04 05:15:31 +00:00
|
|
|
|
|
|
|
tagname_helper = lambda tags: [tag.qualified_name() for tag in tags] if tags else None
|
2016-12-25 01:13:45 +00:00
|
|
|
search_kwargs['tag_musts'] = tagname_helper(search_kwargs['tag_musts'])
|
|
|
|
search_kwargs['tag_mays'] = tagname_helper(search_kwargs['tag_mays'])
|
|
|
|
search_kwargs['tag_forbids'] = tagname_helper(search_kwargs['tag_forbids'])
|
|
|
|
|
2017-03-04 05:15:31 +00:00
|
|
|
#quoted_helper = lambda text: '"%s"' % text if ' ' in text else text
|
|
|
|
#filename_helper = lambda fn: ' '.join(quoted_helper(part) for part in fn) if fn else None
|
|
|
|
#search_kwargs['filename'] = filename_helper(search_kwargs['filename'])
|
|
|
|
|
2016-12-25 01:13:45 +00:00
|
|
|
search_results = list(search_generator)
|
|
|
|
warnings = set()
|
|
|
|
photos = []
|
|
|
|
for item in search_results:
|
2017-05-02 04:23:16 +00:00
|
|
|
if isinstance(item, etiquette.objects.WarningBag):
|
2016-12-25 01:13:45 +00:00
|
|
|
warnings.update(item.warnings)
|
|
|
|
else:
|
|
|
|
photos.append(item)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
|
|
|
# TAGS ON THIS PAGE
|
2016-09-18 08:33:46 +00:00
|
|
|
total_tags = set()
|
|
|
|
for photo in photos:
|
2016-12-21 00:33:40 +00:00
|
|
|
for tag in photo.tags():
|
|
|
|
total_tags.add(tag.qualified_name())
|
2016-10-10 03:50:13 +00:00
|
|
|
total_tags = sorted(total_tags)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
|
|
|
# PREV-NEXT PAGE URLS
|
2016-12-25 02:34:34 +00:00
|
|
|
offset = search_kwargs['offset'] or 0
|
2016-11-06 04:24:43 +00:00
|
|
|
original_params = request.args.to_dict()
|
2017-03-04 06:11:29 +00:00
|
|
|
original_params['limit'] = limit
|
2016-09-18 08:33:46 +00:00
|
|
|
if len(photos) == limit:
|
2017-03-04 06:11:29 +00:00
|
|
|
next_params = original_params.copy()
|
|
|
|
next_params['offset'] = offset + limit
|
2017-05-02 04:23:16 +00:00
|
|
|
next_params = etiquette.helpers.dict_to_params(next_params)
|
2016-09-18 08:33:46 +00:00
|
|
|
next_page_url = '/search' + next_params
|
|
|
|
else:
|
|
|
|
next_page_url = None
|
2017-03-04 06:11:29 +00:00
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
if offset > 0:
|
2017-03-04 06:11:29 +00:00
|
|
|
prev_params = original_params.copy()
|
|
|
|
prev_params['offset'] = max(0, offset - limit)
|
2017-05-02 04:23:16 +00:00
|
|
|
prev_params = etiquette.helpers.dict_to_params(prev_params)
|
2016-09-18 08:33:46 +00:00
|
|
|
prev_page_url = '/search' + prev_params
|
|
|
|
else:
|
|
|
|
prev_page_url = None
|
|
|
|
|
2016-11-07 02:00:30 +00:00
|
|
|
view = request.args.get('view', 'grid')
|
|
|
|
search_kwargs['view'] = view
|
2016-10-10 03:50:13 +00:00
|
|
|
|
|
|
|
final_results = {
|
|
|
|
'next_page_url': next_page_url,
|
|
|
|
'prev_page_url': prev_page_url,
|
|
|
|
'photos': photos,
|
|
|
|
'total_tags': total_tags,
|
2016-12-25 01:13:45 +00:00
|
|
|
'warnings': list(warnings),
|
2016-10-10 03:50:13 +00:00
|
|
|
'search_kwargs': search_kwargs,
|
|
|
|
}
|
|
|
|
return final_results
|
|
|
|
|
|
|
|
@site.route('/search')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_search_html():
|
|
|
|
search_results = get_search_core()
|
|
|
|
search_kwargs = search_results['search_kwargs']
|
2017-05-02 04:23:16 +00:00
|
|
|
qualname_map = P.export_tags(exporter=etiquette.photodb.tag_export_qualname_map)
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
2016-09-18 08:33:46 +00:00
|
|
|
response = flask.render_template(
|
|
|
|
'search.html',
|
2016-10-10 03:50:13 +00:00
|
|
|
next_page_url=search_results['next_page_url'],
|
|
|
|
prev_page_url=search_results['prev_page_url'],
|
|
|
|
photos=search_results['photos'],
|
2016-09-18 08:33:46 +00:00
|
|
|
qualname_map=json.dumps(qualname_map),
|
2016-10-10 03:50:13 +00:00
|
|
|
search_kwargs=search_kwargs,
|
2016-12-18 13:12:14 +00:00
|
|
|
session=session,
|
2016-10-10 03:50:13 +00:00
|
|
|
total_tags=search_results['total_tags'],
|
2016-12-25 01:13:45 +00:00
|
|
|
warnings=search_results['warnings'],
|
2016-09-18 08:33:46 +00:00
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
@site.route('/search.json')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-10-10 03:50:13 +00:00
|
|
|
def get_search_json():
|
|
|
|
search_results = get_search_core()
|
2017-03-15 04:18:42 +00:00
|
|
|
search_results['photos'] = [
|
2017-05-02 04:23:16 +00:00
|
|
|
etiquette.jsonify.photo(photo, include_albums=False) for photo in search_results['photos']
|
2017-03-15 04:18:42 +00:00
|
|
|
]
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response(search_results)
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
def get_tags_core(specific_tag=None):
|
2017-03-05 03:59:16 +00:00
|
|
|
if specific_tag is None:
|
|
|
|
tags = P.get_tags()
|
|
|
|
else:
|
|
|
|
tags = specific_tag.walk_children()
|
|
|
|
tags = list(tags)
|
2017-03-10 13:18:49 +00:00
|
|
|
tags.sort(key=lambda x: x.qualified_name())
|
2016-11-06 04:24:43 +00:00
|
|
|
return tags
|
2016-12-17 04:02:08 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
@site.route('/tags')
|
|
|
|
@site.route('/tags/<specific_tag>')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-11-06 04:24:43 +00:00
|
|
|
def get_tags_html(specific_tag=None):
|
2017-03-05 03:59:16 +00:00
|
|
|
if specific_tag is not None:
|
|
|
|
specific_tag = P_tag(specific_tag, response_type='html')
|
2016-11-06 04:24:43 +00:00
|
|
|
tags = get_tags_core(specific_tag)
|
2016-12-18 13:12:14 +00:00
|
|
|
session = session_manager.get(request)
|
2017-04-23 04:12:43 +00:00
|
|
|
include_synonyms = request.args.get('synonyms')
|
2017-05-02 04:23:16 +00:00
|
|
|
include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms)
|
2017-04-23 04:12:43 +00:00
|
|
|
response = flask.render_template(
|
|
|
|
'tags.html',
|
|
|
|
include_synonyms=include_synonyms,
|
|
|
|
session=session,
|
|
|
|
tags=tags,
|
|
|
|
)
|
|
|
|
return response
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
@site.route('/tags.json')
|
|
|
|
@site.route('/tags/<specific_tag>.json')
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2016-11-06 04:24:43 +00:00
|
|
|
def get_tags_json(specific_tag=None):
|
2017-03-05 03:59:16 +00:00
|
|
|
if specific_tag is not None:
|
|
|
|
specific_tag = P_tag(specific_tag, response_type='json')
|
2016-11-06 04:24:43 +00:00
|
|
|
tags = get_tags_core(specific_tag)
|
2017-04-23 04:12:43 +00:00
|
|
|
include_synonyms = request.args.get('synonyms')
|
2017-05-02 04:23:16 +00:00
|
|
|
include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms)
|
|
|
|
tags = [etiquette.jsonify.tag(tag, include_synonyms=include_synonyms) for tag in tags]
|
|
|
|
return etiquette_flask.jsonify.make_json_response(tags)
|
2016-11-06 04:24:43 +00:00
|
|
|
|
|
|
|
|
2016-10-10 03:50:13 +00:00
|
|
|
@site.route('/thumbnail/<photoid>')
|
|
|
|
def get_thumbnail(photoid):
|
|
|
|
photoid = photoid.split('.')[0]
|
|
|
|
photo = P_photo(photoid)
|
|
|
|
if photo.thumbnail:
|
|
|
|
path = photo.thumbnail
|
|
|
|
else:
|
|
|
|
flask.abort(404, 'That file doesnt have a thumbnail')
|
|
|
|
return send_file(path)
|
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2017-02-05 02:30:02 +00:00
|
|
|
def get_user_core(username):
|
|
|
|
user = P_user(username)
|
|
|
|
return user
|
|
|
|
|
|
|
|
@site.route('/user/<username>', methods=['GET'])
|
|
|
|
@session_manager.give_token
|
|
|
|
def get_user_html(username):
|
|
|
|
user = get_user_core(username)
|
|
|
|
session = session_manager.get(request)
|
|
|
|
return flask.render_template('user.html', user=user, session=session)
|
|
|
|
|
|
|
|
@site.route('/user/<username>.json', methods=['GET'])
|
|
|
|
@session_manager.give_token
|
|
|
|
def get_user_json(username):
|
|
|
|
user = get_user_core(username)
|
2017-05-02 04:23:16 +00:00
|
|
|
user = etiquette.jsonify.user(user)
|
|
|
|
user = etiquette_flask.jsonify.make_json_response(user)
|
2017-02-05 02:30:02 +00:00
|
|
|
return user
|
|
|
|
|
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
@site.route('/album/<albumid>/add_tag', methods=['POST'])
|
2016-12-18 13:12:14 +00:00
|
|
|
@session_manager.give_token
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_album_add_tag(albumid):
|
2016-10-18 05:13:12 +00:00
|
|
|
'''
|
|
|
|
Apply a tag to every photo in the album.
|
|
|
|
'''
|
2016-10-21 03:15:24 +00:00
|
|
|
response = {}
|
|
|
|
album = P_album(albumid)
|
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
tag = request.form['tagname'].strip()
|
2016-10-10 03:50:13 +00:00
|
|
|
try:
|
2017-02-26 08:33:26 +00:00
|
|
|
tag = P_tag(tag)
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.NoSuchTag as e:
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=404)
|
2017-02-26 08:33:26 +00:00
|
|
|
recursive = request.form.get('recursive', False)
|
2017-05-02 04:23:16 +00:00
|
|
|
recursive = etiquette.helpers.truthystring(recursive)
|
2017-02-26 08:33:26 +00:00
|
|
|
album.add_tag_to_all(tag, nested_children=recursive)
|
|
|
|
response['action'] = 'add_tag'
|
2016-10-10 03:50:13 +00:00
|
|
|
response['tagname'] = tag.name
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response(response)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
2017-04-23 04:38:23 +00:00
|
|
|
@site.route('/album/<albumid>/edit', methods=['POST'])
|
|
|
|
@session_manager.give_token
|
|
|
|
def post_album_edit(albumid):
|
|
|
|
'''
|
|
|
|
Edit the title / description.
|
|
|
|
'''
|
|
|
|
album = P_album(albumid)
|
|
|
|
|
|
|
|
title = request.form.get('title', None)
|
|
|
|
description = request.form.get('description', None)
|
|
|
|
album.edit(title=title, description=description)
|
|
|
|
response = {'title': album.title, 'description': album.description}
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response(response)
|
2017-04-23 04:38:23 +00:00
|
|
|
|
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_photo_add_remove_tag_core(photoid, tagname, add_or_remove):
|
|
|
|
photo = P_photo(photoid, response_type='json')
|
|
|
|
tag = P_tag(tagname, response_type='json')
|
|
|
|
|
2017-03-17 07:10:33 +00:00
|
|
|
try:
|
|
|
|
if add_or_remove == 'add':
|
|
|
|
photo.add_tag(tag)
|
|
|
|
elif add_or_remove == 'remove':
|
|
|
|
photo.remove_tag(tag)
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.EtiquetteException as e:
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
response = etiquette_flask.jsonify.make_json_response(response, status=400)
|
2017-03-17 07:10:33 +00:00
|
|
|
flask.abort(response)
|
2017-02-26 08:33:26 +00:00
|
|
|
|
2017-03-10 22:04:50 +00:00
|
|
|
response = {'tagname': tag.name}
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response(response)
|
2017-02-26 08:33:26 +00:00
|
|
|
|
|
|
|
@site.route('/photo/<photoid>/add_tag', methods=['POST'])
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['tagname'], forbid_whitespace=True)
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_photo_add_tag(photoid):
|
|
|
|
'''
|
|
|
|
Add a tag to this photo.
|
2016-10-18 05:13:12 +00:00
|
|
|
'''
|
2017-02-26 08:33:26 +00:00
|
|
|
return post_photo_add_remove_tag_core(photoid, request.form['tagname'], 'add')
|
|
|
|
|
|
|
|
@site.route('/photo/<photoid>/remove_tag', methods=['POST'])
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['tagname'], forbid_whitespace=True)
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_photo_remove_tag(photoid):
|
|
|
|
'''
|
|
|
|
Remove a tag from this photo.
|
2016-10-18 05:13:12 +00:00
|
|
|
'''
|
2017-02-26 08:33:26 +00:00
|
|
|
return post_photo_add_remove_tag_core(photoid, request.form['tagname'], 'remove')
|
|
|
|
|
2017-03-03 01:38:15 +00:00
|
|
|
@site.route('/photo/<photoid>/refresh_metadata', methods=['POST'])
|
|
|
|
def post_photo_refresh_metadata(photoid):
|
|
|
|
'''
|
|
|
|
Refresh the file metadata.
|
|
|
|
'''
|
2017-05-02 04:23:16 +00:00
|
|
|
P.caches['photo'].remove(photoid)
|
2017-03-03 01:38:15 +00:00
|
|
|
photo = P_photo(photoid, response_type='json')
|
2017-03-17 21:02:33 +00:00
|
|
|
try:
|
|
|
|
photo.reload_metadata()
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.EtiquetteException as e:
|
|
|
|
response = etiquette.jsonify.exception(e)
|
|
|
|
response = etiquette_flask.jsonify.make_json_response(response, status=400)
|
2017-03-17 21:02:33 +00:00
|
|
|
flask.abort(response)
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response({})
|
2017-02-26 08:33:26 +00:00
|
|
|
|
2017-03-05 06:27:24 +00:00
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_tag_create_delete_core(tagname, function):
|
|
|
|
try:
|
|
|
|
response = function(tagname)
|
|
|
|
status = 200
|
2017-05-02 04:23:16 +00:00
|
|
|
except etiquette.exceptions.EtiquetteException as e:
|
|
|
|
response = etiquette.jsonify.exception(e)
|
2017-02-26 09:04:07 +00:00
|
|
|
status = 400
|
2017-03-05 12:54:58 +00:00
|
|
|
#print(response)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2017-05-02 04:23:16 +00:00
|
|
|
return etiquette_flask.jsonify.make_json_response(response, status=status)
|
2016-10-18 05:13:12 +00:00
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
@site.route('/tags/create_tag', methods=['POST'])
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['tagname'], forbid_whitespace=True)
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_tag_create():
|
|
|
|
'''
|
|
|
|
Create a tag.
|
|
|
|
'''
|
|
|
|
return post_tag_create_delete_core(request.form['tagname'], create_tag)
|
2016-10-10 03:50:13 +00:00
|
|
|
|
2017-02-26 08:33:26 +00:00
|
|
|
@site.route('/tags/delete_tag', methods=['POST'])
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['tagname'], forbid_whitespace=True)
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_tag_delete():
|
|
|
|
'''
|
|
|
|
Delete a tag.
|
|
|
|
'''
|
|
|
|
return post_tag_create_delete_core(request.form['tagname'], delete_tag)
|
|
|
|
|
|
|
|
@site.route('/tags/delete_synonym', methods=['POST'])
|
2017-05-02 04:23:16 +00:00
|
|
|
@etiquette_flask.decorators.required_fields(['tagname'], forbid_whitespace=True)
|
2017-02-26 08:33:26 +00:00
|
|
|
def post_tag_delete_synonym():
|
|
|
|
'''
|
|
|
|
Delete a synonym.
|
|
|
|
'''
|
|
|
|
return post_tag_create_delete_core(request.form['tagname'], delete_synonym)
|
2016-09-18 08:33:46 +00:00
|
|
|
|
|
|
|
|
2016-12-18 13:12:14 +00:00
|
|
|
@site.route('/apitest')
|
|
|
|
@session_manager.give_token
|
|
|
|
def apitest():
|
|
|
|
response = flask.Response('testing')
|
|
|
|
response.set_cookie('etiquette_session', 'don\'t overwrite me')
|
|
|
|
return response
|
|
|
|
|
2016-09-18 08:33:46 +00:00
|
|
|
if __name__ == '__main__':
|
2016-11-27 09:06:11 +00:00
|
|
|
#site.run(threaded=True)
|
|
|
|
pass
|