def menu(): context = get_context() if isinstance(context.user, models.Customer): options = [Menuoption("Deposit money", _deposit_money), Menuoption("Withdraw money", _withdraw_money), Menuoption("Display info", context.user.display), Menuoption("Log Out", auth.logout)] elif isinstance(context.user, models.Admin): options = [Menuoption("Add customer", _add_customer), Menuoption("Delete customer", _del_customer), Menuoption("List customers", _list_customers), Menuoption("Edit customer", _edit_customer), Menuoption("Log Out", auth.logout)] elif context.user is None: options = [Menuoption("Log in", auth.login)] options.append(Menuoption("Quit", _quit)) # all states can quit opt_dict = {str(idx): option.f for idx,option in enumerate(options, start=1)} while True: print("MENU") for idx, option in enumerate(options, start=1): print("{i}. {opt.name}".format(i=idx, opt=option)) user_in = input(">> ") try: f = opt_dict[user_in] except KeyError: print("--- Invalid input {} ---".format(user_in)) continue else: f() break
def save(filename): db = get_context().db with open(filename, 'w') as db_f: for record in db.values(): customer = record.user db_f.write("{}\n".format(customer.serialize())) print("Updates saved!")
def _list_customers(): # TODO: ensure authentication db = get_context().db print("Username : Name") print("---------------") for username, record in db.items(): name = record.user.name print("{:8} : {}".format(username, name))
def _del_customer(): # TODO: ensure authentication db = get_context().db customer_username = input("Username for customer: ") customer = db[customer_username] print("About to delete the following customer") customer.display() if input("Are you sure? ").lower().startswith("y"): del db[customer]
def _withdraw_money(): context = get_context() account = context.user.checking print("How much would you like to withdraw? Balance: {}".format(account.balance)) amount = input(":: $ ") try: account.withdraw(amount) except Exception: print("Withdrawal failed, your balance has not changed") raise else: print("Thank you! Your new balance is {}".format(account.balance))
def load(filename): context = get_context() db = {} admin = Admin(ADMIN_USERNAME, ADMIN_PASSWORD) make_record(db, admin) try: with open(filename) as db_f: customers = [Customer.deserialize(line) for line in db_f if line.strip()] for customer in customers: make_record(db, customer) except FileNotFoundError: print("No such database. Save first?") context.db = db
def login(): '''Prompt for login''' context = get_context() print("LOGIN") username = input("Username: "******"Password: "******"User {} is already logged in") return user = authenticate_user(context.db, username, password) if user: context.user = user else: print("Invalid username or password")
def _edit_customer(): # TODO: ensure authentication db = get_context().db customer_username = input("Username for customer: ") customer = db[customer_username] field_to_edit = input("What field do you want to edit? ") try: cur_value = getattr(customer, field_to_edit) except AttributeError: print("Bad customer field {}".format(field_to_edit)) else: print("Current value is {}. What do you want to change it to?".format(cur_value)) new_value = input(">> ") confirm = input("Changing {field} from {old} to {new}. Are you sure? ".format( field=field_to_edit, old=cur_value, new=new_value)) if confirm.lower().startswith('y'): setattr(customer, field_to_edit, new_value)
def logout(): context = get_context() context.user = None
def _add_customer(): # TODO: ensure authentication db = get_context().db customer = models.Customer.new_prompted() make_record(db, customer)
def _quit(): '''Poison pill''' context = get_context() context.quitting = True
import app import db import menu import sys database_location = "database.txt" def init(): app.make_context() db.load(database_location) if __name__ == "__main__": init() context = app.get_context() while True: menu.menu() if input("Save changes? ").lower().startswith('y'): db.save(database_location) if context.quitting: sys.exit(0)