def login(acc_data): username = input('account: ') ac_log_obj = logger.set_ac_logger(username, 'access') max_try_cnt, try_cnt = 3, 0 # 运行最大尝试次数 all_user = data_handler.get_all_username() # 存在data/accounts文件夹下的所有用户 if username in all_user: # 用户名存在 user_db = data_handler.get_user_db(username) last_login_time = user_db['last_login_time'] if user_db['status'] == 0: # 正常状态 while try_cnt < max_try_cnt: password = input('Please input your password(quit:q): ') # 认证成功后欢迎信息 ,判断密码是否与真实密码相等 if user_db['password'] == password: welcome_login() # 欢迎信息 ac_log_obj.info('you are login') acc_data['is_authenticated'] = True acc_data['account_id'] = username return acc_data # 退出循环 elif password == 'q': # 取消重新登陆,直接退出程序 exit() else: # 未达到三次,最大尝试次数减一 try_cnt += 1 print('You have \033[1;35m%s times chance\033[0m' % (max_try_cnt - try_cnt)) else: # 输入三次错误后锁定 # localTime = time.strftime("%Y/%m/%d %H:%M:%S", time.localtime()) user_db['last_login_time'] = time.time() # 时间戳 user_db['status'] = 1 data_handler.set_user_db(username, user_db) ac_log_obj.error("too many login attempts") print("you have been locked...") exit() pass elif user_db['status'] == 1: # 锁定状态 print("you have been locked at the time:" "\033[1;32m %s\033[0m " % last_login_time) # 高亮显示锁定时间 pass elif user_db['status'] == 2: # 注销状态,即不可见 print("you have been revoke your account at the time: " "\033[1;32m %s\033[0m " % last_login_time) # 高亮显示吊销账号时间 pass pass else: # 用户名不存在,提示是否重新注册 want_add_user = input("Do you want add a new account (y/n)? ") if (want_add_user == 'y'): add_user = input('Please input a new account: ') # 输入用户名密码 add_passwd = input('Please input a passwd: ') user_db = default_db.default_db # 为新用户增加用户数据 user_db['id'] = add_user user_db['password'] = add_passwd data_handler.set_user_db(add_user, user_db) ac_log_obj = logger.set_ac_logger(username, 'access') ac_log_obj.info('account [%s] are login' % add_user) acc_data['is_authenticated'] = True acc_data['account_id'] = username return acc_data # 退出循环 else: exit()
def make_transaction(log_obj, user_db, tran_type, amount): amount = float(amount) if tran_type in settings.TRANSACTION_TYPE: interest = amount * settings.TRANSACTION_TYPE[tran_type]['interest'] # 利息 old_balance = user_db['balance'] # 旧款 if settings.TRANSACTION_TYPE[tran_type]['action'] == 'plus': # 当为加 new_balance = old_balance + amount - interest elif settings.TRANSACTION_TYPE[tran_type]['action'] == 'minus': # 为减时 new_balance = old_balance - amount - interest if new_balance < 0: # print('''\033[31;1mYour credit [%s] is not enough for this transaction [-%s], ' your current balance is [%s]''' %(user_db['credit'],(amount + interest), old_balance )) return user_db['balance'] = new_balance data_handler.set_user_db(user_db['id'], user_db) # 保存数据 # 保存相应日志到相应文件 log_obj.info('action:%s amount:%s interest:%s' %(tran_type, amount, interest)) return user_db else: print("\033[31;1mTransaction type [%s] is not exist!\033[0m" % tran_type) # def make_transfer(log_obj, tran_id, user_db, tran_type, amount): # # pass
def reset_passwd(): username = input('Please input a username: '******'[%s] is not been registered') else: user_db = data_handler.get_user_db(username) user_db['password'] = default_db.default_password data_handler.set_user_db(username, user_db) ac_log_obj = logger.set_ac_logger(username, 'access') ac_log_obj.info('reset password successful') print('\033[32;1maccount [%s] reset password successfull!\033[0m' % username)
def change_balance(): username = input('Please input a username: '******'No user named [%s]' % username) else: user_db = data_handler.get_user_db(username) new_balance = input('Please input new balance: ') # 输入旧密码 user_db['password'] = new_balance data_handler.set_user_db(username, user_db) ac_log_obj = logger.set_ac_logger(username, 'access') ac_log_obj.info('change balance successful') print('\033[32;1maccount [%s] change balance successfull\033[0m' % username)
def transfer(acc_data): ''' 转账 :param acc_data: :return: ''' account_data = data_handler.get_user_db(acc_data['account_id']) log_obj = logger.logger(account_data['id'], 'transaction') all_user = data_handler.get_all_username() # 存在data/accounts文件夹下的所有用户 current_balance = '''---------------- BALABCE INFO -------------- Credit : %s Balance: %s -------------------------------------------''' % (account_data['credit'], account_data['balance']) print(current_balance) back_flag = False while not back_flag: tran_id = input( '\033[1;34mInput what account you want transfer: \033[0m').strip() if tran_id in all_user: # 用户存在 tran_amount = input( '\033[1;34mInput withdraw amount: \033[0m').strip() if tran_amount.isdigit(): # 数字 new_balance = transaction.make_transaction( log_obj, account_data, 'withdraw', tran_amount) if new_balance: # 返回的 new_balance 有效,即钱够 # 汇款方进行相关操作并进行相关日志记录 tran_data = data_handler.get_user_db(tran_id) tran_data['balance'] += int(tran_amount) data_handler.set_user_db(tran_id, tran_data) traned_log_obj = logger.logger(tran_id, 'transaction') traned_log_obj.info( 'Received account %s transfer amount %s' % (account_data['id'], tran_amount)) print('New:Balance:\033[32;1m %s \033[0m' % new_balance['balance']) elif tran_amount == 'b': # 返回 back_flag = True elif tran_amount == 'q': # 退出程序 exit('请保管好您的信用卡,欢迎下次使用!') else: # 错误输入 print( '\033[31;1m[%s] is not a valid amount, only accept integer!\033[0m' % tran_amount) elif tran_id == 'b': # 返回 back_flag = True elif tran_id == 'q': # 退出程序 exit('请保管好您的信用卡,欢迎下次使用!') else: # 错误输入 print('\033[31;1m[%s] is not a valid id' % tran_id)
def registered_user(): username = input('Please input a username: '******'[%s] has been registered' % username) else: password = input('Please input password: '******'id'] = username user_db['password'] = password data_handler.set_user_db(username, user_db) ac_log_obj = logger.set_ac_logger(username, 'access') ac_log_obj.info('registered successful') print('\033[32;1maccount [%s] registered successfull\033[0m' % username)
def unfreeze(): ''' 解冻用户 :return: ''' username = input('Please input a username: '******'No user named [%s]' % username) else: user_db = data_handler.get_user_db(username) user_db['status'] = 0 data_handler.set_user_db(username, user_db) ac_log_obj = logger.set_ac_logger(username, 'access') ac_log_obj.info('freeze username [%s] successful!' % username) print('freeze username [%s] successful!' % username)
def change_passwd(): username = input('Please input a username: '******'No user named [%s]' % username) else: user_db = data_handler.get_user_db(username) old_passwd = input('Please input old password: '******'password']: new_passwd = input('Please input new password: '******'Please input new password: '******'': user_db['password'] = new_passwd data_handler.set_user_db(username, user_db) ac_log_obj = logger.set_ac_logger(username, 'access') ac_log_obj.info('change password successful') print('\033[32;1maccount [%s] change password successfull\033[0m' % username) else: print('You have input two different password') else : print('You input an error password')