修改登录和基础功能样式,目前已经支持钉钉登录和自定义登录注册
This commit is contained in:
101
routes.py
101
routes.py
@@ -1,9 +1,9 @@
|
||||
from datetime import datetime
|
||||
from flask import render_template, redirect, url_for, flash, request
|
||||
from math import ceil
|
||||
from flask_login import login_user, logout_user, login_required, current_user
|
||||
from __init__ import app, db
|
||||
from models import User, Demand
|
||||
from forms import DemandForm, AnswerForm
|
||||
from models import User, Demand, now_shanghai
|
||||
from forms import DemandForm, AnswerForm, LoginForm, RegisterForm
|
||||
|
||||
BRANCH_NAMES = {
|
||||
'comprehensive': '综合分会',
|
||||
@@ -21,6 +21,53 @@ def utility_processor():
|
||||
return BRANCH_NAMES.get(branch_key, branch_key)
|
||||
return dict(get_branch_name=get_branch_name)
|
||||
|
||||
@app.route('/login', methods=['GET', 'POST'])
|
||||
def login():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('index'))
|
||||
form = LoginForm()
|
||||
if form.validate_on_submit():
|
||||
user = User.query.filter_by(username=form.username.data).first()
|
||||
if user and user.check_password(form.password.data):
|
||||
login_user(user)
|
||||
flash('登录成功', 'success')
|
||||
next_page = request.args.get('next')
|
||||
return redirect(next_page) if next_page else redirect(url_for('index'))
|
||||
else:
|
||||
flash('用户名或密码错误', 'error')
|
||||
return render_template('login.html', form=form)
|
||||
|
||||
@app.route('/logout')
|
||||
@login_required
|
||||
def logout():
|
||||
logout_user()
|
||||
flash('已退出登录', 'info')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@app.route('/register', methods=['GET', 'POST'])
|
||||
def register():
|
||||
if current_user.is_authenticated:
|
||||
return redirect(url_for('index'))
|
||||
form = RegisterForm()
|
||||
if form.validate_on_submit():
|
||||
if form.password.data != form.confirm_password.data:
|
||||
flash('两次输入的密码不一致', 'warning')
|
||||
return render_template('register.html', form=form)
|
||||
existing_user = User.query.filter_by(username=form.username.data).first()
|
||||
if existing_user:
|
||||
flash('用户名已存在', 'warning')
|
||||
else:
|
||||
user = User(
|
||||
username=form.username.data,
|
||||
role='user'
|
||||
)
|
||||
user.set_password(form.password.data)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
flash('注册成功,请登录', 'success')
|
||||
return redirect(url_for('login'))
|
||||
return render_template('register.html', form=form)
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
user_id = request.args.get('userId')
|
||||
@@ -55,8 +102,12 @@ def before_request():
|
||||
|
||||
@app.route('/')
|
||||
def index():
|
||||
demands = Demand.query.filter_by(is_public=True).order_by(Demand.created_at.desc()).all()
|
||||
return render_template('index.html', demands=demands)
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = 10
|
||||
pagination = Demand.query.filter_by(is_public=True).order_by(Demand.created_at.desc()).paginate(
|
||||
page=page, per_page=per_page, error_out=False
|
||||
)
|
||||
return render_template('index.html', demands=pagination.items, pagination=pagination)
|
||||
|
||||
@app.route('/demand/new', methods=['GET', 'POST'])
|
||||
def new_demand():
|
||||
@@ -74,7 +125,7 @@ def new_demand():
|
||||
)
|
||||
db.session.add(demand)
|
||||
db.session.commit()
|
||||
flash('需求提交成功')
|
||||
flash('需求提交成功', 'success')
|
||||
return redirect(url_for('index'))
|
||||
return render_template('demand_form.html', form=form, title='提交新需求')
|
||||
|
||||
@@ -84,7 +135,7 @@ def edit_demand(id):
|
||||
return render_template('not_logged_in.html')
|
||||
demand = Demand.query.get_or_404(id)
|
||||
if not demand.can_edit(current_user):
|
||||
flash('无权限编辑此需求')
|
||||
flash('无权限编辑此需求', 'error')
|
||||
return redirect(url_for('index'))
|
||||
form = DemandForm(obj=demand)
|
||||
if form.validate_on_submit():
|
||||
@@ -94,9 +145,9 @@ def edit_demand(id):
|
||||
demand.contact = form.contact.data
|
||||
if current_user.is_admin() or not demand.answer:
|
||||
demand.is_public = form.is_public.data
|
||||
demand.updated_at = datetime.utcnow()
|
||||
demand.updated_at = now_shanghai()
|
||||
db.session.commit()
|
||||
flash('需求更新成功')
|
||||
flash('需求更新成功', 'success')
|
||||
return redirect(url_for('index'))
|
||||
return render_template('demand_form.html', form=form, title='编辑需求', demand=demand)
|
||||
|
||||
@@ -106,14 +157,14 @@ def answer_demand(id):
|
||||
return render_template('not_logged_in.html')
|
||||
demand = Demand.query.get_or_404(id)
|
||||
if not current_user.is_admin():
|
||||
flash('只有管理员可以回答需求')
|
||||
flash('只有管理员可以回答需求', 'error')
|
||||
return redirect(url_for('index'))
|
||||
form = AnswerForm(data={'answer': demand.answer or ''})
|
||||
if form.validate_on_submit():
|
||||
demand.answer = form.answer.data
|
||||
demand.answered_at = datetime.utcnow()
|
||||
demand.answered_at = now_shanghai()
|
||||
db.session.commit()
|
||||
flash('回答已保存')
|
||||
flash('回答已保存', 'success')
|
||||
return redirect(url_for('index'))
|
||||
return render_template('answer_form.html', form=form, demand=demand)
|
||||
|
||||
@@ -123,26 +174,34 @@ def toggle_public(id):
|
||||
return render_template('not_logged_in.html')
|
||||
demand = Demand.query.get_or_404(id)
|
||||
if not current_user.is_admin():
|
||||
flash('只有管理员可以修改公开状态')
|
||||
return redirect(url_for('index'))
|
||||
flash('只有管理员可以修改公开状态', 'error')
|
||||
return redirect(url_for(endpoint='admin_demands'))
|
||||
demand.is_public = not demand.is_public
|
||||
db.session.commit()
|
||||
flash('公开状态已更新')
|
||||
return redirect(url_for('index'))
|
||||
flash('公开状态已更新', 'success')
|
||||
return redirect(url_for('admin_demands'))
|
||||
|
||||
@app.route('/my_demands')
|
||||
def my_demands():
|
||||
if not current_user.is_authenticated:
|
||||
return render_template('not_logged_in.html')
|
||||
demands = Demand.query.filter_by(user_id=current_user.id).order_by(Demand.created_at.desc()).all()
|
||||
return render_template('my_demands.html', demands=demands)
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = 10
|
||||
pagination = Demand.query.filter_by(user_id=current_user.id).order_by(Demand.created_at.desc()).paginate(
|
||||
page=page, per_page=per_page, error_out=False
|
||||
)
|
||||
return render_template('my_demands.html', demands=pagination.items, pagination=pagination)
|
||||
|
||||
@app.route('/admin/demands')
|
||||
def admin_demands():
|
||||
if not current_user.is_authenticated:
|
||||
return render_template('not_logged_in.html')
|
||||
if not current_user.is_admin():
|
||||
flash('无权限访问此页面')
|
||||
flash('无权限访问此页面', 'error')
|
||||
return redirect(url_for('index'))
|
||||
demands = Demand.query.order_by(Demand.created_at.desc()).all()
|
||||
return render_template('admin_demands.html', demands=demands)
|
||||
page = request.args.get('page', 1, type=int)
|
||||
per_page = 10
|
||||
pagination = Demand.query.order_by(Demand.created_at.desc()).paginate(
|
||||
page=page, per_page=per_page, error_out=False
|
||||
)
|
||||
return render_template('admin_demands.html', demands=pagination.items, pagination=pagination)
|
||||
|
||||
Reference in New Issue
Block a user