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): users = user.select(user.userName).where(user.isBoss == 0) member = [] for user_data in users: user_data = model_to_dict(user_data) member.append(user_data["userName"]) return json.dumps({"member": member}, ensure_ascii=False)
def _get_news_or_notice(self, key): """ 返回新闻或通知 key不带Id时返回所有的新闻或通知 key为指定id值时返回具体新闻或通知内容 """ # 返回所有新闻的名字,时间,和对应id if key in ('news', 'notice'): with db.execution_context(): all_data = main_data.select(main_data.name_id, main_data.html_content). \ where(main_data.name_id.startswith(key)) data_list = [] for data in all_data: data = model_to_dict(data) title = self._get_title(data['html_content']) time = self._get_time(data['name_id']) news_id = data['name_id'] data_list.append({ 'name': title, 'time': time, 'id': news_id }) return {'data': data_list} else: # 带具体值时返回新闻具体内容 with db.execution_context(): single_data = main_data.select(main_data.html_content) \ .where(main_data.name_id == key) return {'HTML': str(single_data[0].html_content)}
def _get_all_overview_data(self, key): """返回所有概览信息 可能的类别有领导,教师,纪实活动,欢乐时光 数据格式{"getdata": [{"pictures": "src", "html": "xxxxx", "name": "overviews_xxxxx"}, {"pictures": "src", "html": "xxxxxx", "name": "overviews_xxxxx"}]} """ if key == 'leaders': name = 'leader' elif key == 'teachers': name = 'teacher' else: name = key with db.execution_context(): overviews = main_data.select(main_data.name_id, main_data.html_content) \ .where(main_data.name_id.startswith(name)) data_list = [] for overview in overviews: overview = model_to_dict(overview) edit = overview['html_content'] pictures = re.findall(r'src="(.*?)"', overview['html_content']) pictures = pictures[0] html = re.sub(r'<img.*?>', '', overview['html_content']) data_list.append({"pictures": pictures, "html": html, "name": overview['name_id'], "edit": edit}) return {"getdata": data_list}
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)
def _get_images(self, key): """提取html中所有图片的src html为首页走马灯或中心环境的html内容 key值为'index_imgs'或'enviro_imgs' json:{'pictures': ['url_1', 'url_2', 'url_3']} """ name = key + '_html' with db.execution_context(): html_data = main_data.select(main_data.html_content) \ .where(main_data.name_id == name) html_data = model_to_dict(html_data[0]) src = re.findall(r'src="(.*?)"', html_data["html_content"]) return {'pictures': src}
def get_data(self): user_data = user.select(user.userName, user.taskNowTime, user.taskFinished).where(user.isBoss != 2) table_data = [] for data in user_data: data = model_to_dict(data) data["taskNowTime"] = str(data["taskNowTime"]) + "%" data["taskFinished"] = str(data["taskFinished"]) + "%" table_data.append({ "name": str(data["userName"]), "doing": str(data["taskNowTime"]), "done": str(data["taskFinished"]) }) return json.dumps({"tableData": table_data}, ensure_ascii=False)
def get_user(self, user_name, user_pass): user_data = user.select(user.userPass, user.isBoss).where( user.userName == user_name) if not user_data: return {"status": 3} else: md5 = hashlib.md5() data = model_to_dict(user_data[0]) md5.update(user_pass.encode('utf-8')) password = md5.hexdigest() if password != data["userPass"]: return {"status": 3} else: if data["isBoss"]: return {"status": 1} return {"status": 2}