def sign(): if request.method == 'POST': userdata = request.form.to_dict() print 'sign - userdata : ', userdata # user.save(userdata) # 把新注册用户写入数据库(在实际中,会利用js 在页面里过滤不合法的 用户名和密码,然后直接把数据放进去) # users = user.load(user_db_file) users = user.load() for ur in users: # if userdata['username'] == ur['name']: if userdata['username'] == ur[1]: return '<h1> User name already exists </h1>' if len(userdata['name']) <= 2: return '<h1> User name should be more than 2 bytes.</h2>' # 应该弹出 提示框 if userdata['password1'] == userdata['password']: del userdata['password1'] # 两个密码相同,只需要存一个好了,所以把另一个删掉 user.save(userdata) return '<h1> sign OK </h1>' else: return '<h1>前后密码不匹配,请重新输入密码</h1>' # 应该弹出 提示框 return render_template('sign.html')
def user_list(): username = request.cookies.get('username') if username == 'admin': usersdata = user.load() return render_template('user_list.html', users_info=usersdata) else: return "<h1> 当前用户无权限查看该页面</h1>"
def get_user_set(user_set): data = yaml.load(open('user.cache.yml', 'w+').read()) user_set = user.load() if data['basic'] != user_set: user_set = download_problem_list() else: user_set = data['crawl'] return uset_set
def download_user_set(): e_info('downloading user set') data = dict() user_set = user.load() data['basic'] = user_set for i in range(0, len(user_set)): user_set[i] = set_ac_list(user_set[i]) data['crawl'] = user_set open('user.cache.yml', 'w+').write(yaml.dump(data)) e_info('downloaded user set') return user_set
def retrieve_password(): if request.method == 'POST': user_email = request.form.to_dict() print 'user_email: ', user_email users = user.load() for ur in users: if ur[3] == user_email['email']: # 发送用户和密码 送给 该邮箱 print ur['user_name'], ur['password'] # return "<h1> 你好, 已经将密码发到 " + user_email['email'] + "</h1>" # UnicodeDecodeError: 'gbk' codec can't decode bytes in position 33-34: illegal multibyte sequence return "<h1> OK~ ,已经将密码发到你注册的邮箱</h1>" return "<h1> 咦? 这个邮箱还没有注册耶~ ... <br> come on ,baby ❤ ~ </h1>" return render_template('retrieve_password.html')
def edit_user(id): username = request.cookies.get('username') if username == 'admin': if user.search_id(id): edit_id_url = '/user/edit/' + str(id) default_data = user.search_id(id) if request.methods == 'POST': user_data = request.form.to_dict() user.update(id, user_data) users_data = user.load() return render_template('user_list.html', users_info=users_data) return render_template('user_edit.html', action_url=edit_id_url, default_info=default_data) else: return "<h1> 当前用户无权限查看该页面</h1>"
def edit_user(id): username = request.cookies.get('username') if username == 'admin': if user.search_id(id): default_user = user.search_id(id) print default_user if request.method == 'POST': user_data = request.form.to_dict() user.update(id, user_data) users_data = user.load() return render_template('user_list.html', user_id=id, users_info=users_data, default_info=default_user) return render_template('user_edit.html', user_id=id) else: return "<h1> 当前用户无权限查看该页面</h1>"
def add_user(): username = request.cookies.get('username') if username == 'admin': if request.methods == 'POST': userdata = request.form.to_dict() if userdata['password'] == userdata['password1']: del userdata['password1'] user.save(userdata) print "add user OK : ", userdata usersdata = user.load() return render_template('user_list.html', users_info=usersdata) # FIXME 添加成功后,进入 user_list 页面,查看资料 else: return render_template('user_add.html', tips="<h1>输入密码前后不一致,请重新设置</h1>") return render_template('user_add.html') # return flask.redirect(flask.url_for('login')) return "<h1> 当前用户无权限查看该页面</h1>"
def settings(name): username = request.cookies.get('username') if username == name: users_data = user.load() url = '/settings/' + str(name) if request.method == 'POST': user_password = request.form.to_dict() if user_password['password'] == user_password['password1']: del user_password['password1'] for ur in users_data: # if ur['name'] == name: if ur[1] == name: # ur['password'] = user_passwords['password1'] # user.cover(users_data) user.update(ur[0], user_password) return '<h1> 密码更改成功<h2>' # FIXME user return render_template('settings.html', username=name, action_url=url) else: return flask.redirect(flask.url_for('/login'))
def add_user(): username = request.cookies.get('username') if username == 'admin': if request.method == 'POST': userdata = request.form.to_dict() if userdata['password'] == userdata['password1']: del userdata['password1'] print userdata for i in userdata: userdata[i] = userdata[i].encode('utf-8') user.save(userdata) print "add user OK : ", userdata usersdata = user.load() return flask.redirect(flask.url_for('settings/user/list')) # FIXME 添加成功后,进入 user_list 页面,查看资料 else: return render_template('user_add.html', tips="<h1>输入密码前后不一致,请重新设置</h1>") return render_template('user_add.html') # return flask.redirect(flask.url_for('login')) return "<h1> 当前用户无权限查看该页面</h1>"
def login(): if request.method == 'POST': userdata = request.form.to_dict() # 载入数据库,遍历对比登录信息,当然,这个过程应该写成一个函数放到user.py里,这里就会清爽干净很多 print 'login - userdata : ', userdata # 这里可以调试debug 的时候看看有没有正确把login 里面的用户信息传送进来 users = user.load() for ur in users: # if userdata['name'] == ur["name"] and userdata['password']== ur['password']: if ur[1] == userdata['name']: if ur[2] == userdata['password']: print "login ok.\(^o^)/" # set cookies # response = flask.make_response(flask.redirect(flask.url_for('problems'))) # response.set_cookie('username', ur['name']) response = flask.make_response(flask.redirect(flask.url_for('index'))) response.set_cookie('username', ur[1]) return response else: print ur[1], userdata['name'] return '<h1> password is not suit for the username. </h1>' return render_template('login.html')
def login(): if request.method == 'POST': userdata = request.form.to_dict() # 载入数据库,遍历对比登录信息,当然,这个过程应该写成一个函数放到user.py里,这里就会清爽干净很多 print 'login - userdata : ', userdata # 这里可以调试debug 的时候看看有没有正确把login 里面的用户信息传送进来 users = user.load() for ur in users: # if userdata['name'] == ur["name"] and userdata['password']== ur['password']: if ur[1] == userdata['name']: if ur[2] == userdata['password']: print "login ok.\(^o^)/" # set cookies # response = flask.make_response(flask.redirect(flask.url_for('problems'))) # response.set_cookie('username', ur['name']) response = flask.make_response( flask.redirect(flask.url_for('index'))) response.set_cookie('username', ur[1]) return response else: print ur[1], userdata['name'] return '<h1> password is not suit for the username. </h1>' return render_template('login.html')