From 708bf238704b78b1b607bad2c2563dbe8fd99ffa Mon Sep 17 00:00:00 2001 From: AndroidDrew Date: Fri, 10 Nov 2017 12:16:44 -0500 Subject: [PATCH] Added default application settings in application factory --- cookie_api/app.py | 12 +++++++++++- cookie_api/schema.py | 0 wsgi.py | 35 +++++++++++++++++++++++++++++++++-- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 cookie_api/schema.py diff --git a/cookie_api/app.py b/cookie_api/app.py index ae96349..9b5837e 100644 --- a/cookie_api/app.py +++ b/cookie_api/app.py @@ -46,6 +46,14 @@ def create_cookie(session: Session, json_data: http.RequestData, route: Router): return http.Response(cookie.to_dict(), status=201, headers=headers) +def delete_cookie(session: Session, id: int): + cookie = session.query(Cookie).filter_by(id=id).one_or_none() + if cookie is None: + msg = {"error": "404 Not Found"} + return http.Response(msg, status=404) + session.delete(cookie) + return {"message": "200 OK"} + routes = [ Route('/state', 'GET', get_state), Route('/cookies', 'GET', get_cookies), @@ -55,6 +63,8 @@ routes = [ Include('/static', static_urls) ] +app_settings = {} + routes = routes + auth_routes commands = sqlalchemy_backend.commands + commands @@ -64,7 +74,7 @@ components = sqlalchemy_backend.components + auth_components def application_factory(settings={}): """Returns an instance of Cookie API""" - return App(settings=settings, + return App(settings={**app_settings, **settings}, commands=commands, components=components, routes=routes) diff --git a/cookie_api/schema.py b/cookie_api/schema.py new file mode 100644 index 0000000..e69de29 diff --git a/wsgi.py b/wsgi.py index 2f46235..0110410 100644 --- a/wsgi.py +++ b/wsgi.py @@ -1,6 +1,7 @@ +import os +import sys from cookie_api import JSONRenderer, Base, application_factory - settings = { 'DATABASE': { 'URL': 'postgresql://apistar:local@localhost/apistar', @@ -12,6 +13,36 @@ settings = { } } + +def prepare_import(path): + """Given a filename this will try to calculate the python path, add it + to the search path and return the actual module name that is expected. + """ + path = os.path.realpath(path) + + if os.path.splitext(path)[1] == '.py': + path = os.path.splitext(path)[0] + + if os.path.basename(path) == '__init__': + path = os.path.dirname(path) + + module_name = [] + + # move up until outside package structure (no __init__.py) + while True: + path, name = os.path.split(path) + module_name.append(name) + + if not os.path.exists(os.path.join(path, '__init__.py')): + break + + if sys.path[0] != path: + sys.path.insert(0, path) + + return '.'.join(module_name[::-1]) + + +app = application_factory(settings) + if __name__ == "__main__": - app = application_factory(settings) app.main()