From c7c7498f19b2d0efd9a3c211629dfe7cc4210fc3 Mon Sep 17 00:00:00 2001 From: almelid Date: Fri, 7 May 2010 17:32:39 +0000 Subject: [PATCH] jassaa --- data | Bin 0 -> 6144 bytes db.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++ helpers.py | 43 ++++++++++++++++++++++++ text_based.py | 68 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+) create mode 100644 data create mode 100644 db.py create mode 100644 helpers.py create mode 100644 text_based.py diff --git a/data b/data new file mode 100644 index 0000000000000000000000000000000000000000..783c3808f840460196e9e8ba0e066b049fa60ba2 GIT binary patch literal 6144 zcmeH}O>fgM7{}u{U1?wvw;)Z`yjvo5QoAy4NMyxZ%i0vOg0u@!))>H~WO3QJPqo!L_Vf7V|NI_1?b(Z=pAhsWjOK2Fx{@LRAU#1ylB7HI zTP+IhfQq5ou9*LAZo@xPP5LasXYi-o2j6M4EME?4b!~qieA5#5Vn!BeG=1mB#34Zv z`6OQNZ}n_!PB5C7M?;L(y+~IZzJ~_Z1fO6VS>p+^UJi#Xq&CW-SN_Z#gariN$PsWfKG0_!!&R>fy6eXMFvv zH4XgKm6OgaBpRx*ysy-?W)m#q6|{WJB7fF+NaYG@qgbN|ibE8*bAn!(cCT;RdbgcL zw}|{H2ZcjWEXO2$f^}_o7knLYc`OqcE4-*Y&or(ZQNF;ilc~%z zyvpF3!Uc)gX(k>z8jpwAwAd(4_BEL+m*(IzD^8D2{868^rufeYn=fFST60(apK$lnX{ rJ=g@rs{pXyfBL6i6hS}`*gyi}{%>H?MaY6cPC(p$aTElB%_Z;`2jVPV literal 0 HcmV?d00001 diff --git a/db.py b/db.py new file mode 100644 index 0000000..51ec682 --- /dev/null +++ b/db.py @@ -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 "" % 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 "" % (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 "" % (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 "" % (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 diff --git a/helpers.py b/helpers.py new file mode 100644 index 0000000..5235639 --- /dev/null +++ b/helpers.py @@ -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 diff --git a/text_based.py b/text_based.py new file mode 100644 index 0000000..fc3509c --- /dev/null +++ b/text_based.py @@ -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() + +