def login(): """ 登录 """ if g.user is not None and g.user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if request.method == 'POST': if form.validate_on_submit(): from user_auth import get_user_auth_row condition = { 'auth_type': 'email', 'auth_key': form.email.data, 'auth_secret': form.password.data } user_auth_info = get_user_auth_row(**condition) if user_auth_info is None: flash(u'%s, You were logged failed' % form.email.data, 'warning') return render_template('login.html', title='login', form=form) if user_auth_info.verified == 0: flash(u'%s, Please verify email address in mailbox' % form.email.data, 'warning') return render_template('login.html', title='login', form=form) # session['logged_in'] = True # 用户通过验证后,记录登入IP from user import edit_user edit_user(user_auth_info.user_id, {'last_ip': request.headers.get('X-Forwarded-For', request.remote_addr)}) # 用 login_user 函数来登入他们 from user import get_user_row_by_id login_user(get_user_row_by_id(user_auth_info.user_id)) flash(u'%s, You were logged in' % form.email.data, 'success') return redirect(request.args.get('next') or url_for('index')) flash(form.errors, 'warning') # 调试打开 return render_template('login.html', title='login', form=form)
def profile(user_id): if not auth.is_logged_in(session): return redirect_to_login(STRINGS['SIGNED_OUT']) if int(user_id) == session['user_id'] or auth.is_admin(session): if request.method == 'POST': if auth.is_admin(session): email = request.form['email'] name = request.form['user-name'] suspended = request.form.get('suspended') is_admin = request.form.get('role') if suspended is None: suspended = 0 if is_admin == 'admin': is_admin = 1 else: is_admin = 0 user.edit_user_admin(user_id, email, name, suspended, is_admin) else: email = request.form['email'] name = request.form['user-name'] app.logger.debug(user_id, email, name) user.edit_user(user_id, email, name) flash("Changes saved successfully", category="success") return redirect(url_for('profile', user_id=user_id)) else: return render_template("profile.html", session=session, user=user.get_user_by_id(user_id)) else: return redirect_to_home(STRINGS['NOT_AUTHORIZED'])
def setting(): # return "Hello, World!\nSetting!" form = UserForm(request.form) if request.method == 'GET': # from user import get_user_row_by_id # user_info = get_user_row_by_id(user.id) # if user_info: form.email.data = current_user.email form.password.data = current_user.password form.nickname.data = current_user.nickname form.birthday.data = current_user.birthday form.create_time.data = current_user.create_time form.update_time.data = current_user.update_time form.last_ip.data = current_user.last_ip if request.method == 'POST': if form.validate_on_submit(): # todo 判断邮箱是否重复 from user import edit_user from datetime import datetime user_info = { 'email': form.email.data, 'nickname': form.nickname.data, 'birthday': form.birthday.data, 'update_time': datetime.utcnow(), 'last_ip': request.remote_addr, } result = edit_user(current_user.id, user_info) if result == 1: flash(u'Edit Success', 'success') if result == 0: flash(u'Edit Failed', 'warning') flash(form.errors, 'warning') # 调试打开 flash(u'Hello, %s' % current_user.email, 'info') # 测试打开 return render_template('setting.html', title='setting', form=form)
def setting(): """ 设置 """ # return "Hello, World!\nSetting!" form = UserForm(request.form) if request.method == 'GET': from user import get_user_row_by_id user_info = get_user_row_by_id(current_user.id) if user_info: form.nickname.data = user_info.nickname form.avatar_url.data = user_info.avatar_url form.email.data = user_info.email form.phone.data = user_info.phone form.birthday.data = user_info.birthday form.create_time.data = user_info.create_time form.update_time.data = user_info.update_time form.last_ip.data = user_info.last_ip if request.method == 'POST': if form.validate_on_submit(): # todo 判断邮箱是否重复 from user import edit_user from datetime import datetime user_info = { 'nickname': form.nickname.data, 'avatar_url': form.avatar_url.data, 'email': form.email.data, 'phone': form.phone.data, 'birthday': form.birthday.data, 'update_time': datetime.utcnow(), 'last_ip': request.headers.get('X-Forwarded-For', request.remote_addr), } result = edit_user(current_user.id, user_info) if result == 1: flash(u'Edit Success', 'success') if result == 0: flash(u'Edit Failed', 'warning') flash(form.errors, 'warning') # 调试打开 flash(u'Hello, %s' % current_user.email, 'info') # 测试打开 return render_template('setting.html', title='setting', form=form)