初始提交:人事共享服务中心钉钉登录功能
This commit is contained in:
148
routes.py
Normal file
148
routes.py
Normal file
@@ -0,0 +1,148 @@
|
||||
from datetime import datetime
|
||||
from flask import render_template, redirect, url_for, flash, request
|
||||
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
|
||||
|
||||
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.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():
|
||||
demands = Demand.query.filter_by(is_public=True).order_by(Demand.created_at.desc()).all()
|
||||
return render_template('index.html', demands=demands)
|
||||
|
||||
@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('需求提交成功')
|
||||
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('无权限编辑此需求')
|
||||
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 = datetime.utcnow()
|
||||
db.session.commit()
|
||||
flash('需求更新成功')
|
||||
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('只有管理员可以回答需求')
|
||||
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()
|
||||
db.session.commit()
|
||||
flash('回答已保存')
|
||||
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('只有管理员可以修改公开状态')
|
||||
return redirect(url_for('index'))
|
||||
demand.is_public = not demand.is_public
|
||||
db.session.commit()
|
||||
flash('公开状态已更新')
|
||||
return redirect(url_for('index'))
|
||||
|
||||
@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)
|
||||
|
||||
@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('无权限访问此页面')
|
||||
return redirect(url_for('index'))
|
||||
demands = Demand.query.order_by(Demand.created_at.desc()).all()
|
||||
return render_template('admin_demands.html', demands=demands)
|
||||
Reference in New Issue
Block a user