summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorerdgeist <>2010-08-30 19:00:04 +0000
committererdgeist <>2010-08-30 19:00:04 +0000
commit5a1f3fd19e7a0378f4345d00bb61216b5dc8f934 (patch)
tree53d48d36570eeb9339aedc5adf16a630bd85801f
Kickoff
-rw-r--r--configure.zcml17
-rw-r--r--customer.py52
-rw-r--r--db.py40
-rw-r--r--product.py58
-rw-r--r--tinybill.py23
5 files changed, 190 insertions, 0 deletions
diff --git a/configure.zcml b/configure.zcml
new file mode 100644
index 0000000..2b07c13
--- /dev/null
+++ b/configure.zcml
@@ -0,0 +1,17 @@
1<configure xmlns="http://namespaces.repoze.org/bfg">
2
3 <include package="repoze.bfg.includes" />
4
5 <view view="tinybill.hello_world" />
6
7 <!-- Customers -->
8 <view view="customer.list" name="customers" renderer="templates/customers.pt" />
9 <view view="customer.new" name="customerNew" />
10 <route view="customer.info" name="customersRoute" path="/customer/:customer" />
11
12 <!-- Products -->
13 <view view="product.list" name="products" renderer="templates/products.pt" />
14 <view view="product.new" name="productNew" />
15 <route view="product.info" name="productRoute" path="/product/:product" />
16
17</configure>
diff --git a/customer.py b/customer.py
new file mode 100644
index 0000000..e707346
--- /dev/null
+++ b/customer.py
@@ -0,0 +1,52 @@
1import sqlalchemy
2from sqlalchemy import *
3from sqlalchemy.ext.declarative import declarative_base
4from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
5from webob import Response
6from webob.exc import HTTPFound
7import db, product
8
9Base = declarative_base()
10class Customer(Base):
11 __tablename__ = 'customers'
12
13 id = Column(Integer, primary_key=True)
14 email = Column(String)
15 name = Column(String)
16
17 def __init__(self, name, email ):
18 self.name = name
19 self.email = email
20 session = db.Session()
21 session.add( self )
22 session.commit()
23
24 def __repr__(self):
25 return "<User('%s, %s')>" % (self.name, self.email)
26
27def list(request):
28 session = db.Session()
29 customers = {}
30 products = {}
31 for cust in session.query(Customer).order_by(Customer.id):
32 customers[ cust.id ] = { 'name': cust.name, 'email': cust.email }
33 for prod in session.query(product.Product).order_by(product.Product.id):
34 products[ prod.id ] = { 'name': prod.name, 'type': prod.type, 'price': prod.price }
35 session.close()
36 return { 'customers': customers, 'products': products }
37
38def info(request):
39 session = db.Session()
40 try:
41 customer = session.query(Customer).filter(Customer.name == request.matchdict['customer'] ).one()
42 resp = "User: id=%s name=%s email=%s<br>" % ( customer.id, customer.name, customer.email )
43 except MultipleResultsFound, e:
44 resp = "Multiple users found with name %s." % request.matchdict['customer']
45 except NoResultFound, e:
46 resp = "User %s unknown." % request.matchdict['customer']
47 return Response( resp )
48
49def new(request):
50 newUser = Customer( request.params['name'], request.params['email'] )
51 destination = "/customers/%s" % newUser.name
52 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 @@
1import sqlalchemy
2from sqlalchemy import *
3from sqlalchemy.orm import sessionmaker
4
5def setupdb():
6 #engine = create_engine('sqlite:///Users/erdgeist/Coding/tinybill/foo.db', echo=True)
7 engine = create_engine('sqlite:///foo.db', echo=True)
8 metadata = MetaData()
9
10 Table( 'customers', metadata,
11 Column( 'id', Integer, primary_key=True ),
12 Column( 'name', String, unique=True ),
13 Column( 'email', String )
14 )
15
16 Table( 'products', metadata,
17 Column( 'id', Integer, primary_key=True ),
18 Column( 'type', String ),
19 Column( 'name', String, unique=True ),
20 Column( 'price', Integer ),
21 )
22
23 Table( 'purchases', metadata,
24 Column( 'id', Integer, primary_key=True ),
25 Column( 'product', Integer, ForeignKey('products.id' ) ),
26 Column( 'customer', Integer, ForeignKey('customers.id' ) ),
27 Column( 'detail', String ),
28 Column( 'date', Date )
29 )
30
31 Table( 'payment', metadata,
32 Column( 'id', Integer, primary_key=True ),
33 Column( 'purchase', Integer, ForeignKey('purchases.id') ),
34 Column( 'amount', Integer ),
35 Column( 'date', Date )
36 )
37
38 metadata.create_all(engine)
39 global Session
40 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 @@
1import sqlalchemy
2from sqlalchemy import *
3from sqlalchemy.ext.declarative import declarative_base
4from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
5from webob import Response
6from webob.exc import HTTPFound
7import db, customer
8
9Base = declarative_base()
10class Product(Base):
11 __tablename__ = 'products'
12
13 id = Column(Integer, primary_key=True)
14 name = Column(String)
15 type = Column(String)
16 price = Column(Integer)
17
18 def __init__(self, name, type, price ):
19 self.name = name
20 self.type = type
21 self.price = price
22 session = db.Session()
23 try:
24 session.add( self )
25 session.commit()
26 except sqlalchemy.exc.IntegrityError, e:
27 session.rollback()
28
29 def __repr__(self):
30 return "<Product('%s, %s, %s')>" % (self.name, self.type, self.price)
31
32def list(request):
33 session = db.Session()
34 customers = {}
35 products = {}
36 for cust in session.query(customer.Customer).order_by(customer.Customer.id):
37 customers[ cust.id ] = { 'name': cust.name, 'email': cust.email }
38 for product in session.query(Product).order_by(Product.id):
39 products[ product.id ] = { 'name': product.name, 'type': product.type, 'price': product.price }
40 session.close()
41 return { 'customers': customers, 'products': products }
42
43def info(request):
44 session = db.Session()
45 try:
46 product = session.query(Product).filter(Product.name == request.matchdict['product'] ).one()
47 resp = "User: id=%s name=%s type=%s price=%d<br>" % ( product.id, product.name, product.type, product.price )
48 except MultipleResultsFound, e:
49 resp = "Multiple products found with name %s." % request.matchdict['product']
50 except NoResultFound, e:
51 resp = "Product %s unknown." % request.matchdict['product']
52 return Response( resp )
53
54def new(request):
55 newProduct = Product( request.params['name'], request.params['type'], request.params['price'] )
56 destination = "/product/%s" % newProduct.name
57 return HTTPFound( location = destination )
58
diff --git a/tinybill.py b/tinybill.py
new file mode 100644
index 0000000..adc7c78
--- /dev/null
+++ b/tinybill.py
@@ -0,0 +1,23 @@
1import sqlalchemy
2from sqlalchemy import *
3
4# bfg imports
5from webob import Response
6from paste.httpserver import serve
7from repoze.bfg.configuration import Configurator
8
9# tinybill imports
10from customer import *
11import db
12
13def hello_world(request):
14 return Response('Hello world!')
15
16if __name__ == '__main__':
17 db.setupdb( )
18 config = Configurator()
19 config.begin()
20 config.load_zcml('configure.zcml')
21 config.end()
22 app = config.make_wsgi_app()
23 serve(app, host='0.0.0.0')