summaryrefslogtreecommitdiff
path: root/product.py
diff options
context:
space:
mode:
authorerdgeist <>2010-08-30 19:00:04 +0000
committererdgeist <>2010-08-30 19:00:04 +0000
commit5a1f3fd19e7a0378f4345d00bb61216b5dc8f934 (patch)
tree53d48d36570eeb9339aedc5adf16a630bd85801f /product.py
Kickoff
Diffstat (limited to 'product.py')
-rw-r--r--product.py58
1 files changed, 58 insertions, 0 deletions
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