jassaa
This commit is contained in:
parent
95285be342
commit
c7c7498f19
|
@ -0,0 +1,88 @@
|
|||
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
|
||||
|
||||
engine = create_engine('sqlite:///data')
|
||||
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)
|
||||
|
||||
def __init__(self, name, card, credit=0):
|
||||
self.name = name
|
||||
self.card = card
|
||||
self.credit = credit
|
||||
|
||||
def __repr__(self):
|
||||
return "<User('%s')>" % self.name
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = 'products'
|
||||
|
||||
bar_code = Column(Integer, primary_key=True)
|
||||
name = Column(String(30))
|
||||
price = Column(Integer)
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
self.purchase_id = purchase.id
|
||||
self.amount = amount
|
||||
|
||||
def __repr__(self):
|
||||
return "<PurchaseEntry('%s', '%s', '%s')>" % (self.purchase.user.user, self.product.name, 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))
|
||||
products = relationship(PurchaseEntry, backref=backref("purchase"))
|
||||
|
||||
def __init__(self ,performed=False):
|
||||
self.performed = performed
|
||||
|
||||
def __repr__(self):
|
||||
return "<Purchase('%s', '%s', '%s')>" % (self.user.name, self.price, self.time.strftime('%c'))
|
||||
|
||||
def set_price(self):
|
||||
self.price = 0
|
||||
for entry in self.products:
|
||||
self.price += entry.amount*entry.product.price
|
||||
|
||||
def perform_purchase(self):
|
||||
if self.performed:
|
||||
print "This transaction has already been performed"
|
||||
else:
|
||||
self.time = datetime.datetime.now()
|
||||
self.set_price()
|
||||
self.user.credit -= self.price
|
||||
self.performed = True
|
|
@ -0,0 +1,43 @@
|
|||
from db import *
|
||||
from sqlalchemy import or_
|
||||
|
||||
def retrieve_user(string, session):
|
||||
first = session.query(User).filter(or_(User.name==string, User.card==string)).first()
|
||||
if first:
|
||||
print "Found user "+first.name
|
||||
return first
|
||||
else:
|
||||
list = session.query(User).filter(or_(User.name.like('%'+string+'%'),User.card.like('%'+string+'%'))).all()
|
||||
if len(list) == 0:
|
||||
print "No users found matching your search"
|
||||
return None
|
||||
if len(list) == 1:
|
||||
print "Found one user: "+list[0].name
|
||||
if confirm():
|
||||
return list[0]
|
||||
else:
|
||||
return None
|
||||
else:
|
||||
print "Found "+str(len(list))+" users:"
|
||||
return select_from_list(list)
|
||||
|
||||
|
||||
def confirm():
|
||||
while True:
|
||||
input = raw_input("Confirm? (y/n)\n")
|
||||
if input in ["y","yes"]:
|
||||
return True
|
||||
elif input in ["n","no"]:
|
||||
return False
|
||||
else:
|
||||
print "Nonsense!"
|
||||
|
||||
def select_from_list(list):
|
||||
while True:
|
||||
for i in range(len(list)):
|
||||
print i+1, " ) ", list[i].name
|
||||
choice = raw_input("Select user :\n")
|
||||
if choice in [str(x+1) for x in range(len(list))]:
|
||||
return list[int(choice)-1]
|
||||
else:
|
||||
return None
|
|
@ -0,0 +1,68 @@
|
|||
from helpers import *
|
||||
|
||||
class Menu():
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
class ChargeMenu(Menu):
|
||||
def __init__(self):
|
||||
self.name = "Add Credits to a User Account"
|
||||
|
||||
def execute(self):
|
||||
self.session = Session()
|
||||
while 1:
|
||||
abort = False
|
||||
while 1:
|
||||
user_string = raw_input('\nEnter the user name or card number of the account you wish to add credits to, or type "exit" to exit:\n')
|
||||
if user_string in ['exit', 'abort', 'quit']:
|
||||
abort = True
|
||||
break
|
||||
else:
|
||||
user = retrieve_user(user_string,self.session)
|
||||
if user:
|
||||
break
|
||||
if abort:
|
||||
break
|
||||
while 1:
|
||||
print '\nHow much do you wish to charge?\n'
|
||||
amount_string = raw_input('Enter an amount, or type "exit" to exit:\n')
|
||||
|
||||
if amount_string in ['exit', 'abort', 'quit']:
|
||||
abort = True
|
||||
break
|
||||
try:
|
||||
amount = int(amount_string)
|
||||
break
|
||||
except:
|
||||
print "Please enter an integer"
|
||||
if abort:
|
||||
break
|
||||
else:
|
||||
user.credit += amount
|
||||
self.session.add(user)
|
||||
self.session.commit()
|
||||
self.session.close()
|
||||
break
|
||||
|
||||
class MainMenu():
|
||||
def __init__(self):
|
||||
self.menu_list = [Menu("Buy"),ChargeMenu(), Menu("Add User"), Menu("Add Product")]
|
||||
|
||||
def execute(self):
|
||||
while 1:
|
||||
print "Main Menu: \nWhat do you want to do? \n"
|
||||
for i in range(len(self.menu_list)):
|
||||
print i+1," ) ",self.menu_list[i].name
|
||||
result = raw_input('\nEnter a number corresponding to your action, or "exit" to exit \n')
|
||||
if result in ["1","2","3","4"]:
|
||||
self.menu_list[int(result)-1].execute()
|
||||
elif result in ["quit", "exit", "abort"]:
|
||||
print "OK, quitting"
|
||||
break
|
||||
else:
|
||||
print "This does not make sense"
|
||||
|
||||
main = MainMenu()
|
||||
main.execute()
|
||||
|
||||
|
Loading…
Reference in New Issue