def display_accounts(customer_id=None):
  if customer_id == None:
    for account in S.query(Account).all():
      print "  ACCOUNT NUMBER: " + str(account.acct_num) + "\tACCOUNT TYPE: " + str(account.acct_type) + "\tACCOUNT BALANCE: " + str(account.balance) + "\n",
  else:
    for account in S.query(Account).filter(Account.customer_id==customer_id):
      print "  ACCOUNT NUMBER: " + str(account.acct_num) + "\tACCOUNT TYPE: " + str(account.acct_type) + "\tACCOUNT BALANCE: " + str(account.balance) + "\n",
  return
def abort_if_account_doesnt_exist(customer_id, acct_num):
  account = S.query(AccountModel) \
    .filter(AccountModel.customer_id==customer_id) \
    .filter(AccountModel.acct_num==acct_num).first()
  if not account:
    abort(404, error=doesnt_exist_error('account')['error'])
  return
 def do_3(self, args):
   print "Please enter the account number to transfer FROM:"
   display_accounts(customer_id=login_manager.get_customer().customer_id)
   account_num_from = int(raw_input(">").strip())
   account_from = S.query(Account).filter(Account.acct_num==account_num_from).first()
   display_accounts(customer_id=login_manager.get_customer().customer_id)
   print "Please enter the account number to transfer TO:"
   account_num_to = int(raw_input(">").strip())
   account_to = S.query(Account).filter(Account.acct_num==account_num_to).first()
   amount = float(raw_input("Please enter the amount to deposit: ").strip())
   account_from.balance -= amount
   account_to.balance += amount
   S.commit()
   create_transaction(from_acct=account_from, to_acct=account_to, amount=amount, transaction_type="transfer", final_balance=None)
   print self.intro
   return
 def get(self, customer_id=None):
   if customer_id:
     abort_if_customer_doesnt_exist(customer_id)
     customer = get_customer(customer_id)
     return customer.serialize
   else:
     customers = []
     for c in S.query(CustomerModel).all():
       customers.append(c.serialize)
     return customers
 def do_5(self, args):
   print "Please choose which customer to suspend:"
   display_customers()
   customers = S.query(Customer).all()
   customer_index = int(raw_input(">").strip())
   customer = customers[customer_index]
   customer.suspended = True
   S.commit()
   print self.intro
   return
 def login(self):
   try:
     self.customer = S.query(Customer).filter(Customer.username==self.username) \
                                      .filter(Customer.password==self.password) \
                                      .one()
   except:
     return False
   if self.customer.suspended:
     print "Failed to login. Customer is SUSPENDED."
     return False
   return True
 def do_2(self, args):
   print "Please enter the account number:"
   display_accounts(customer_id=login_manager.get_customer().customer_id)
   account_num = int(raw_input(">").strip())
   account = S.query(Account).filter(Account.acct_num==account_num).first()
   amount = float(raw_input("Please enter the amount to withdraw: ").strip())
   account.balance -= amount
   S.commit()
   create_transaction(from_acct=account, amount=amount, transaction_type="withdrawal", final_balance=account.balance)
   print self.intro
   return
 def do_1(self, args):
   print "Please enter the account number:"
   display_accounts(customer_id=login_manager.get_customer().customer_id)
   account_num = int(raw_input(">").strip())
   account = S.query(Account).filter(Account.acct_num==account_num).first()
   amount = float(raw_input("Please enter the amount to deposit: ").strip())
   account.balance += amount
   S.commit()
   create_transaction(to_acct=account, amount=amount, transaction_type="deposit")
   print self.intro
   return
 def do_4(self, args):
   global login_manager
   transactions = S.query(Transaction).order_by(Transaction.date).all()
   for t in transactions:
     if t.from_account != None:
       if t.from_account.customer == login_manager.customer:
         display_transaction(t)
     elif t.to_account != None:
       if t.to_account.customer == login_manager.customer:
         display_transaction(t)
   print self.intro
   return
 def do_2(self, args):
   print "Please choose which customer to add the account for:"
   customers = S.query(Customer).all()
   display_customers()
   customer_index = int(raw_input(">").strip())
   customer = customers[customer_index]
   acct_types = {
     0: 'checking',
     1: 'savings'
   }
   acct_type_prompt = "Please pick what type the account is:\n" + \
                      "  0. Checking\n" + \
                      "  1. Savings\n>"
   acct_type = acct_types[int(raw_input(acct_type_prompt).strip())]
   acct = Account(acct_type=acct_type, balance=0.0, customer=customer)
   S.add(acct)
   S.commit()
   print self.intro
   return
def get_customer(customer_id=None, username=None):
  if username:
    return S.query(CustomerModel) \
          .filter(CustomerModel.username==username).first()
  return S.query(CustomerModel) \
        .filter(CustomerModel.customer_id==customer_id).first()
def abort_if_customer_doesnt_exist(customer_id):
  customer = S.query(CustomerModel) \
    .filter(CustomerModel.customer_id==customer_id).first()
  if not customer:
    abort(404, error=doesnt_exist_error('customer')['error'])
  return
 def do_3(self, args):
   transactions = S.query(Transaction).order_by(Transaction.date).all()
   for t in transactions:
     display_transaction(t)
   print self.intro
   return
def display_customers():
  for customer in enumerate(S.query(Customer).all()):
    print "  " + str(customer[0]) + ". " + customer[1].username
  return
 def get(self, customer_id):
   abort_if_customer_doesnt_exist(customer_id)
   customer = get_customer(customer_id)
   stocks = S.query(StocksModel).filter(StocksModel.customer_id==customer_id).all()
   return stocks