dibbler/db.py
oysteini 109db03edb Fikset datatype for kolonnen PurchaseEntry.product_bar_code.
Glemte visst å endre denne da jeg endret Product.bar_code tidligere.
2010-05-11 18:02:45 +00:00

137 lines
3.7 KiB
Python

from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey, create_engine, DateTime, Boolean, or_
from sqlalchemy.orm import sessionmaker, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
import datetime
import conf
engine = create_engine(conf.db_url)
Base = declarative_base()
Session = sessionmaker(bind=engine)
class User(Base):
__tablename__ = 'users'
name = Column(String(10), primary_key=True)
card = Column(String(10))
credit = Column(Integer)
name_re = r"[a-z]+"
card_re = r"((ntnu)?[0-9]+)?"
def __init__(self, name, card, credit=0):
self.name = name
if card == '':
card = None
self.card = card
self.credit = credit
def __repr__(self):
return "<User('%s')>" % self.name
def __str__(self):
return self.name
class Product(Base):
__tablename__ = 'products'
bar_code = Column(String(13), primary_key=True)
name = Column(String(30))
price = Column(Integer)
bar_code_re = r"[0-9]+"
name_re = r".+"
def __init__(self, bar_code, name, price):
self.name = name
self.bar_code = bar_code
self.price = price
def __repr__(self):
return "<Product('%s', '%s', '%s')>" % (self.name, self.bar_code, self.price)
def __str__(self):
return self.name
class PurchaseEntry(Base):
__tablename__ = 'purchase_entries'
id = Column(Integer, primary_key=True)
purchase_id = Column(Integer,ForeignKey("purchases.id"))
product_bar_code = Column(String(13),ForeignKey("products.bar_code"))
amount = Column(Integer)
product = relationship(Product,backref="purchases")
def __init__(self, purchase, product, amount):
self.product = product
self.product_bar_code = product.bar_code
self.purchase = purchase
self.amount = amount
def __repr__(self):
return "<PurchaseEntry('%s', '%s', '%s')>" % (self.purchase.user.user, self.product.name, self.amount )
class Transaction(Base):
__tablename__ = 'transactions'
id = Column(Integer, primary_key=True)
time = Column(DateTime)
user_name = Column(String(10), ForeignKey('users.name'))
amount = Column(Integer)
description = Column(String(50))
purchase_id = Column(Integer, ForeignKey('purchases.id'))
user = relationship(User, backref=backref('transactions', order_by=time))
def __init__(self, user, amount=0, description=None, purchase=None):
self.user = user
self.amount = amount
self.description = description
self.purchase = purchase
def perform_transaction(self):
self.time = datetime.datetime.now()
self.user.credit -= self.amount
class Purchase(Base):
__tablename__ = 'purchases'
id = Column(Integer, primary_key=True)
time = Column(DateTime)
# user_name = Column(Integer, ForeignKey('users.name'))
price = Column(Integer)
# performed = Column(Boolean)
# user = relationship(User, backref=backref('purchases', order_by=id))
# users = relationship(User, secondary=purchase_user, backref='purhcases'
transactions = relationship(Transaction, order_by=Transaction.user_name, backref='purchase')
entries = relationship(PurchaseEntry, backref=backref("purchase"))
def __init__(self):
pass
def __repr__(self):
return "<Purchase('%s', '%s', '%s')>" % (self.user.name, self.price, self.time.strftime('%c'))
def is_complete(self):
return len(self.transactions) > 0 and len(self.entries) > 0
def price_per_transaction(self):
return self.price/len(self.transactions)
def set_price(self):
self.price = 0
for entry in self.entries:
self.price += entry.amount*entry.product.price
if len(self.transactions) > 0:
for t in self.transactions:
t.amount = self.price_per_transaction()
def perform_purchase(self):
self.time = datetime.datetime.now()
self.set_price()
for t in self.transactions:
t.perform_transaction()
# self.user.credit -= self.price
# self.performed = True