代码拉取完成,页面将自动刷新
from flask import Blueprint, render_template, request, redirect, url_for
from datetime import datetime
from db import *
project_management = Blueprint('project_management', __name__)
# 在 Flask 应用中定义一个辅助函数
# 判断项目中是否有参与用户
def project_exists_user(project, user):
if project is None:
return False
return any(project_user.user == user for project_user in project.participated_users)
# 在上下文中添加辅助函数
@project_management.context_processor
def inject_helpers():
return dict(project_exists_user=project_exists_user)
# 项目列表
@project_management.route('/projects')
def projects():
user_group_query = UserGroup.query
group_id = request.args.get("group_id")
if group_id:
user_group_query = user_group_query.filter_by(id=group_id)
# 查询所有用户组
user_groups = user_group_query.all()
# 创建一个字典,用于存储每个用户组的项目列表
projects_by_group = {}
# 遍历每个用户组,查询属于该用户组的项目
for user_group in user_groups:
projects = Project.query.filter_by(group_id=user_group.id).all()
projects_by_group[user_group.id] = (user_group.name, projects)
# 还需要计算每个项目的进度情况
return render_template('projects.html', projects_by_group=projects_by_group)
@project_management.route('/project_delete/<int:project_id>')
def project_delete(project_id):
project = Project.query.get(project_id)
db.session.delete(project)
db.session.commit()
referer = request.headers.get('Referer')
return redirect(referer)
@project_management.route('/project_edit', methods=['GET', 'POST'])
def project_edit():
project_id = request.args.get("project_id")
project = None
if project_id:
project = Project.query.get(project_id)
if request.method == 'POST':
if project is None:
project = Project()
db.session.add(project)
# 获取表单数据并创建项目
project.name = request.form['name']
project.description = request.form.get('description')
project.level = request.form.get('level')
project.plan_start_time = parse_datetime(request.form.get('plan_start_time'))
project.plan_end_time = parse_datetime(request.form.get('plan_end_time'))
project.actual_start_time = parse_datetime(request.form.get('actual_start_time'))
project.actual_end_time = parse_datetime(request.form.get('actual_end_time'))
project.plan_days = db_use_day_calc(project.plan_start_time, project.plan_end_time)
project.actual_days = db_use_day_calc(project.actual_start_time, project.actual_end_time)
project.group_id = int(request.form['group_id'])
project.responsible_user_id = request.form['responsible_user_id']
# 清空项目的参与用户列表
project.participated_users = []
# 创建项目对象并添加到数据库
db.session.commit()
#如何更新
participated_users_ids = request.form.getlist('participated_users[]')
# 获取刚刚添加的项目的 ID
new_project_id = project.id
# 将参与用户添加到关联表
for user_id in participated_users_ids:
project_user = ProjectUser(project_id=new_project_id, user_id=user_id)
db.session.add(project_user)
db.session.commit()
referer = request.form.get('referer', '/project_management.projects')
return redirect(referer)
# 获取用户组列表和用户列表用于下拉选择
user_groups = UserGroup.query.all()
users = User.query.all()
project_levels = ["高级", "重要", "一般", "低级"]
referer = request.headers.get('Referer')
return render_template('project_edit.html',
project_levels=project_levels,
user_groups=user_groups,
users=users,
referer=referer,
project=project)
@project_management.route('/project_tasks/<int:project_id>')
def project_tasks(project_id):
project = Project.query.filter_by(id=project_id).first()
tasks = ProjectTask.query.filter_by(project_id=project_id).all()
return render_template('project_tasks.html', project=project, tasks=tasks)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。