summaryrefslogtreecommitdiff
path: root/product.py
blob: 386af373a8e909090a5a4393da60d286a325427f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 "<Product('%s, %s, %s')>" % (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<br>" % ( 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 )