def add_user(mobile, username, password, is_admin=None): """ 添加用户 """ try: u = User({'mobile': mobile, 'username': username, 'password': password}) if is_admin is not None: u._is_admin = is_admin print(u._data) u.upsert() log.info('创建/更新用户{}成功!'.format(u._id)) return True except Exception as e: log.exception(str(e)) return False
def add_account(user, exchange, login_name, login_password, money_password='', bank_password=''): """ 添加账号 """ try: u = User.query_one({'_id': user}) if not u: log.info('找不到用户{}, 无法创建账号'.format(user)) return False if exchange not in exchanges: log.info('找不到交易所{}, 无法创建账号'.format(exchange)) return False a = Account({ 'user': user, 'exchange': exchange, 'login_name': login_name, 'login_password': login_password, 'money_password': money_password, 'bank_password': bank_password, }) a.upsert() log.info('创建/更新账号{}成功'.format(a._id)) return True except Exception as e: log.exception(str(e)) return False
def user_login(): mobile = request.form.get('mobile') password = request.form.get('password') user = User.query_one({'_id': mobile}) if user and user.password == password: login_user(user) return redirect(request.args.get('next', '/'))
def update_user(user): """ 更新用户的账号信息 """ accounts = list(Account.query({'user': user})) u = User.query_one({'_id': user}) for key, value in accounts_summary(accounts).items(): setattr(u, key, value) u.num_accounts = len(accounts) u.num_exchanges = len(set(a.exchange for a in accounts)) u.upsert()
def update_users(users=None): for user in users or [u._id for u in User.query()]: update_user(user)
def load_user(user_id): return User.query_one({'_id': user_id})
def delete_user(): user = request.form.get('user') User.delete_one({'_id': user}) return jsonify(status=200)
def admin_account(): users = list(User.query()) accounts = list(Account.query()) locals()['exchanges'] = exchanges return render_template('admin/account.html', **locals())
def admin_user(): users = list(User.query()) return render_template('admin/user.html', **locals())
def admin(): num_users = User.count() num_accounts = Account.count() return render_template('admin/index.html', **locals())