diff --git a/hydrus/app.py b/hydrus/app.py index 7a7216e8..c855dae2 100644 --- a/hydrus/app.py +++ b/hydrus/app.py @@ -1,23 +1,30 @@ """Main route for the application""" import logging +import sys +from os.path import dirname, abspath +# insert the ./app.py file path in the PYTHONPATH variable for imports to work +sys.path.insert(0, dirname(dirname(abspath(__file__)))) -from sqlalchemy import create_engine +# ignoring import error by flake 8 temporarily +# for setting PYTHONPATH before imports +from sqlalchemy import create_engine # noqa: E402 +from flask import request # noqa: E402 +from collections import defaultdict # noqa: E402 +from gevent.pywsgi import WSGIServer # noqa: E402 +from sqlalchemy.orm import sessionmaker # noqa: E402 -from gevent.pywsgi import WSGIServer -from sqlalchemy.orm import sessionmaker - -from hydrus.app_factory import app_factory +from hydrus.app_factory import app_factory # noqa: E402 from hydrus.conf import ( - HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) -from hydrus.data import doc_parse -from hydrus.data.db_models import Base -from hydrus.data.exceptions import UserExists -from hydrus.data.user import add_user -from hydra_python_core import doc_maker + HYDRUS_SERVER_URL, API_NAME, DB_URL, APIDOC_OBJ, PORT, DEBUG) # noqa: E402 +from hydrus.data import doc_parse # noqa: E402 +from hydrus.data.db_models import Base # noqa: E402 +from hydrus.data.exceptions import UserExists # noqa: E402 +from hydrus.data.user import add_user # noqa: E402 +from hydra_python_core import doc_maker # noqa: E402 from hydrus.utils import ( set_session, set_doc, set_hydrus_server_url, - set_token, set_api_name, set_authentication) + set_token, set_api_name, set_authentication) # noqa: E402 logger = logging.getLogger(__file__) @@ -42,8 +49,8 @@ doc_parse.insert_classes(classes, session) doc_parse.insert_properties(properties, session) -AUTH = True -TOKEN = True +AUTH = False +TOKEN = False if AUTH: try: @@ -54,6 +61,41 @@ # Create a Hydrus app app = app_factory(API_NAME) +# global dict to store mapping of each function to be run before +# specified route. +# stores in the form {'path1': {'method1': function_before_path1_for_method1}} +before_request_funcs = defaultdict(dict) + + +@app.before_request +def before_request_callback(): + path = request.path + method = request.method + global before_request_funcs + func = before_request_funcs.get(path, {}).get(method, None) + if func: + func() + + +# decorator to define logic for custom before request methods +def custom_before_request(path, method): + def wrapper(f): + global before_request_funcs + before_request_funcs[path][method] = f + return f + return wrapper + + +@custom_before_request('/api/MessageCollection', 'PUT') +def do_this_before_put_on_drone_collections(): + print("Do something before PUT request on MessageCollection") + + +@custom_before_request('/api/MessageCollection', 'GET') +def do_this_before_get_on_drone_collections(): + print("Do something before GET request on MessageCollection") + + with set_authentication(app, AUTH): # Use authentication for all requests with set_token(app, TOKEN):