gallery.accords-library.com/server/szurubooru/app.py

76 lines
2.9 KiB
Python
Raw Normal View History

''' Exports create_app. '''
import falcon
2016-04-18 20:41:39 +00:00
from szurubooru import api, errors, middleware
def _on_auth_error(ex, _request, _response, _params):
raise falcon.HTTPForbidden(
title='Authentication error', description=str(ex))
def _on_validation_error(ex, _request, _response, _params):
raise falcon.HTTPBadRequest(title='Validation error', description=str(ex))
def _on_search_error(ex, _request, _response, _params):
raise falcon.HTTPBadRequest(title='Search error', description=str(ex))
2016-04-02 12:40:10 +00:00
def _on_integrity_error(ex, _request, _response, _params):
raise falcon.HTTPConflict(
title='Integrity violation', description=ex.args[0])
def _on_not_found_error(ex, _request, _response, _params):
raise falcon.HTTPNotFound(title='Not found', description=str(ex))
2016-04-09 19:41:10 +00:00
def _on_processing_error(ex, _request, _response, _params):
2016-04-10 15:04:25 +00:00
raise falcon.HTTPBadRequest(title='Processing error', description=str(ex))
2016-04-09 19:41:10 +00:00
2016-04-19 10:40:08 +00:00
def create_method_not_allowed(allowed_methods):
allowed = ', '.join(allowed_methods)
def method_not_allowed(request, response, **_kwargs):
2016-04-19 10:40:08 +00:00
response.status = falcon.status_codes.HTTP_405
response.set_header('Allow', allowed)
request.context.output = {
'title': 'Method not allowed',
'description': 'Allowed methods: %r' % allowed_methods,
}
return method_not_allowed
def create_app():
2016-04-03 20:03:58 +00:00
''' Create a WSGI compatible App object. '''
2016-04-19 10:40:08 +00:00
falcon.responders.create_method_not_allowed = create_method_not_allowed
app = falcon.API(
request_type=api.Request,
middleware=[
2016-04-03 20:03:58 +00:00
middleware.RequireJson(),
middleware.ContextAdapter(),
2016-04-18 20:41:39 +00:00
middleware.DbSession(),
2016-04-03 20:03:58 +00:00
middleware.Authenticator(),
])
2016-04-03 20:03:58 +00:00
user_list_api = api.UserListApi()
user_detail_api = api.UserDetailApi()
tag_category_list_api = api.TagCategoryListApi()
tag_category_detail_api = api.TagCategoryDetailApi()
2016-04-15 21:02:30 +00:00
tag_list_api = api.TagListApi()
tag_detail_api = api.TagDetailApi()
2016-04-20 17:02:39 +00:00
tag_merging_api = api.TagMergingApi()
password_reset_api = api.PasswordResetApi()
2016-04-03 20:03:58 +00:00
app.add_error_handler(errors.AuthError, _on_auth_error)
app.add_error_handler(errors.IntegrityError, _on_integrity_error)
app.add_error_handler(errors.ValidationError, _on_validation_error)
app.add_error_handler(errors.SearchError, _on_search_error)
app.add_error_handler(errors.NotFoundError, _on_not_found_error)
2016-04-09 19:41:10 +00:00
app.add_error_handler(errors.ProcessingError, _on_processing_error)
2016-04-03 16:00:38 +00:00
app.add_route('/users/', user_list_api)
app.add_route('/user/{user_name}', user_detail_api)
app.add_route('/tag-categories/', tag_category_list_api)
app.add_route('/tag-category/{category_name}', tag_category_detail_api)
2016-04-15 21:02:30 +00:00
app.add_route('/tags/', tag_list_api)
app.add_route('/tag/{tag_name}', tag_detail_api)
2016-04-20 17:02:39 +00:00
app.add_route('/tag-merge/', tag_merging_api)
app.add_route('/password-reset/{user_name}', password_reset_api)
return app