Clean up and refactoring of code and style

I didn't do much to the functionality of the code, however I did make style changes that I believe will make the project more readable.
develop
androiddrew 7 years ago
parent c7d65e06d2
commit 49b0844d81

@ -1,2 +1,3 @@
include wsgi
recursive-include cookie_api/templates * recursive-include cookie_api/templates *
recursive-include cookie_api/static * recursive-include cookie_api/static *

@ -1,5 +1,4 @@
from apistar import Route from apistar import Route, App
from apistar.http import HTMLResponse
from apistar_jwt import JWT from apistar_jwt import JWT
from apistar_mail import MailComponent from apistar_mail import MailComponent
import logbook import logbook
@ -7,9 +6,9 @@ from sqlalchemy import create_engine
from pathlib import Path from pathlib import Path
from cookie_api.auth import auth_routes from cookie_api.auth import auth_routes
from cookie_api.logger import global_init from cookie_api.logger import init_logging
from cookie_api.resources import cookie_routes from cookie_api.resources import cookie_routes
from cookie_api.util import SQLAlchemyHook, SQLAlchemySession, MetaApp as App from cookie_api.util import SQLAlchemyHook, SQLAlchemySession
BASEDIR = Path(__file__).parent BASEDIR = Path(__file__).parent
TEMPLATE_DIR = BASEDIR.joinpath('templates') TEMPLATE_DIR = BASEDIR.joinpath('templates')
@ -17,18 +16,15 @@ STATIC_DIR = BASEDIR.joinpath('static')
logger = logbook.Logger(__name__) logger = logbook.Logger(__name__)
engine = create_engine('postgresql://apistar@localhost:5432/apistar')
app_settings = { app_settings = {
"LOGGING": { "LOGGING": {
"LEVEL": "DEBUG" "LEVEL": "INFO"
} }
} }
def return_index_html(): def return_index_html(app: App):
with open(STATIC_DIR.joinpath('index.html'), mode='rb') as f: return app.render_template('index.html')
return HTMLResponse(content=f.read())
index = Route('/', 'GET', return_index_html) index = Route('/', 'GET', return_index_html)
@ -38,7 +34,7 @@ _routes = cookie_routes + auth_routes + [index]
_hooks = [SQLAlchemyHook] _hooks = [SQLAlchemyHook]
_components = [ _components = [
SQLAlchemySession(engine=engine), SQLAlchemySession(engine=create_engine('postgresql://apistar@localhost:5432/apistar')),
JWT({ JWT({
'JWT_USER_ID': 'sub', 'JWT_USER_ID': 'sub',
'JWT_SECRET': 'thisisasecret', 'JWT_SECRET': 'thisisasecret',
@ -60,7 +56,8 @@ def application_factory(routes=_routes, components=_components, hooks=_hooks, se
"""Returns an instance of Cookie API""" """Returns an instance of Cookie API"""
_settings = {**app_settings, **settings} _settings = {**app_settings, **settings}
global_init(_settings) init_logging(_settings)
logger.debug("Template directory {}".format(template_dir)) logger.debug("Template directory {}".format(template_dir))
logger.debug("Static directory {}".format(STATIC_DIR)) logger.debug("Static directory {}".format(STATIC_DIR))

@ -2,7 +2,7 @@ import sys
import logbook import logbook
def global_init(settings={}): def init_logging(settings={}):
_logging_setting = settings.get("LOGGING", {"LEVEL": logbook.TRACE}) _logging_setting = settings.get("LOGGING", {"LEVEL": logbook.TRACE})
_log_file = _logging_setting.get("LOG_FILE") _log_file = _logging_setting.get("LOG_FILE")

@ -10,12 +10,10 @@ from sqlalchemy.types import DateTime as DateTimeType
BCRYPT_LOG_ROUNDS = 11 BCRYPT_LOG_ROUNDS = 11
# can be moved to models util?
class utcnow(expression.FunctionElement): class utcnow(expression.FunctionElement):
type = DateTimeType() type = DateTimeType()
# Can be moved to the models util?
@compiles(utcnow, 'postgresql') @compiles(utcnow, 'postgresql')
def pg_utcnow(element, compiler, **kw): def pg_utcnow(element, compiler, **kw):
return "TIMEZONE('utc', CURRENT_TIMESTAMP)" return "TIMEZONE('utc', CURRENT_TIMESTAMP)"

@ -1,7 +1,7 @@
import typing import typing
import logbook import logbook
from apistar import http, Route, App from apistar import Route, App
from apistar_jwt import authentication_required, JWTUser from apistar_jwt import authentication_required, JWTUser
from sqlalchemy.orm import Session from sqlalchemy.orm import Session

@ -11,7 +11,7 @@
<div id="app"> <div id="app">
<ul> <ul>
<li v-for="cookie in cookies"> <li v-for="cookie in cookies">
{{ cookie.name }} {% raw %}{{ cookie.name }} {% endraw%}
</li> </li>
</ul> </ul>
</div> </div>

@ -1,5 +1,4 @@
from .app import MetaApp
from .http import MetaJSONResponse, ExtJSONResponse from .http import MetaJSONResponse, ExtJSONResponse
from .component import SQLAlchemySession, DBSession from .component import SQLAlchemySession, DBSession
from .hook import SQLAlchemyHook from .hook import SQLAlchemyHook
from .validators import Decimal from .validators import Decimal

@ -1,28 +0,0 @@
import sys
from apistar import App, exceptions
from apistar.http import Response, HTMLResponse
from apistar.server.components import ReturnValue
from .http import MetaJSONResponse
class MetaApp(App):
"""
A WSGI App subclass with a MetaJSONResponse default response type
"""
def render_response(self, return_value: ReturnValue) -> Response:
if isinstance(return_value, Response):
return return_value
elif isinstance(return_value, str):
return HTMLResponse(return_value)
return MetaJSONResponse(return_value)
def exception_handler(self, exc: Exception) -> Response:
if isinstance(exc, exceptions.HTTPException):
return MetaJSONResponse(exc.detail, status_code=exc.status_code, headers=exc.get_headers())
raise
def error_handler(self) -> Response:
return MetaJSONResponse('Server error', status_code=500, exc_info=sys.exc_info())

@ -1,6 +1,7 @@
from apistar.http import Response from apistar.http import Response
from .component import Session, DBSession from .component import Session, DBSession
class SQLAlchemyHook: class SQLAlchemyHook:
def on_request(self, session: Session): def on_request(self, session: Session):
return return
@ -12,4 +13,4 @@ class SQLAlchemyHook:
def on_error(self, session: Session, response: Response): def on_error(self, session: Session, response: Response):
session.rollback() session.rollback()
DBSession.remove() DBSession.remove()
return response return response

@ -4,7 +4,10 @@
# #
# pip-compile --output-file dev_requirements.txt dev_requirements.in # pip-compile --output-file dev_requirements.txt dev_requirements.in
# #
alembic==0.9.9 --trusted-host pypi.python.org
--trusted-host git.aigalatic.com
alembic==0.9.10
atomicwrites==1.1.5 # via pytest atomicwrites==1.1.5 # via pytest
attrs==18.1.0 # via pytest attrs==18.1.0 # via pytest
coverage==4.5.1 # via pytest-cov coverage==4.5.1 # via pytest-cov
@ -12,12 +15,12 @@ mako==1.0.7 # via alembic
markupsafe==1.0 # via mako markupsafe==1.0 # via mako
more-itertools==4.2.0 # via pytest more-itertools==4.2.0 # via pytest
pluggy==0.6.0 # via pytest, tox pluggy==0.6.0 # via pytest, tox
py==1.5.3 # via pytest, tox py==1.5.4 # via pytest, tox
pytest-cov==2.5.1 pytest-cov==2.5.1
pytest==3.6.1 pytest==3.6.3
python-dateutil==2.7.3 # via alembic python-dateutil==2.7.3 # via alembic
python-editor==1.0.3 # via alembic python-editor==1.0.3 # via alembic
six==1.11.0 # via more-itertools, pytest, python-dateutil, tox six==1.11.0 # via more-itertools, pytest, python-dateutil, tox
sqlalchemy==1.2.8 # via alembic sqlalchemy==1.2.9 # via alembic
tox==3.0.0 tox==3.0.0
virtualenv==16.0.0 # via tox virtualenv==16.0.0 # via tox

@ -0,0 +1,8 @@
apistar
apistar-jwt
apistar-mail
bcrypt
psycopg2-binary
itsdangerous
logbook
sqlalchemy

@ -2,28 +2,31 @@
# This file is autogenerated by pip-compile # This file is autogenerated by pip-compile
# To update, run: # To update, run:
# #
# pip-compile --output-file requirements.txt setup.py # pip-compile --output-file requirements.txt requirements.in
# #
apistar-jwt==0.4.2 --trusted-host pypi.python.org
--trusted-host git.aigalatic.com
apistar-jwt==0.5.0
apistar-mail==0.3.0 apistar-mail==0.3.0
apistar==0.5.18 apistar==0.5.40
bcrypt==3.1.4 bcrypt==3.1.4
certifi==2018.4.16 # via requests certifi==2018.4.16 # via requests
cffi==1.11.5 # via bcrypt cffi==1.11.5 # via bcrypt
chardet==3.0.4 # via requests chardet==3.0.4 # via requests
click==6.7 # via apistar click==6.7 # via apistar
idna==2.6 # via requests idna==2.7 # via requests
itsdangerous==0.24 itsdangerous==0.24
jinja2==2.10 # via apistar jinja2==2.10 # via apistar
logbook==1.3.3 logbook==1.4.0
markupsafe==1.0 # via jinja2 markupsafe==1.0 # via jinja2
psycopg2-binary==2.7.4 psycopg2-binary==2.7.5
pycparser==2.18 # via cffi pycparser==2.18 # via cffi
pyjwt==1.6.4 # via apistar-jwt pyjwt==1.6.4 # via apistar-jwt
pyyaml==3.12 # via apistar pyyaml==3.12 # via apistar
requests==2.18.4 # via apistar requests==2.19.1 # via apistar
six==1.11.0 # via bcrypt six==1.11.0 # via bcrypt
sqlalchemy==1.2.8 sqlalchemy==1.2.9
urllib3==1.22 # via requests urllib3==1.23 # via requests
werkzeug==0.14.1 # via apistar werkzeug==0.14.1 # via apistar
whitenoise==3.3.1 # via apistar whitenoise==3.3.1 # via apistar

@ -4,14 +4,14 @@ with open('README.md') as readme_file:
readme = readme_file.read() readme = readme_file.read()
requirements = [ requirements = [
'apistar-jwt==0.4.2', 'apistar-jwt==0.5.0',
'apistar-mail==0.3.0', 'apistar-mail==0.3.0',
'apistar==0.5.18', 'apistar==0.5.40',
'bcrypt==3.1.4', 'bcrypt==3.1.4',
'itsdangerous==0.24', 'itsdangerous==0.24',
'logbook==1.3.3', 'logbook==1.4.0',
'psycopg2-binary==2.7.4', 'psycopg2-binary==2.7.5',
'sqlalchemy==1.2.8', 'sqlalchemy==1.2.9',
] ]
test_requirements = [ test_requirements = [
@ -40,7 +40,7 @@ setup(
'Development Status :: 2 - Pre-Alpha', 'Development Status :: 2 - Pre-Alpha',
'Environment :: Web Environment', 'Environment :: Web Environment',
'Intended Audience :: Developers', 'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', 'License :: OSI Approved :: MIT License',
'Natural Language :: English', 'Natural Language :: English',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
@ -50,7 +50,7 @@ setup(
}, },
entry_points={ entry_points={
'console_scripts': [ 'console_scripts': [
'wsgi_runner=wsgi:main' 'wsgi_serve=wsgi:main'
] ]
} }
) )

@ -12,7 +12,7 @@ from sqlalchemy import create_engine
from cookie_api import application_factory from cookie_api import application_factory
from cookie_api.util import SQLAlchemySession, SQLAlchemyHook from cookie_api.util import SQLAlchemySession, SQLAlchemyHook
from config import db_config, jwt_config, mail_config from config import db_config, jwt_config, mail_config, logging_config
components = [ components = [
SQLAlchemySession(create_engine(db_config)), SQLAlchemySession(create_engine(db_config)),
@ -24,7 +24,7 @@ hooks = [
SQLAlchemyHook() SQLAlchemyHook()
] ]
app = application_factory(components=components, hooks=hooks) app = application_factory(components=components, hooks=hooks, settings={**logging_config})
def main(): def main():
@ -32,4 +32,4 @@ def main():
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save