def freeze(user_data): """freeze account function""" user_data['acc_data']['status'] = 2 db_connection.db_api('update accounts where account = %s' % user_data['acc_data']['user'], acc_data=user_data['acc_data']) exit('\033[34;1mFreezing account successfully!\033[0m')
def modify_credit(user_data): """function of modify the account credit""" # before modifying account credits, make sure the account credits already repay if float(user_data['acc_data']['remain_amount']) == float( user_data['acc_data']['credit']): print('Your current credit:', user_data['acc_data']['credit']) while True: new_credit = input( 'Please enter the new credit.(between 0 to 15000)').strip() if new_credit.isdigit() and 0 < float(new_credit) <= 15000: user_data['acc_data']['credit'] = new_credit user_data['acc_data']['remain_amount'] = new_credit db_connection.db_api('update accounts where account = %s' % user_data['acc_data']['user'], acc_data=user_data['acc_data']) exit( '\033[34;1mModify the account credits successfully!\033[0m' ) else: print( '\033[31;1mPlease enter a number between 0 to 15000!\033[0m' ) else: print( '\033[31;1mPlease repay your credit before modifying your account credits\033[0m' ) print('After 3 seconds, you will enter to the repayment interface' + '\n' + '3'.center(15, '*')) time.sleep(1) print('2'.center(15, '*')) time.sleep(1) print('1'.center(15, '*')) time.sleep(1) main.pay_back(user_data)
def locking(acc_data): """ account locking function, if one account try to login too many time, it will be locked. :param acc_data: account information :return: account information, after locking """ loc_time = str(datetime.date.today()) acc_data['status'] = 1 acc_data['lock_time'] = loc_time db_connection.db_api('update accounts where account = %s' % acc_data['user'], acc_data=acc_data)
def transfer(user_data): """For user transfer to others""" acc_data = user_data['acc_data'] while True: other_acc = input('Other account:').strip() if other_acc == acc_data['user']: print("\033[31;1mCan't transfer to yourself\033[0m") continue else: other_acc_data = db_connection.db_api( 'select * from accounts where account = %s' % other_acc) while other_acc_data: print('Your remain amount:', '%.2f' % float(acc_data['remain_amount'])) amount = input('Transfer amount:').strip() if amount.isdigit(): acc_data['remain_amount'] = '%.2f' % ( float(acc_data['remain_amount']) - float(amount)) other_acc_data['remain_amount'] = '%.2f' % ( float(other_acc_data['remain_amount']) + float(amount)) print(other_acc_data) acc_data = transaction(transaction_log, 'transfer', amount, acc_data=acc_data, other_acc_data=other_acc_data) user_data['acc_data'] = acc_data auto_back(user_data) else: print('please enter a number') else: print('Please enter again.')
def add_acc(): """add a new account function""" print( '\033[34;1mYou will have 15000 credits after the account is created successfully.\033[0m' ) while True: user = input('Account:').strip() sql = 'select account from accounts where account = %s' % user if not db_connection.db_api(sql): password1 = input('Password:'******'Confirm password:'******'\033[34;1mAdd account successfully!\033[0m') print( 'After 3 seconds, you will enter to the login interface' + '\n' + '3'.center(15, '*')) time.sleep(1) print('2'.center(15, '*')) time.sleep(1) print('1'.center(15, '*')) time.sleep(1) main.atm(setting.user_data) else: print( '\033[31;1mThe password you entered twice is different!\033[0m' ) else: print( '\033[31;1mThe account you entered already exists, please enter again!\033[0m' )
def transaction(log_obj, trans_type, amount, **kwargs): """ deal with the user's transactions :param log_obj: transaction logger :param trans_type: transaction type :param amount: transaction amount :param kwargs: account data or other account data :return: account data, after transaction """ acc_data = kwargs.get('acc_data') if 0 <= float(acc_data['remain_amount']) <= 15000: if trans_type == 'transfer': other_acc_data = kwargs.get('other_acc_data') if 0 <= float(other_acc_data['remain_amount']) <= 15000: db_connection.db_api('update accounts where account = %s' % other_acc_data['user'], acc_data=other_acc_data) log_obj.info('account [%s] %s %s' % (other_acc_data['user'], 'income', amount)) else: log_obj.info('account [%s] %s failed' % (acc_data['user'], trans_type)) print( '\033[31;1mExceeding the maximum credit limit of the other account\033[0m' ) print('\033[31;1mTransaction failed!\033[0m') acc_data['remain_amount'] = '%.2f' % ( float(acc_data['remain_amount']) + float(amount)) return acc_data db_connection.db_api('update accounts where account = %s' % acc_data['user'], acc_data=acc_data) log_obj.info('account [%s] %s %s' % (acc_data['user'], 'expenditure', amount)) print('Account [%s] %s successfully!' % (acc_data['user'], trans_type)) return acc_data else: log_obj.info('account [%s] %s failed' % (acc_data['user'], trans_type)) print( '\033[31;1mYour remain amount is not enough for this transaction!\033[0m' ) print('\033[31;1mTransaction failed!\033[0m') acc_data['remain_amount'] = '%.2f' % ( float(acc_data['remain_amount']) + float(amount)) return acc_data
def unlock(acc_data): """ account unlock function, after one day, a locked account will be unlock. :param acc_data: account information :return: account information, after unlock """ local_time = datetime.date.today() if acc_data['status'] == 0: return acc_data elif acc_data['status'] == 1: if local_time >= datetime.date(*map(int, acc_data['lock_time'].split('-'))) + datetime.timedelta(days=1): acc_data['status'] = 0 del acc_data['lock_time'] db_connection.db_api('update accounts where account = %s' % acc_data['user'], acc_data=acc_data) return acc_data else: return acc_data elif acc_data['status'] == 2: exit('\033[31;1mYour account is disabled!\033[0m')
def atm_api(user_data, *args): """For shopping mall connect ATM""" data = auth.login(user_data, access_log) acc_data = data['acc_data'] if args: amount = args[0] money = float(acc_data['remain_amount']) - float(amount) if money >= 0: acc_data['remain_amount'] = '%.2f' % money sql = 'update accounts where account = %s' % acc_data['user'] if not db_connection.db_api(sql, acc_data=acc_data): return acc_data else: exit( '\033[34;1mYour remain amount is insufficient, failed purchase!\033[0m' ) else: return acc_data
def login(user_data, log_obj): """ account login function :param user_data: user info data, only save in memory :param log_obj: use to record the user's login logging :return: if login, return user info data, otherwise, return None """ while True: user = input('Account:').strip() account = db_connection.db_api('select account from accounts where account = %s' % user) acc_file = '%s/db/accounts/%s.json' % (setting.base_dir, account) if account: with open(acc_file, 'r') as f: acc_data = unlock(json.load(f)) count = 0 while count < 3: password = initial_acc.encryption(input('Password:'******'password']: if acc_data['status'] == 0: if datetime.date.today() > datetime.date(*map(int, acc_data['expire_date'].split('-'))): print('\033[31;1mYour account has expired, please apply a new credit card!\033[0m') else: log_obj.info('account [%s] login' % account) user_data['account'] = account user_data['login'] = True user_data['acc_data'] = acc_data return user_data elif acc_data['status'] == 1: exit('\033[31;1mYour account is locked!\033[0m') elif acc_data['status'] == 2: exit('\033[31;1mYour account is disabled!\033[0m') else: count += 1 print('\033[31;1mYour password is incorrect!\033[0m') else: locking(acc_data) log_obj.error('too many login attempt, account [%s] has been locked' % account) print() exit('\033[31;1mToo many login attempt, your account has been locked! please try tomorrow.\033[0m') else: pass