From 2eee8dc349dd443f339e32ecf6a81a99a3f982f2 Mon Sep 17 00:00:00 2001 From: androiddrew Date: Sun, 8 Oct 2017 09:51:51 -0400 Subject: [PATCH] Refactored model functionality --- app.py | 58 ++++++++++++++++++++++++++--------- {model => models}/__init__.py | 0 models.py => models/schema.py | 9 ++++-- {model => models}/util.py | 0 render.py | 6 ++-- 5 files changed, 54 insertions(+), 19 deletions(-) rename {model => models}/__init__.py (100%) rename models.py => models/schema.py (91%) rename {model => models}/util.py (100%) diff --git a/app.py b/app.py index ecd8329..5b5de23 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,13 @@ import json -from apistar import Include, Route, http, Response, annotate -from apistar.frameworks.wsgi import WSGIApp as App -from apistar.handlers import docs_urls, static_urls +from apistar import Include, Route, annotate, http +from apistar.interfaces import Router + from apistar.backends import sqlalchemy_backend from apistar.backends.sqlalchemy_backend import Session - -from models import Base, Cookie -from model.util import alchemyencoder +from apistar.frameworks.wsgi import WSGIApp as App +from apistar.handlers import docs_urls, static_urls +from models.schema import Base, Cookie from render import JSONRenderer @@ -20,20 +20,46 @@ def welcome(name=None): @annotate(renderers=[JSONRenderer()]) def get_cookies(session: Session): cookies = session.query(Cookie).all() - result = [{"id": cookie.id, - "created_date": cookie.created_date, - "modified_date": cookie.modified_date, - "name": cookie.name, - "recipe_url": cookie.recipe_url, - "sku": cookie.sku, - "qoh": cookie.unit_cost} - for cookie in cookies] + # result = [{"id": cookie.id, + # "created_date": cookie.created_date, + # "modified_date": cookie.modified_date, + # "name": cookie.name, + # "recipe_url": cookie.recipe_url, + # "sku": cookie.sku, + # "qoh": cookie.qoh, + # "unit_cost": cookie.unit_cost} + # for cookie in cookies] + result = [cookie.to_dict() for cookie in cookies] return result + +@annotate(renderers=[JSONRenderer()]) +def get_cookie(session: Session, id): + 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) + return cookie.to_dict() + + +def create_cookie(session: Session, json_data: http.RequestData, route: Router): + cookie = Cookie(name=json_data['name'], + recipe_url=json_data['recipe_url'], + sku=json_data['sku'], + qoh=json_data['qoh'], + unit_cost=json_data['unit_cost']) + session.add(cookie) + session.flush() + headers = {'Location': route.reverse_url('get_cookie', dict(id=cookie.id))} + return http.Response(cookie.to_dict(), status=201, headers=headers) + + routes = [ Route('/', 'GET', welcome), Route('/cookies', 'GET', get_cookies), + Route('/cookies', 'POST', create_cookie), + Route('/cookies/{id}', 'GET', get_cookie), Include('/docs', docs_urls), Include('/static', static_urls) ] @@ -50,3 +76,7 @@ app = App(routes=routes, commands=sqlalchemy_backend.commands, components=sqlalchemy_backend.components ) + + +if __name__ == "__main__": + app.main() \ No newline at end of file diff --git a/model/__init__.py b/models/__init__.py similarity index 100% rename from model/__init__.py rename to models/__init__.py diff --git a/models.py b/models/schema.py similarity index 91% rename from models.py rename to models/schema.py index 8f7c5de..ffc13b9 100644 --- a/models.py +++ b/models/schema.py @@ -5,11 +5,11 @@ from sqlalchemy.sql import expression from sqlalchemy.ext.compiler import compiles from sqlalchemy.types import DateTime as DateTimeType -# can be moved to models util +# can be moved to models util? class utcnow(expression.FunctionElement): type = DateTimeType() -# Can be moved to the models util dirp +# Can be moved to the models util? @compiles(utcnow, 'postgresql') def pg_utcnow(element, compiler, **kw): return "TIMEZONE('utc', CURRENT_TIMESTAMP)" @@ -24,6 +24,11 @@ class DBMixin: created_date = Column(DateTime, server_default=utcnow()) modified_date = Column(DateTime, server_default=utcnow(), onupdate=utcnow()) + def to_dict(self): + d = self.__dict__ + d.pop('_sa_instance_state') + return d + def ReferenceCol(tablename, nullable=False, **kw): return Column(ForeignKey('{}.id'.format(tablename)), nullable=nullable, **kw) diff --git a/model/util.py b/models/util.py similarity index 100% rename from model/util.py rename to models/util.py diff --git a/render.py b/render.py index 77c029b..1e105d5 100644 --- a/render.py +++ b/render.py @@ -1,8 +1,8 @@ import json + from apistar import http from apistar.renderers import Renderer - -from model.util import alchemyencoder +from models.util import alchemyencoder class JSONRenderer(Renderer): @@ -10,4 +10,4 @@ class JSONRenderer(Renderer): charset = None def render(self, data: http.ResponseData) -> bytes: - return json.dumps(data, default=alchemyencoder).encode('utf-8') \ No newline at end of file + return json.dumps(data, default=alchemyencoder).encode('utf-8') \ No newline at end of file