def submit_item(): form_one = ProjectForm() form_two = MainForm() if request.method == 'POST': # project数据库对象存储 project_id = 1 project_name = form_one.project_name.data project_ST = form_one.project_ST.data project_FT = form_one.project_FT.data db_project = DBProject(project_id=project_id, project_name=project_name, project_ST=project_ST, project_FT=project_FT) print(db_project) db.session.add(db_project) # item数据库对象存储 item_id = 1 for item in form_two.items.data: item_dict = dict( item) # 每个item是一个dict类型,但是不能用dict的方法,需要先转换成dict类型的变量 item_keys_list = list(item_dict.keys()) # 字典中的键列表 item_values_list = list(item_dict.values()) # 字典中的值列表 print(item_keys_list) print(item_values_list) item_name = item_values_list[1] # item类的前置事件获取 item_pre_list = item_values_list[2] item_pre = '' if item_pre_list is not None: for pre in item_pre_list: item_pre += pre item_pre += ',' item_pre = 'null' if (item_pre == '') else item_pre[:-1] item_LT = item_values_list[3] db_item = DBItem(item_id=item_id, item_name=item_name, item_pre=item_pre, item_LT=item_LT) print(db_item) item_id += 1 db.session.add(db_item) # 数据库提交 try: db.session.commit() except Exception as e: db.session.rollback() print(e) return render_template('try.html', form_one=form_one, form_two=form_two)
def view(): projectInfo = ProjectForm() print('查看的project_id') print(request.args.get('project_id')) project = DBProject.query.filter_by( project_id=request.args.get('project_id')).first() projectInfo.project_id = project.project_id projectInfo.project_name = project.project_name getproject_ST = datetime.datetime.strptime(project.project_ST, '%Y-%m-%d') projectInfo.project_ST = getproject_ST.strftime('%Y-%m-%d') getproject_FT = datetime.datetime.strptime(project.project_FT, '%Y-%m-%d') projectInfo.project_FT = getproject_FT.strftime('%Y-%m-%d') return render_template('view.html', title='view', projectInfo=projectInfo)
def new(): print('--------------------new--------------------') form_one = ProjectForm() if request.method == 'GET': print('GET') return render_template('new.html', title='new', form_one=form_one) if request.method == 'POST': if not form_one.validate_on_submit(): return render_template('new.html', title='new', form_one=form_one) else: tempSQLData = [[], []] tempProject = { 'ID': DBProject.query.count(), 'name': form_one.project_name.data, 'startTime': form_one.project_ST.data.strftime('%Y-%m-%d'), 'finishTime': form_one.project_FT.data.strftime('%Y-%m-%d') } tempSQLData[0].append(tempProject) form_two = request.form.to_dict(flat=False) print(form_two) item_key_list = list(form_two.keys())[5:] item_value_list = list(form_two.values())[5:] print(item_key_list) print(item_value_list) i = 0 if item_value_list: j = int(str(item_key_list[-1]).split('-')[1]) while i <= j: item_id = i + 1 item_index = item_key_list.index('items-' + str(i) + '-item_name') item_name = item_value_list[item_index][0] item_pre = '' item_LT = '' if item_key_list[item_index + 1] == 'items-' + str(i) + '-item_pre': for pre in item_value_list[item_index + 1]: if pre not in ['0', '无']: item_pre = item_pre + getIdByName( item_key_list, item_value_list, pre) + ' ' + pre + ',' item_pre = '00' if (item_pre == '') else item_pre[:-1] item_LT = item_value_list[item_index + 2][0] else: item_pre = '00' item_LT = item_value_list[item_index + 1][0] i += 1 tempSQLData[1].append({ 'ID': int(item_id), 'LT': int(item_LT), 'name': item_name, 'pre': item_pre }) # 计算 p = Project() tempSQLData = TranslateTempSQLData(tempSQLData) isCircle = p.readDataFromSQL(tempSQLData) overDue = p.duartion_OK() p.graph.calculateCoordinates([1480, 400]) p.graph.info() print(overDue) if overDue is False: return render_template('new.html', title='new', form_one=form_one, overDue=overDue) if isCircle is True: return render_template('new.html', title='new', form_one=form_one, isCircle=isCircle) else: # project数据库对象存储 db_project = DBProject( project_name=form_one.project_name.data, project_ST=form_one.project_ST.data, project_FT=form_one.project_FT.data, user=current_user.get_id()) print(db_project) db.session.add(db_project) # item数据库对象存储 max_id = db.session.query(db.func.max( DBProject.project_id)).scalar() item_project_id = db.session.query(DBProject).filter( DBProject.project_id == max_id).first().project_id # 若有多个相同值用all() print(item_project_id) for Knot in p.graph.knotList: pre_item = '' suf_item = '' for pre in Knot.pre_item: pre_item += pre pre_item += ',' for suf in Knot.suf_item: suf_item += suf suf_item += ',' db_item = DBItem( item_name=Knot.name, item_pre_item=pre_item[:-1], item_suf_item=suf_item[:-1], item_last_time=Knot.last_time, item_earliest_start_time=Knot.earliest_start_time. strftime('%Y-%m-%d'), item_earliest_finish_time=Knot. earliest_finish_time.strftime('%Y-%m-%d'), item_latest_start_time=Knot.latest_start_time. strftime('%Y-%m-%d'), item_latest_finish_time=Knot.latest_finish_time. strftime('%Y-%m-%d'), item_free_time_difference=Knot. free_time_difference, item_total_time_difference=Knot. total_time_difference, item_X=Knot.X, item_Y=Knot.Y, item_is_key=Knot.is_key, item_toPoID=Knot.toPoID, project=item_project_id) print(db_item) db.session.add(db_item) else: # project数据库对象存储 db_project = DBProject(project_name=form_one.project_name.data, project_ST=form_one.project_ST.data, project_FT=form_one.project_FT.data, user=current_user.get_id()) print(db_project) db.session.add(db_project) try: db.session.commit() print('new表单成功提交') except Exception as e: db.session.rollback() print('new表单提交失败') print(e) print('project.html') return redirect(url_for('project'))
def change(): print('--------------------change--------------------') if request.method == 'GET': project = DBProject.query.filter_by( project_id=request.args.get('project_id')).first() project_data = { 'project_id': project.project_id, 'project_name': project.project_name, 'project_ST': datetime.datetime.strptime(project.project_ST, '%Y-%m-%d'), 'project_FT': datetime.datetime.strptime(project.project_FT, '%Y-%m-%d'), } form_one = ProjectForm(**project_data) # print('√1') return render_template('change.html', project_id=request.args.get('project_id'), title='change', form_one=form_one) if request.method == 'POST': form_one = ProjectForm() if not form_one.validate_on_submit(): # print('√2') return render_template('change.html', project_id=request.args.get('project_id'), title='change', form_one=form_one) else: tempSQLData = [[], []] tempProject = {'ID': 0, 'name': 0, 'startTime': 0, 'finishTime': 0} form_one = ProjectForm() # project数据库对象存储 tempProject['name'] = form_one.project_name.data tempProject['startTime'] = form_one.project_ST.data.strftime( '%Y-%m-%d') tempProject['finishTime'] = form_one.project_FT.data.strftime( '%Y-%m-%d') tempSQLData[0].append(tempProject) # item数据库对象存储 # 先删除所有project=project_id的Items delete_item_list = DBItem.query.filter_by( project=request.args.get('project_id')).all() for item in delete_item_list: db.session.delete(item) form_two = request.form.to_dict(flat=False) print(form_two) item_key_list = list(form_two.keys())[5:] item_value_list = list(form_two.values())[5:] # print(item_key_list) # print(item_value_list) i = 0 if item_value_list: j = int(str(item_key_list[-1]).split('-')[1]) while i <= j: item_id = i + 1 item_index = item_key_list.index('items-' + str(i) + '-item_name') item_name = item_value_list[item_index][0] item_pre = '' item_LT = '' if item_key_list[item_index + 1] == 'items-' + str(i) + '-item_pre': for pre in item_value_list[item_index + 1]: if pre not in ['0', '无']: item_pre = item_pre + getIdByName( item_key_list, item_value_list, pre) + ' ' + pre + ',' item_pre = '00' if (item_pre == '') else item_pre[:-1] item_LT = item_value_list[item_index + 2][0] else: item_pre = '00' item_LT = item_value_list[item_index + 1][0] i += 1 tempSQLData[1].append({ 'ID': int(item_id), 'LT': int(item_LT), 'name': item_name, 'pre': item_pre }) # 计算 p = Project() tempSQLData = TranslateTempSQLData(tempSQLData) isCircle = p.readDataFromSQL(tempSQLData) overDue = p.duartion_OK() p.graph.info() print(overDue) if overDue is False: # print('√3') return render_template( 'change.html', project_id=request.args.get('project_id'), title='change', form_one=form_one, overDue=overDue) if isCircle is True: # print('√4') return render_template( 'change.html', project_id=request.args.get('project_id'), title='change', form_one=form_one, isCircle=isCircle) else: # 更新DBProject数据库数据 DBProject.query.filter_by( project_id=request.args.get('project_id')).update({ "project_name": form_one.project_name.data, "project_ST": form_one.project_ST.data, "project_FT": form_one.project_FT.data }) for Knot in p.graph.knotList: pre_item = '' suf_item = '' for pre in Knot.pre_item: pre_item += pre pre_item += ',' for suf in Knot.suf_item: suf_item += suf suf_item += ',' db_item = DBItem( item_name=Knot.name, item_pre_item=pre_item[:-1], item_suf_item=suf_item[:-1], item_last_time=Knot.last_time, item_earliest_start_time=Knot.earliest_start_time. strftime('%Y-%m-%d'), item_earliest_finish_time=Knot. earliest_finish_time.strftime('%Y-%m-%d'), item_latest_start_time=Knot.latest_start_time. strftime('%Y-%m-%d'), item_latest_finish_time=Knot.latest_finish_time. strftime('%Y-%m-%d'), item_free_time_difference=Knot. free_time_difference, item_total_time_difference=Knot. total_time_difference, item_X=Knot.X, item_Y=Knot.Y, item_is_key=Knot.is_key, item_toPoID=Knot.toPoID, project=request.args.get('project_id')) print(db_item) db.session.add(db_item) else: # project数据库对象存储 db_project = DBProject(project_name=form_one.project_name.data, project_ST=form_one.project_ST.data, project_FT=form_one.project_FT.data, user=current_user.get_id()) print(db_project) db.session.add(db_project) try: db.session.commit() print('成功提交') except Exception as e: db.session.rollback() print('提交失败') projectInfo = ProjectForm() projectInfo.project_id = request.args.get('project_id') projectInfo.project_name = form_one.project_name.data projectInfo.project_ST = form_one.project_ST.data projectInfo.project_FT = form_one.project_FT.data # print('√5') return render_template('view.html', project_id=request.args.get('project_id'), title='view', projectInfo=projectInfo)