208 lines
7.9 KiB
Python
208 lines
7.9 KiB
Python
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, now_shanghai
|
|
from forms import DemandForm, AnswerForm, LoginForm, RegisterForm
|
|
|
|
BRANCH_NAMES = {
|
|
'comprehensive': '综合分会',
|
|
'training': '培训服务分会',
|
|
'hr': '基础人事服务分会',
|
|
'talent': '人才服务分会',
|
|
'functional': '职能支持分会',
|
|
'finance_review': '经费审查委员会',
|
|
'women': '女职工委员会'
|
|
}
|
|
|
|
@app.context_processor
|
|
def utility_processor():
|
|
def get_branch_name(branch_key):
|
|
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')
|
|
name = request.args.get('name')
|
|
dept = request.args.get('dept')
|
|
|
|
if user_id or name or dept:
|
|
print(f'[后端] [7] ============== before_request 收到登录参数 ==============')
|
|
print(f'[后端] [7] userId={user_id}, name={name}, dept={dept}')
|
|
|
|
if user_id and not current_user.is_authenticated:
|
|
print(f'[后端] [7] 开始登录流程, userId={user_id}')
|
|
user = User.query.filter_by(dingtalk_userid=user_id).first()
|
|
|
|
if user:
|
|
print(f'[后端] [7] 用户已存在, 直接登录: {user.username}')
|
|
login_user(user)
|
|
elif name:
|
|
print(f'[后端] [7] 用户不存在, 创建新用户: {name}')
|
|
role = 'admin' if user_id == 'admin' else 'user'
|
|
user = User(
|
|
username=name,
|
|
dingtalk_userid=user_id,
|
|
dingtalk_name=name,
|
|
dingtalk_dept=dept or '',
|
|
role=role
|
|
)
|
|
db.session.add(user)
|
|
db.session.commit()
|
|
login_user(user)
|
|
print(f'[后端] [7] 用户创建并登录成功')
|
|
|
|
@app.route('/')
|
|
def index():
|
|
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():
|
|
if not current_user.is_authenticated:
|
|
return render_template('not_logged_in.html')
|
|
form = DemandForm()
|
|
if form.validate_on_submit():
|
|
demand = Demand(
|
|
title=form.title.data,
|
|
content=form.content.data,
|
|
branch=form.branch.data,
|
|
contact=form.contact.data,
|
|
is_public=form.is_public.data,
|
|
user_id=current_user.id
|
|
)
|
|
db.session.add(demand)
|
|
db.session.commit()
|
|
flash('需求提交成功', 'success')
|
|
return redirect(url_for('index'))
|
|
return render_template('demand_form.html', form=form, title='提交新需求')
|
|
|
|
@app.route('/demand/<int:id>/edit', methods=['GET', 'POST'])
|
|
def edit_demand(id):
|
|
if not current_user.is_authenticated:
|
|
return render_template('not_logged_in.html')
|
|
demand = Demand.query.get_or_404(id)
|
|
if not demand.can_edit(current_user):
|
|
flash('无权限编辑此需求', 'error')
|
|
return redirect(url_for('index'))
|
|
form = DemandForm(obj=demand)
|
|
if form.validate_on_submit():
|
|
demand.title = form.title.data
|
|
demand.content = form.content.data
|
|
demand.branch = form.branch.data
|
|
demand.contact = form.contact.data
|
|
if current_user.is_admin() or not demand.answer:
|
|
demand.is_public = form.is_public.data
|
|
demand.updated_at = now_shanghai()
|
|
db.session.commit()
|
|
flash('需求更新成功', 'success')
|
|
return redirect(url_for('index'))
|
|
return render_template('demand_form.html', form=form, title='编辑需求', demand=demand)
|
|
|
|
@app.route('/demand/<int:id>/answer', methods=['GET', 'POST'])
|
|
def answer_demand(id):
|
|
if not current_user.is_authenticated:
|
|
return render_template('not_logged_in.html')
|
|
demand = Demand.query.get_or_404(id)
|
|
if not current_user.is_admin():
|
|
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 = now_shanghai()
|
|
db.session.commit()
|
|
flash('回答已保存', 'success')
|
|
return redirect(url_for('index'))
|
|
return render_template('answer_form.html', form=form, demand=demand)
|
|
|
|
@app.route('/demand/<int:id>/toggle_public', methods=['POST'])
|
|
def toggle_public(id):
|
|
if not current_user.is_authenticated:
|
|
return render_template('not_logged_in.html')
|
|
demand = Demand.query.get_or_404(id)
|
|
if not current_user.is_admin():
|
|
flash('只有管理员可以修改公开状态', 'error')
|
|
return redirect(url_for(endpoint='admin_demands'))
|
|
demand.is_public = not demand.is_public
|
|
db.session.commit()
|
|
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')
|
|
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('无权限访问此页面', 'error')
|
|
return redirect(url_for('index'))
|
|
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)
|