コード例 #1
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
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
コード例 #2
0
ファイル: db.py プロジェクト: NotTheEconomist/banking
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!")
コード例 #3
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
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))
コード例 #4
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
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]
コード例 #5
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
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))
コード例 #6
0
ファイル: db.py プロジェクト: NotTheEconomist/banking
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
コード例 #7
0
ファイル: auth.py プロジェクト: NotTheEconomist/banking
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")
コード例 #8
0
ファイル: auth.py プロジェクト: NotTheEconomist/banking
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")
コード例 #9
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
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)
コード例 #10
0
ファイル: auth.py プロジェクト: NotTheEconomist/banking
def logout():
    context = get_context()
    context.user = None
コード例 #11
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
def _add_customer():
    # TODO: ensure authentication
    db = get_context().db
    customer = models.Customer.new_prompted()
    make_record(db, customer)
コード例 #12
0
ファイル: menu.py プロジェクト: NotTheEconomist/banking
def _quit():
    '''Poison pill'''

    context = get_context()
    context.quitting = True
コード例 #13
0
ファイル: auth.py プロジェクト: NotTheEconomist/banking
def logout():
    context = get_context()
    context.user = None
コード例 #14
0
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)
コード例 #15
0
ファイル: main.py プロジェクト: NotTheEconomist/banking
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)