Move make_json_response to voussoirkit.flasktools.

master
voussoir 2021-06-04 21:49:45 -07:00
parent adb691405c
commit f003f55fca
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
8 changed files with 70 additions and 70 deletions

View File

@ -107,7 +107,7 @@ def P_wrapper(function):
flask.abort(status, exc.error_message)
else:
response = exc.jsonify()
response = jsonify.make_json_response(response, status=status)
response = flasktools.make_json_response(response, status=status)
flask.abort(response)
except Exception as exc:
@ -115,7 +115,7 @@ def P_wrapper(function):
if response_type == 'html':
flask.abort(500)
else:
flask.abort(jsonify.make_json_response({}, status=500))
flask.abort(flasktools.make_json_response({}, status=500))
return P_wrapped

View File

@ -105,7 +105,7 @@ def catch_etiquette_exception(function):
else:
status = 400
response = exc.jsonify()
response = jsonify.make_json_response(response, status=status)
response = flasktools.make_json_response(response, status=status)
flask.abort(response)
return wrapped
@ -153,7 +153,7 @@ def required_fields(fields, forbid_whitespace=False):
'error_type': 'MISSING_FIELDS',
'error_message': 'Required fields: %s' % ', '.join(fields),
}
response = jsonify.make_json_response(response, status=400)
response = flasktools.make_json_response(response, status=400)
return response
return function(*args, **kwargs)

View File

@ -3,6 +3,7 @@ import os
import time
import urllib.parse
from voussoirkit import flasktools
from voussoirkit import stringtools
import etiquette
@ -31,7 +32,7 @@ def get_album_html(album_id):
def get_album_json(album_id):
album = common.P_album(album_id, response_type='json')
album = album.jsonify()
return jsonify.make_json_response(album)
return flasktools.make_json_response(album)
@site.route('/album/<album_id>.zip')
def get_album_zip(album_id):
@ -66,7 +67,7 @@ def post_album_add_child(album_id):
print(children)
album.add_children(children, commit=True)
response = album.jsonify()
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/album/<album_id>/remove_child', methods=['POST'])
@decorators.required_fields(['child_id'], forbid_whitespace=True)
@ -77,14 +78,14 @@ def post_album_remove_child(album_id):
children = list(common.P_albums(child_ids, response_type='json'))
album.remove_children(children, commit=True)
response = album.jsonify()
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@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())
return flasktools.make_json_response(album.jsonify())
@site.route('/album/<album_id>/refresh_directories', methods=['POST'])
def post_album_refresh_directories(album_id):
@ -95,7 +96,7 @@ def post_album_refresh_directories(album_id):
digest = common.P.digest_directory(directory, new_photo_ratelimit=0.1)
etiquette.helpers.run_generator(digest)
common.P.commit(message='refresh album directories endpoint')
return jsonify.make_json_response({})
return flasktools.make_json_response({})
@site.route('/album/<album_id>/set_thumbnail_photo', methods=['POST'])
@decorators.required_fields(['photo_id'], forbid_whitespace=True)
@ -104,7 +105,7 @@ def post_album_set_thumbnail_photo(album_id):
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())
return flasktools.make_json_response(album.jsonify())
# Album photo operations ###########################################################################
@ -120,7 +121,7 @@ def post_album_add_photo(album_id):
photos = list(common.P_photos(photo_ids, response_type='json'))
album.add_photos(photos, commit=True)
response = album.jsonify()
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/album/<album_id>/remove_photo', methods=['POST'])
@decorators.required_fields(['photo_id'], forbid_whitespace=True)
@ -134,7 +135,7 @@ def post_album_remove_photo(album_id):
photos = list(common.P_photos(photo_ids, response_type='json'))
album.remove_photos(photos, commit=True)
response = album.jsonify()
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
# Album tag operations #############################################################################
@ -151,13 +152,13 @@ def post_album_add_tag(album_id):
tag = common.P_tag(tag, response_type='json')
except etiquette.exceptions.NoSuchTag as exc:
response = exc.jsonify()
return jsonify.make_json_response(response, status=404)
return flasktools.make_json_response(response, status=404)
recursive = request.form.get('recursive', False)
recursive = etiquette.helpers.truthystring(recursive)
album.add_tag_to_all(tag, nested_children=recursive, commit=True)
response['action'] = 'add_tag'
response['tagname'] = tag.name
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
# Album metadata operations ########################################################################
@ -172,7 +173,7 @@ def post_album_edit(album_id):
description = request.form.get('description', None)
album.edit(title=title, description=description, commit=True)
response = album.jsonify(minimal=True)
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/album/<album_id>/show_in_folder', methods=['POST'])
def post_album_show_in_folder(album_id):
@ -188,7 +189,7 @@ def post_album_show_in_folder(album_id):
if os.name == 'nt':
command = f'start explorer.exe "{directory.absolute_path}"'
os.system(command)
return jsonify.make_json_response({})
return flasktools.make_json_response({})
flask.abort(501)
@ -199,7 +200,7 @@ def post_album_show_in_folder(album_id):
def get_all_album_names():
all_albums = {album.id: album.display_name for album in common.P.get_albums()}
response = {'albums': all_albums}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
def get_albums_core():
albums = list(common.P.get_root_albums())
@ -221,7 +222,7 @@ def get_albums_html():
def get_albums_json():
albums = get_albums_core()
albums = [album.jsonify(minimal=True) for album in albums]
return jsonify.make_json_response(albums)
return flasktools.make_json_response(albums)
# Album create and delete ##########################################################################
@ -241,10 +242,10 @@ def post_albums_create():
common.P.commit('create album endpoint')
response = album.jsonify(minimal=False)
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/album/<album_id>/delete', methods=['POST'])
def post_album_delete(album_id):
album = common.P_album(album_id, response_type='json')
album.delete(commit=True)
return jsonify.make_json_response({})
return flasktools.make_json_response({})

View File

@ -1,5 +1,7 @@
import flask; from flask import request
from voussoirkit import flasktools
import etiquette
from .. import common
@ -15,7 +17,7 @@ session_manager = common.session_manager
def get_bookmark_json(bookmark_id):
bookmark = common.P_bookmark(bookmark_id, response_type='json')
response = bookmark.jsonify()
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/bookmark/<bookmark_id>/edit', methods=['POST'])
def post_bookmark_edit(bookmark_id):
@ -26,7 +28,7 @@ def post_bookmark_edit(bookmark_id):
bookmark.edit(title=title, url=url, commit=True)
response = bookmark.jsonify()
response = jsonify.make_json_response(response)
response = flasktools.make_json_response(response)
return response
# Bookmark listings ################################################################################
@ -39,7 +41,7 @@ def get_bookmarks_html():
@site.route('/bookmarks.json')
def get_bookmarks_json():
bookmarks = [b.jsonify() for b in common.P.get_bookmarks()]
return jsonify.make_json_response(bookmarks)
return flasktools.make_json_response(bookmarks)
# Bookmark create and delete #######################################################################
@ -51,11 +53,11 @@ def post_bookmark_create():
user = session_manager.get(request).user
bookmark = common.P.new_bookmark(url=url, title=title, author=user, commit=True)
response = bookmark.jsonify()
response = jsonify.make_json_response(response)
response = flasktools.make_json_response(response)
return response
@site.route('/bookmark/<bookmark_id>/delete', methods=['POST'])
def post_bookmark_delete(bookmark_id):
bookmark = common.P_bookmark(bookmark_id, response_type='json')
bookmark.delete(commit=True)
return jsonify.make_json_response({})
return flasktools.make_json_response({})

View File

@ -4,6 +4,7 @@ import traceback
import urllib.parse
from voussoirkit import cacheclass
from voussoirkit import flasktools
from voussoirkit import stringtools
import etiquette
@ -28,7 +29,7 @@ def get_photo_html(photo_id):
def get_photo_json(photo_id):
photo = common.P_photo(photo_id, response_type='json')
photo = photo.jsonify()
photo = jsonify.make_json_response(photo)
photo = flasktools.make_json_response(photo)
return photo
@site.route('/file/<photo_id>')
@ -76,7 +77,7 @@ def post_photo_delete(photo_id):
delete_file = request.form.get('delete_file', False)
delete_file = etiquette.helpers.truthystring(delete_file)
photo.delete(delete_file=delete_file, commit=True)
return jsonify.make_json_response({})
return flasktools.make_json_response({})
# Photo tag operations #############################################################################
@ -95,7 +96,7 @@ def post_photo_add_remove_tag_core(photo_ids, tagname, add_or_remove):
common.P.commit('photo add remove tag core')
response = {'action': add_or_remove, 'tagname': tag.name}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/photo/<photo_id>/add_tag', methods=['POST'])
@decorators.required_fields(['tagname'], forbid_whitespace=True)
@ -120,7 +121,7 @@ def post_photo_copy_tags(photo_id):
other = common.P_photo(request.form['other_photo'], response_type='json')
photo.copy_tags(other)
common.P.commit('photo copy tags')
return jsonify.make_json_response([tag.jsonify(minimal=True) for tag in photo.get_tags()])
return flasktools.make_json_response([tag.jsonify(minimal=True) for tag in photo.get_tags()])
@site.route('/photo/<photo_id>/remove_tag', methods=['POST'])
@decorators.required_fields(['tagname'], forbid_whitespace=True)
@ -165,7 +166,7 @@ def post_photo_generate_thumbnail(photo_id):
photo = common.P_photo(photo_id, response_type='json')
photo.generate_thumbnail(commit=True, **special)
response = jsonify.make_json_response({})
response = flasktools.make_json_response({})
return response
def post_photo_refresh_metadata_core(photo_ids):
@ -186,7 +187,7 @@ def post_photo_refresh_metadata_core(photo_ids):
common.P.commit('photo refresh metadata core')
return jsonify.make_json_response({})
return flasktools.make_json_response({})
@site.route('/photo/<photo_id>/refresh_metadata', methods=['POST'])
def post_photo_refresh_metadata(photo_id):
@ -203,13 +204,13 @@ def post_batch_photos_refresh_metadata():
def post_photo_set_searchhidden(photo_id):
photo = common.P_photo(photo_id, response_type='json')
photo.set_searchhidden(True)
return jsonify.make_json_response({})
return flasktools.make_json_response({})
@site.route('/photo/<photo_id>/unset_searchhidden', methods=['POST'])
def post_photo_unset_searchhidden(photo_id):
photo = common.P_photo(photo_id, response_type='json')
photo.set_searchhidden(False)
return jsonify.make_json_response({})
return flasktools.make_json_response({})
def post_batch_photos_searchhidden_core(photo_ids, searchhidden):
if isinstance(photo_ids, str):
@ -222,7 +223,7 @@ def post_batch_photos_searchhidden_core(photo_ids, searchhidden):
common.P.commit('photo set searchhidden core')
return jsonify.make_json_response({})
return flasktools.make_json_response({})
@site.route('/photo/<photo_id>/show_in_folder', methods=['POST'])
def post_photo_show_in_folder(photo_id):
@ -233,7 +234,7 @@ def post_photo_show_in_folder(photo_id):
if os.name == 'nt':
command = f'start explorer.exe /select,"{photo.real_path.absolute_path}"'
os.system(command)
return jsonify.make_json_response({})
return flasktools.make_json_response({})
flask.abort(501)
@ -269,7 +270,7 @@ def post_batch_photos():
photos = list(common.P_photos(photo_ids, response_type='json'))
photos = [photo.jsonify() for photo in photos]
response = jsonify.make_json_response(photos)
response = flasktools.make_json_response(photos)
return response
@site.route('/batch/photos/photo_card', methods=['POST'])
@ -295,7 +296,7 @@ def post_batch_photos_photo_cards():
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}
response = jsonify.make_json_response(divs)
response = flasktools.make_json_response(divs)
return response
# Zipping ##########################################################################################
@ -348,7 +349,7 @@ def post_batch_photos_download_zip():
photo_download_zip_tokens[zip_token] = photo_ids
response = {'zip_token': zip_token}
response = jsonify.make_json_response(response)
response = flasktools.make_json_response(response)
return response
# Search ###########################################################################################
@ -548,7 +549,7 @@ def get_search_json():
search_results['total_tags'] = [
tag.jsonify(minimal=True) for tag in search_results['total_tags']
]
return jsonify.make_json_response(search_results)
return flasktools.make_json_response(search_results)
# Swipe ############################################################################################

View File

@ -1,6 +1,8 @@
import flask; from flask import request
import time
from voussoirkit import flasktools
import etiquette
from .. import common
@ -40,7 +42,7 @@ def get_tag_json(specific_tag_name):
include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms)
response = specific_tag.jsonify(include_synonyms=include_synonyms)
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/tag/<tagname>/edit', methods=['POST'])
def post_tag_edit(tagname):
@ -53,7 +55,7 @@ def post_tag_edit(tagname):
tag.edit(description=description, commit=True)
response = tag.jsonify()
response = jsonify.make_json_response(response)
response = flasktools.make_json_response(response)
return response
@site.route('/tag/<tagname>/add_child', methods=['POST'])
@ -63,7 +65,7 @@ def post_tag_add_child(tagname):
child = common.P_tag(request.form['child_name'], response_type='json')
parent.add_child(child, commit=True)
response = {'action': 'add_child', 'tagname': f'{parent.name}.{child.name}'}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/tag/<tagname>/add_synonym', methods=['POST'])
@decorators.required_fields(['syn_name'], forbid_whitespace=True)
@ -74,7 +76,7 @@ def post_tag_add_synonym(tagname):
syn_name = master_tag.add_synonym(syn_name, commit=True)
response = {'action': 'add_synonym', 'synonym': syn_name}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/tag/<tagname>/remove_child', methods=['POST'])
@decorators.required_fields(['child_name'], forbid_whitespace=True)
@ -83,7 +85,7 @@ def post_tag_remove_child(tagname):
child = common.P_tag(request.form['child_name'], response_type='json')
parent.remove_child(child, commit=True)
response = {'action': 'remove_child', 'tagname': f'{parent.name}.{child.name}'}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/tag/<tagname>/remove_synonym', methods=['POST'])
@decorators.required_fields(['syn_name'], forbid_whitespace=True)
@ -94,7 +96,7 @@ def post_tag_remove_synonym(tagname):
syn_name = master_tag.remove_synonym(syn_name, commit=True)
response = {'action': 'delete_synonym', 'synonym': syn_name}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
# Tag listings #####################################################################################
@ -104,7 +106,7 @@ def get_all_tag_names():
all_tags = list(common.P.get_all_tag_names())
all_synonyms = common.P.get_all_synonyms()
response = {'tags': all_tags, 'synonyms': all_synonyms}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/tag/<specific_tag_name>')
@site.route('/tags')
@ -153,7 +155,7 @@ def get_tags_json():
tags = list(common.P.get_tags())
response = [tag.jsonify(include_synonyms=include_synonyms) for tag in tags]
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
# Tag create and delete ############################################################################
@ -165,7 +167,7 @@ def post_tag_create():
tag = common.P.new_tag(name, description, author=session_manager.get(request).user, commit=True)
response = tag.jsonify()
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)
@site.route('/tags/easybake', methods=['POST'])
@decorators.required_fields(['easybake_string'], forbid_whitespace=True)
@ -174,11 +176,11 @@ def post_tag_easybake():
notes = common.P.easybake(easybake_string, author=session_manager.get(request).user, commit=True)
notes = [{'action': action, 'tagname': tagname} for (action, tagname) in notes]
return jsonify.make_json_response(notes)
return flasktools.make_json_response(notes)
@site.route('/tag/<tagname>/delete', methods=['POST'])
def post_tag_delete(tagname):
tag = common.P_tag(tagname, response_type='json')
tag.delete(commit=True)
response = {'action': 'delete_tag', 'tagname': tag.name}
return jsonify.make_json_response(response)
return flasktools.make_json_response(response)

View File

@ -1,5 +1,7 @@
import flask; from flask import request
from voussoirkit import flasktools
import etiquette
from .. import common
@ -21,7 +23,7 @@ def get_user_html(username):
def get_user_json(username):
user = common.P_user(username, response_type='json')
user = user.jsonify()
return jsonify.make_json_response(user)
return flasktools.make_json_response(user)
@site.route('/userid/<user_id>')
@site.route('/userid/<user_id>.json')
@ -40,10 +42,10 @@ def post_user_edit(username):
session = session_manager.get(request)
if not session:
return jsonify.make_json_response(etiquette.exceptions.Unauthorized().jsonify(), status=403)
return flasktools.make_json_response(etiquette.exceptions.Unauthorized().jsonify(), status=403)
user = common.P_user(username, response_type='json')
if session.user != user:
return jsonify.make_json_response(etiquette.exceptions.Unauthorized().jsonify(), status=403)
return flasktools.make_json_response(etiquette.exceptions.Unauthorized().jsonify(), status=403)
display_name = request.form.get('display_name')
if display_name is not None:
@ -51,7 +53,7 @@ def post_user_edit(username):
common.P.commit()
return jsonify.make_json_response(user.jsonify())
return flasktools.make_json_response(user.jsonify())
# Login and logout #################################################################################
@ -72,7 +74,7 @@ def post_login():
if session.user:
exc = etiquette.exceptions.AlreadySignedIn()
response = exc.jsonify()
return jsonify.make_json_response(response, status=403)
return flasktools.make_json_response(response, status=403)
username = request.form['username']
password = request.form['password']
@ -85,18 +87,18 @@ def post_login():
except (etiquette.exceptions.NoSuchUser, etiquette.exceptions.WrongLogin):
exc = etiquette.exceptions.WrongLogin()
response = exc.jsonify()
return jsonify.make_json_response(response, status=422)
return flasktools.make_json_response(response, status=422)
except etiquette.exceptions.FeatureDisabled as exc:
response = exc.jsonify()
return jsonify.make_json_response(response, status=400)
return flasktools.make_json_response(response, status=400)
session = sessions.Session(request, user)
session_manager.add(session)
return jsonify.make_json_response({})
return flasktools.make_json_response({})
@site.route('/logout', methods=['POST'])
def logout():
session_manager.remove(request)
response = jsonify.make_json_response({})
response = flasktools.make_json_response({})
return response
# User registration ################################################################################
@ -112,7 +114,7 @@ def post_register():
if session.user:
exc = etiquette.exceptions.AlreadySignedIn()
response = exc.jsonify()
return jsonify.make_json_response(response, status=403)
return flasktools.make_json_response(response, status=403)
username = request.form['username']
display_name = request.form.get('display_name', None)
@ -124,10 +126,10 @@ def post_register():
'error_type': 'PASSWORDS_DONT_MATCH',
'error_message': 'Passwords do not match.',
}
return jsonify.make_json_response(response, status=422)
return flasktools.make_json_response(response, status=422)
user = common.P.new_user(username, password_1, display_name=display_name, commit=True)
session = sessions.Session(request, user)
session_manager.add(session)
return jsonify.make_json_response({})
return flasktools.make_json_response({})

View File

@ -1,8 +0,0 @@
import flask
import json
def make_json_response(j, *args, **kwargs):
dumped = json.dumps(j)
response = flask.Response(dumped, *args, **kwargs)
response.headers['Content-Type'] = 'application/json;charset=utf-8'
return response