def get_data(self, task_name): try: task = main_task.select( main_task.startTime, main_task.endTime, main_task.leader, main_task.member, main_task.describe, main_task.childTask).where(main_task.name == task_name) if not task: return {"info": "no such task"} task[0].startTime = task[0].startTime.date() task[0].endTime = task[0].endTime.date() # 将成员Id转换为名字 members = [] task[0].member = ast.literal_eval(task[0].member) for member_id in task[0].member: member = user.select(user.userName).where(user.id == member_id) members.append(str(member[0].userName)) task = model_to_dict(task[0]) # 将子任务id转换为详情 task_detail = [] if task["childTask"] != "": task["childTask"] = ast.literal_eval(task["childTask"]) for task_id in task["childTask"]: sub_task = child_task.select( child_task.name, child_task.person, child_task.childWeight, child_task.status, child_task.endTime, child_task.describe).where(child_task.id == task_id) sub_task[0].endTime = sub_task[0].endTime.date() sub_task = model_to_dict(sub_task[0]) sub_task[ "status"] = "完成" if sub_task["status"] == 1 else "未完成" task_detail.append({ "misname": sub_task["name"], "mispeo": sub_task["person"], "misper": sub_task["childWeight"], "miscon": sub_task["status"], "misend": str(sub_task["endTime"]), "misdetail": sub_task["describe"] }) data = { "start": str(task["startTime"]), "end": str(task["endTime"]), "actor": task["leader"], "groPeo": members, "desc": task["describe"], "TaskDetail": task_detail } return json.dumps(data, ensure_ascii=False) except Exception as e: print(e) return {"info": "database error"}
def get_data(self, task_name): try: tasks = main_task.select( main_task.startTime, main_task.endTime, main_task.leader, main_task.member, main_task.describe, main_task.childTask).where(task_name == main_task.name) # 转换为dict tasks[0].startTime = str(tasks[0].startTime.date()) tasks[0].endTime = str(tasks[0].endTime.date()) tasks = model_to_dict(tasks[0]) # 将id转换为组员名字 members = ast.literal_eval(tasks[0].member) members_str = "" for member_id in members: member = user.select(user.userName).where(user.id == member_id) members_str = members_str + str(member[0].userName) + "," members_str = members_str[:-1] # 将id转换为子任务信息 child_tasks = [] childs = ast.literal_eval(tasks[0].childTask) for child_id in childs: sub_task = child_task.select( child_task.name, child_task.person, child_task.childWeight, child_task.status, child_task.endTime, child_task.describe).where(child_id == child_task.id) sub_task[0].endTime = str(sub_task[0].endTime.date()) sub_task = model_to_dict(sub_task[0]) sub_task["status"] = "完成" if int(sub_task["status"]) else "未完成" child_tasks.append({ "misname": sub_task["name"], "mispeo": sub_task["person"], "miscon": sub_task["status"], "misend": sub_task["endTime"], "misdetail": sub_task["describe"] }) result = { "start": tasks["startTime"], "end": tasks["endTime"], "actor": tasks["leader"], "groPeo": members_str, "desc": tasks["describe"], "TaskDetail": child_tasks } return json.dumps(result, ensure_ascii=False) except: return {"err": "databse error"}
def update_data(self, task_name, child_data): try: child_tasks = child_task.select( child_task.person, child_task.childWeight, child_task.id, child_task.status).where(child_task.mainTask == task_name) # 判断是否为第一次插入,如果为第一次直接插入,否则先更改数据库,再删除,最后插入 if not child_tasks: self.insert_data(task_name, child_data) return {"info": "insert success"} else: # 对于修改记录的操作,取出数据库中之前的子任务,依次遍历对user进行修改 # 修改完后删除子任务,重新插入 id_list = [] for child in child_tasks: child = model_to_dict(child) id_list.append(int(child["id"])) # 对user修改 user_data = user.select( user.taskNowTime, user.taskFinished, user.childID).where(user.userName == child["person"]) user_data = model_to_dict(user_data[0]) user_data["taskFinished"] = int( user_data["taskFinished"]) - int(child["childWeight"]) if not child["status"]: user_data["taskNowTime"] = int( user_data["taskNowTime"]) - int( child["childWeight"]) user_data["childID"] = ast.literal_eval( user_data["childID"]) user_data["childID"].remove(int(child["id"])) user_data["childID"] = str(user_data["childID"]) user.update( **{ "taskFinished": user_data["taskFinished"], "taskNowTime": user_data["taskNowTime"], "childID": user_data["childID"] }).where(user.userName == child["person"]).execute() # 删除子任务 child_task.delete().where( child_task.mainTask == task_name).execute() self.insert_data(task_name, child_data) return {"info": "update success"} except Exception as e: return {"info": "database error"}
def insert_data(self, task_name, child_data): id_list = [] for data in child_data: data["misend"] = data["misend"][:10] data["miscon"] = 1 if data["miscon"] == "完成" else 0 child_task.create( **{ "name": data["misname"], "mainTask": task_name, "person": data["mispeo"], "childWeight": data["misper"], "status": data["miscon"], "endTime": data["misend"], "describe": data["misdetail"], "startTime": str(datetime.datetime.now())[:10] }) child_ids = child_task.select( child_task.id).where(child_task.name == data["misname"]) child_ids = model_to_dict(child_ids[0]) id_list.append(int(child_ids["id"])) user_data = user.select( user.taskNowTime, user.taskFinished, user.childID).where(user.userName == data["mispeo"]) user_data = model_to_dict(user_data[0]) if user_data["childID"] == "": user_data["childID"] = [] else: user_data["childID"] = ast.literal_eval(user_data["childID"]) user_data["childID"].append(child_ids["id"]) user_data["childID"] = str(user_data["childID"]) if not data["miscon"]: user_data["taskNowTime"] = int(user_data["taskNowTime"]) + int( data["misper"]) user_data["taskFinished"] = int(user_data["taskFinished"]) + int( data["misper"]) user.update( **{ "taskFinished": user_data["taskFinished"], "childID": user_data["childID"], "taskNowTime": user_data["taskNowTime"] }).where(user.userName == data['mispeo']).execute() main_task.update(**{ "childTask": str(id_list) }).where(main_task.name == task_name).execute()
def get_data(self, name): user_data = user.select(user.mainID, user.taskFinished, user.childID) \ .where(user.userName == name) user_data = model_to_dict(user_data[0]) if user_data["childID"] == "": user_data["childID"] = [] else: user_data["childID"] = ast.literal_eval(user_data["childID"]) if user_data["mainID"] == "": user_data["mainID"] = [] else: user_data["mainID"] = ast.literal_eval(user_data["mainID"]) tasks = [] for main_id in user_data["mainID"]: task = main_task.select(main_task.name, main_task.leader, main_task.childTask) \ .where(main_task.id == main_id) task = model_to_dict(task[0]) if task["childTask"] == "": task["childTask"] = [] else: task["childTask"] = ast.literal_eval(task["childTask"]) percen = 0 for ID in task["childTask"]: if ID in user_data["childID"]: sub_task = child_task.select( child_task.childWeight).where(child_task.id == ID) percen += int(sub_task[0].childWeight) percen = str(percen) + "%" tasks.append({ "project": task["name"], "actor": task["leader"], "OnePer": percen }) percent = str(user_data["taskFinished"]) + "%" return json.dumps({ "MyTask": tasks, "percent": percent }, ensure_ascii=False)