2010-05-07 19:32:39 +02:00
|
|
|
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
|
2010-05-11 17:04:40 +02:00
|
|
|
import conf
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-11 17:04:40 +02:00
|
|
|
engine = create_engine(conf.db_url)
|
2010-05-07 19:32:39 +02:00
|
|
|
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)
|
|
|
|
|
2010-05-08 22:59:48 +02:00
|
|
|
name_re = r"[a-z]+"
|
2010-05-11 14:26:30 +02:00
|
|
|
card_re = r"((ntnu)?[0-9]+)?"
|
2010-05-08 22:59:48 +02:00
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
def __init__(self, name, card, credit=0):
|
|
|
|
self.name = name
|
2010-05-09 20:24:05 +02:00
|
|
|
if card == '':
|
|
|
|
card = None
|
2010-05-07 19:32:39 +02:00
|
|
|
self.card = card
|
|
|
|
self.credit = credit
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<User('%s')>" % self.name
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
class Product(Base):
|
|
|
|
__tablename__ = 'products'
|
|
|
|
|
|
|
|
bar_code = Column(Integer, primary_key=True)
|
|
|
|
name = Column(String(30))
|
|
|
|
price = Column(Integer)
|
|
|
|
|
2010-05-09 20:24:05 +02:00
|
|
|
bar_code_re = r"[0-9]+"
|
|
|
|
name_re = r".+"
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
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)
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
class PurchaseEntry(Base):
|
|
|
|
__tablename__ = 'purchase_entries'
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
purchase_id = Column(Integer,ForeignKey("purchases.id"))
|
|
|
|
product_bar_code = Column(Integer,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
|
2010-05-08 20:05:28 +02:00
|
|
|
self.purchase = purchase
|
2010-05-07 19:32:39 +02:00
|
|
|
self.amount = amount
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<PurchaseEntry('%s', '%s', '%s')>" % (self.purchase.user.user, self.product.name, self.amount )
|
|
|
|
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
class Purchase(Base):
|
|
|
|
__tablename__ = 'purchases'
|
|
|
|
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
time = Column(DateTime)
|
2010-05-08 20:05:28 +02:00
|
|
|
# user_name = Column(Integer, ForeignKey('users.name'))
|
2010-05-07 19:32:39 +02:00
|
|
|
price = Column(Integer)
|
2010-05-08 20:05:28 +02:00
|
|
|
# performed = Column(Boolean)
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
# 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"))
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
2010-05-07 19:32:39 +02:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<Purchase('%s', '%s', '%s')>" % (self.user.name, self.price, self.time.strftime('%c'))
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
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)
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
def set_price(self):
|
|
|
|
self.price = 0
|
2010-05-08 20:05:28 +02:00
|
|
|
for entry in self.entries:
|
2010-05-07 19:32:39 +02:00
|
|
|
self.price += entry.amount*entry.product.price
|
2010-05-08 20:05:28 +02:00
|
|
|
if len(self.transactions) > 0:
|
|
|
|
for t in self.transactions:
|
|
|
|
t.amount = self.price_per_transaction()
|
2010-05-07 19:32:39 +02:00
|
|
|
|
|
|
|
def perform_purchase(self):
|
2010-05-08 20:05:28 +02:00
|
|
|
self.time = datetime.datetime.now()
|
|
|
|
self.set_price()
|
|
|
|
for t in self.transactions:
|
|
|
|
t.perform_transaction()
|
|
|
|
# self.user.credit -= self.price
|
|
|
|
# self.performed = True
|