def add_task(self, data): # 将组员名字转换为id exist = main_task.select( main_task.id).where(main_task.name == data["name"]) if exist: return {"info": "task already exist"} leader = user.select( user.userName).where(user.userName == data["leader"]) if not leader: return {"result": "mistake"} member_ids = [] members = data["member"] for name in data["member"]: # 将名字转换为id user_data = user.select(user.id).where(user.userName == name) member_ids.append(user_data[0].id) data["member"] = str(member_ids) data["goal"] = int(data["goal"][:-1]) main_task.create( **{ "name": data["name"], "leader": data["leader"], "member": data["member"], "childTask": "", "totalWeight": data["goal"], "startTime": data["date1"], "endTime": data["date2"], "describe": data["desc"] }) # 将main_task的Id加入每个组员的数据库中 main_id = main_task.select( main_task.id).where(main_task.name == data["name"]) main_id = model_to_dict(main_id[0]) for name in members: task = user.select(user.mainID).where(user.userName == name) task = model_to_dict(task[0]) if task["mainID"] == "": ID = [main_id["id"]] ID = str(ID) else: ID = ast.literal_eval(task["mainID"]) ID.append(main_id["id"]) ID = str(ID) user.update(**{ "mainID": ID }).where(user.userName == name).execute() # 将main_task的ID加入组长的数据库中 lead_main_id = user.select( user.mainID).where(user.userName == data["leader"]) lead_main_id = model_to_dict(lead_main_id[0]) if lead_main_id["mainID"] == "": lead_main_id["mainID"] = [main_id["id"]] else: lead_main_id["mainID"] = ast.literal_eval(lead_main_id["mainID"]) lead_main_id["mainID"].append(main_id["id"]) lead_main_id["mainID"] = str(lead_main_id["mainID"]) user.update(**{ "mainID": lead_main_id["mainID"] }).where(user.userName == data["leader"]).execute() return {"result": "success"}
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 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)
def get_data(self): data_list = [] tasks = main_task.select(main_task.name, main_task.leader, main_task.startTime, main_task.endTime, main_task.totalWeight) for task in tasks: end_time = str(task.endTime.date()).split('-') endtime = datetime.datetime(int(end_time[0]), int(end_time[1]), int(end_time[2])) nowtime = datetime.datetime.now() status = (endtime - nowtime).days task.startTime = str(task.startTime.date()) task.endTime = str(task.endTime.date()) task.totalWeight = str(task.totalWeight) + "%" data_list.append({ "taskname": task.name, "name": task.leader, "start": task.startTime, "end": task.endTime, "state": int(status), "percent": task.totalWeight }) return json.dumps({"AllTask": data_list}, ensure_ascii=False)