diff options
author | erdgeist <> | 2010-08-30 19:00:04 +0000 |
---|---|---|
committer | erdgeist <> | 2010-08-30 19:00:04 +0000 |
commit | 5a1f3fd19e7a0378f4345d00bb61216b5dc8f934 (patch) | |
tree | 53d48d36570eeb9339aedc5adf16a630bd85801f /product.py |
Kickoff
Diffstat (limited to 'product.py')
-rw-r--r-- | product.py | 58 |
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 @@ | |||
1 | import sqlalchemy | ||
2 | from sqlalchemy import * | ||
3 | from sqlalchemy.ext.declarative import declarative_base | ||
4 | from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound | ||
5 | from webob import Response | ||
6 | from webob.exc import HTTPFound | ||
7 | import db, customer | ||
8 | |||
9 | Base = declarative_base() | ||
10 | class 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 | |||
32 | def 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 | |||
43 | def 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 | |||
54 | def 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 | |||