From 5a1f3fd19e7a0378f4345d00bb61216b5dc8f934 Mon Sep 17 00:00:00 2001
From: erdgeist <>
Date: Mon, 30 Aug 2010 19:00:04 +0000
Subject: Kickoff
---
configure.zcml | 17 +++++++++++++++++
customer.py | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
db.py | 40 ++++++++++++++++++++++++++++++++++++++++
product.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
tinybill.py | 23 +++++++++++++++++++++++
5 files changed, 190 insertions(+)
create mode 100644 configure.zcml
create mode 100644 customer.py
create mode 100644 db.py
create mode 100644 product.py
create mode 100644 tinybill.py
diff --git a/configure.zcml b/configure.zcml
new file mode 100644
index 0000000..2b07c13
--- /dev/null
+++ b/configure.zcml
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/customer.py b/customer.py
new file mode 100644
index 0000000..e707346
--- /dev/null
+++ b/customer.py
@@ -0,0 +1,52 @@
+import sqlalchemy
+from sqlalchemy import *
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
+from webob import Response
+from webob.exc import HTTPFound
+import db, product
+
+Base = declarative_base()
+class Customer(Base):
+ __tablename__ = 'customers'
+
+ id = Column(Integer, primary_key=True)
+ email = Column(String)
+ name = Column(String)
+
+ def __init__(self, name, email ):
+ self.name = name
+ self.email = email
+ session = db.Session()
+ session.add( self )
+ session.commit()
+
+ def __repr__(self):
+ return "" % (self.name, self.email)
+
+def list(request):
+ session = db.Session()
+ customers = {}
+ products = {}
+ for cust in session.query(Customer).order_by(Customer.id):
+ customers[ cust.id ] = { 'name': cust.name, 'email': cust.email }
+ for prod in session.query(product.Product).order_by(product.Product.id):
+ products[ prod.id ] = { 'name': prod.name, 'type': prod.type, 'price': prod.price }
+ session.close()
+ return { 'customers': customers, 'products': products }
+
+def info(request):
+ session = db.Session()
+ try:
+ customer = session.query(Customer).filter(Customer.name == request.matchdict['customer'] ).one()
+ resp = "User: id=%s name=%s email=%s
" % ( customer.id, customer.name, customer.email )
+ except MultipleResultsFound, e:
+ resp = "Multiple users found with name %s." % request.matchdict['customer']
+ except NoResultFound, e:
+ resp = "User %s unknown." % request.matchdict['customer']
+ return Response( resp )
+
+def new(request):
+ newUser = Customer( request.params['name'], request.params['email'] )
+ destination = "/customers/%s" % newUser.name
+ return HTTPFound( location = destination )
diff --git a/db.py b/db.py
new file mode 100644
index 0000000..5e13924
--- /dev/null
+++ b/db.py
@@ -0,0 +1,40 @@
+import sqlalchemy
+from sqlalchemy import *
+from sqlalchemy.orm import sessionmaker
+
+def setupdb():
+ #engine = create_engine('sqlite:///Users/erdgeist/Coding/tinybill/foo.db', echo=True)
+ engine = create_engine('sqlite:///foo.db', echo=True)
+ metadata = MetaData()
+
+ Table( 'customers', metadata,
+ Column( 'id', Integer, primary_key=True ),
+ Column( 'name', String, unique=True ),
+ Column( 'email', String )
+ )
+
+ Table( 'products', metadata,
+ Column( 'id', Integer, primary_key=True ),
+ Column( 'type', String ),
+ Column( 'name', String, unique=True ),
+ Column( 'price', Integer ),
+ )
+
+ Table( 'purchases', metadata,
+ Column( 'id', Integer, primary_key=True ),
+ Column( 'product', Integer, ForeignKey('products.id' ) ),
+ Column( 'customer', Integer, ForeignKey('customers.id' ) ),
+ Column( 'detail', String ),
+ Column( 'date', Date )
+ )
+
+ Table( 'payment', metadata,
+ Column( 'id', Integer, primary_key=True ),
+ Column( 'purchase', Integer, ForeignKey('purchases.id') ),
+ Column( 'amount', Integer ),
+ Column( 'date', Date )
+ )
+
+ metadata.create_all(engine)
+ global Session
+ Session = sessionmaker(bind=engine)
diff --git a/product.py b/product.py
new file mode 100644
index 0000000..386af37
--- /dev/null
+++ b/product.py
@@ -0,0 +1,58 @@
+import sqlalchemy
+from sqlalchemy import *
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
+from webob import Response
+from webob.exc import HTTPFound
+import db, customer
+
+Base = declarative_base()
+class Product(Base):
+ __tablename__ = 'products'
+
+ id = Column(Integer, primary_key=True)
+ name = Column(String)
+ type = Column(String)
+ price = Column(Integer)
+
+ def __init__(self, name, type, price ):
+ self.name = name
+ self.type = type
+ self.price = price
+ session = db.Session()
+ try:
+ session.add( self )
+ session.commit()
+ except sqlalchemy.exc.IntegrityError, e:
+ session.rollback()
+
+ def __repr__(self):
+ return "" % (self.name, self.type, self.price)
+
+def list(request):
+ session = db.Session()
+ customers = {}
+ products = {}
+ for cust in session.query(customer.Customer).order_by(customer.Customer.id):
+ customers[ cust.id ] = { 'name': cust.name, 'email': cust.email }
+ for product in session.query(Product).order_by(Product.id):
+ products[ product.id ] = { 'name': product.name, 'type': product.type, 'price': product.price }
+ session.close()
+ return { 'customers': customers, 'products': products }
+
+def info(request):
+ session = db.Session()
+ try:
+ product = session.query(Product).filter(Product.name == request.matchdict['product'] ).one()
+ resp = "User: id=%s name=%s type=%s price=%d
" % ( product.id, product.name, product.type, product.price )
+ except MultipleResultsFound, e:
+ resp = "Multiple products found with name %s." % request.matchdict['product']
+ except NoResultFound, e:
+ resp = "Product %s unknown." % request.matchdict['product']
+ return Response( resp )
+
+def new(request):
+ newProduct = Product( request.params['name'], request.params['type'], request.params['price'] )
+ destination = "/product/%s" % newProduct.name
+ return HTTPFound( location = destination )
+
diff --git a/tinybill.py b/tinybill.py
new file mode 100644
index 0000000..adc7c78
--- /dev/null
+++ b/tinybill.py
@@ -0,0 +1,23 @@
+import sqlalchemy
+from sqlalchemy import *
+
+# bfg imports
+from webob import Response
+from paste.httpserver import serve
+from repoze.bfg.configuration import Configurator
+
+# tinybill imports
+from customer import *
+import db
+
+def hello_world(request):
+ return Response('Hello world!')
+
+if __name__ == '__main__':
+ db.setupdb( )
+ config = Configurator()
+ config.begin()
+ config.load_zcml('configure.zcml')
+ config.end()
+ app = config.make_wsgi_app()
+ serve(app, host='0.0.0.0')
--
cgit v1.2.3