def create_user(self, data): if not self._check_params(data): return get_error_result("ParamError") group = db_api.get_group_with_first({'uuid': data['group_uuid']}) if not group: logger.error("group: %s not exist", data['group_uuid']) return get_error_result("GroupNotExists", name='') # add user to group user_value = { "uuid": create_uuid(), "group_uuid": data['group_uuid'], "user_name": data['user_name'], "passwd": create_md5(data['passwd']), "name": data.get('name', ''), "phone": data.get('phone', ''), "email": data.get('email', ''), "enabled": data.get('enabled', True) } try: db_api.create_group_user(user_value) logger.info("create group user %s success", data['user_name']) except Exception as e: logging.info("insert group user info to db failed:%s", e) return get_error_result("GroupUserCreateError", user_name=data['user_name']) return get_error_result("Success", user_value)
def move_user(self, data): group = db_api.get_group_with_first({"uuid": data['group_uuid']}) if not group: logger.error("group: %s not exist" % data['group_uuid']) return get_error_result("GroupNotExists", name='') for user in data['users']: user = db_api.get_group_user_with_first({'uuid': user['uuid']}) if not user: logger.error("user %s not exist", user['user_name']) return get_error_result("GroupUserNotExists", name=user['user_name']) if user.group_uuid == group.uuid: logger.info("user %s already in group %s, skip", user.user_name, group.name) continue try: user.group_uuid = data['group_uuid'] # 移动后,原先的跟桌面绑定关系要删除 binds = db_api.get_instance_with_all({"user_uuid": user.uuid}) for bind in binds: bind.user_uuid = '' bind.soft_update() user.soft_update() except Exception as e: logger.error("move user %s to group %s failed:%s", user.user_name, group.name, e) return get_error_result("GroupUserMoveError", user_name=user.user_name, group=group.name) logger.info("move user %s to group %s success", user.user_name, group.name) return get_error_result("Success")
def update_group(self, data): group_uuid = data.get('uuid', '') group = db_api.get_group_with_first({"uuid": group_uuid}) if not group: logger.error("group: %s not exist" % group_uuid) return get_error_result("GroupNotExists", name='') if group.group_type == 1: desktop = db_api.get_desktop_with_all({'group_uuid': group_uuid}) if desktop: if 'subnet_uuid' in data['value'] and data['value'][ 'subnet_uuid'] != group.subnet_uuid: return get_error_result("GroupSubnetError") start_ip = data.get('value').get('start_ip') end_ip = data.get('value').get('end_ip') if group.start_ip != start_ip or group.end_ip != end_ip: old_terminals = db_api.get_terminal_with_all( {'group_uuid': group_uuid}) for terminal in old_terminals: terminal.group_uuid = None terminal.updated_at = datetime.datetime.utcnow() terminal.soft_update() new_terminals = db_api.get_terminal_with_all( {'group_uuid': None}) self.change_group_uuid(new_terminals, start_ip, end_ip, group_uuid) try: group.update(data['value']) group.soft_update() except Exception as e: logger.error("update group:%s failed:%s", group_uuid, e) return get_error_result("GroupUpdateError", name=group.name) logger.info("update group:%s success", group_uuid) return get_error_result("Success")
def delete(self, data): """ 删除指定学期、教学分组下的所有课表 :param data: { "term_uuid": "062d6d14-97c1-448d-a9fb-67367fdf843b", "group_uuid": "41b212d6-3ef4-49f1-851d-424cb4559261" } :return: """ try: logger.info("data: %s" % data) term_uuid = data.get("term_uuid", None) group_uuid = data.get("group_uuid", None) if not term_uuid or not group_uuid: return get_error_result("ParamError") if not db_api.get_term_with_first({"uuid": term_uuid}): return get_error_result("TermNotExist") if not db_api.get_group_with_first({"uuid": group_uuid}): return get_error_result("EduGroupNotExist") template_uuids = db_api.get_distinct_course_template_uuids_by_course_schedule( { "term_uuid": term_uuid, "group_uuid": group_uuid }) template_uuids = [tuple_[0] for tuple_ in template_uuids] ds_ret = db_api.delete_course_schedule_many({ "term_uuid": term_uuid, "group_uuid": group_uuid }) logger.info( "delete many[%s] in yzy_course_schedule success where term_uuid[%s] group_uuid[%s] " % (ds_ret, term_uuid, group_uuid)) dt_ret = db_api.delete_course_template_many_by_uuids( template_uuids) logger.info( "delete many[%s] in yzy_course_template success where uuid in [%s]" % (dt_ret, template_uuids)) dc_ret = db_api.delete_course_many_by_course_template_uuids( template_uuids) logger.info( "delete many[%s] in yzy_course success where course_template_uuid in [%s]" % (dc_ret, template_uuids)) return get_error_result() except Exception as e: logger.exception("delete course_schedule failed: %s" % str(e), exc_info=True) return get_error_result("OtherError")
def disable(self, data): """ 禁用指定学期、教学分组下的所有课表 :param data: { "term_uuid": "062d6d14-97c1-448d-a9fb-67367fdf843b", "group_uuid": "41b212d6-3ef4-49f1-851d-424cb4559261" } :return: """ try: logger.info("data: %s" % data) term_uuid = data.get("term_uuid", None) group_uuid = data.get("group_uuid", None) if not term_uuid or not group_uuid: return get_error_result( "ParamError", data={"keys": ["term_uuid", "group_uuid"]}) term_obj = db_api.get_term_with_first({"uuid": term_uuid}) if not term_obj: return get_error_result("TermNotExist") if not db_api.get_group_with_first({"uuid": group_uuid}): return get_error_result("EduGroupNotExist") # 更新该学期的group_status_map字段(保存了教学桌面组的课表启用状态) group_status_map = json.loads(term_obj.group_status_map) if group_status_map.get( group_uuid, None) != constants.COURSE_SCHEDULE_DISABLED: group_status_map[ group_uuid] = constants.COURSE_SCHEDULE_DISABLED term_obj.update( {"group_status_map": json.dumps(group_status_map)}) logger.info( "update uuid[%s] in yzy_term success : {'group_status_map': {'%s': %s}}" % (term_uuid, group_uuid, constants.COURSE_SCHEDULE_DISABLED)) # 找出该学期、该教学分组下的所有课表,标记为禁用 ret = db_api.update_course_schedule_many( value_dict={"status": constants.COURSE_SCHEDULE_DISABLED}, item={ "term_uuid": term_uuid, "group_uuid": group_uuid }) logger.info( "update many[%s] in yzy_course_schedule success where term_uuid[%s], group_uuid[%s]: {'status': %s}" % (ret, term_uuid, group_uuid, constants.COURSE_SCHEDULE_DISABLED)) return get_error_result() except Exception as e: logger.exception("disable course_schedule failed: %s" % str(e), exc_info=True) return get_error_result("OtherError")
def multi_create_user(self, data): """ 如果用户名冲突,则失败计数加1 """ group = db_api.get_group_with_first({'uuid': data['group_uuid']}) if not group: logger.error("group: %s not exist", data['group_uuid']) return get_error_result("GroupNotExists", name='') success_num = 0 failed_num = 0 postfix = data['postfix'] postfix_start = data['postfix_start'] for i in range(data['user_num']): # 桌面名称是前缀加几位数字 if len(str(postfix_start)) < postfix: post = '0' * (postfix - len(str(postfix_start))) + str(postfix_start) else: post = str(postfix_start) user_name = data['prefix'] + post if db_api.get_group_user_with_first({'user_name': user_name}): logger.info( "multi create user failed, the user %s already exists", user_name) failed_num += 1 postfix_start += 1 continue user_uuid = create_uuid() user_value = { "uuid": user_uuid, "group_uuid": data['group_uuid'], "user_name": user_name, "passwd": create_md5(data['passwd']), "name": data.get('name', ''), "phone": data.get('phone', ''), "email": data.get('email', ''), "enabled": data.get('enabled', True) } postfix_start += 1 try: db_api.create_group_user(user_value) logger.info("create user:%s success", user_name) except Exception as e: logging.info("insert group user info to db failed:%s", e) failed_num += 1 postfix_start += 1 continue success_num += 1 logger.info("multi create group user success") return get_error_result("Success", { "failed_num": failed_num, "success_num": success_num })
def delete_group(self, group_uuid): group = db_api.get_group_with_first({"uuid": group_uuid}) if not group: logger.error("group: %s not exist" % group_uuid) return get_error_result("GroupNotExists", name='') # 教学分组如果在使用中,不能删除 if constants.EDUCATION_DESKTOP == group.group_type: desktop = db_api.get_desktop_with_all({"group_uuid": group_uuid}) if desktop: logger.error("group already in use", group_uuid) return get_error_result("GroupInUse", name=group.name) # 教学分组有关联课表时,不能删除 if db_api.get_course_schedule_with_all({"group_uuid": group_uuid}): logger.error("group already in use by course_schedule", group_uuid) return get_error_result("GroupInUseByCourseSchedule", name=group.name) # 个人分组删除时需要删除分组中的用户 if constants.PERSONAL_DEKSTOP == group.group_type: users = db_api.get_group_user_with_all({'group_uuid': group.uuid}) for user in users: binds = db_api.get_instance_with_all({"user_uuid": user.uuid}) for bind in binds: bind.user_uuid = '' bind.soft_update() user.soft_delete() desktops = db_api.get_personal_desktop_with_all( {"group_uuid": group_uuid}) for desktop in desktops: desktop.group_uuid = "" desktop.soft_update() group.soft_delete() logger.info("delete group %s success", group_uuid) ret = terminal_post( "/api/v1/terminal/task", { "handler": "WebTerminalHandler", "command": "delete_group", "data": { "group_uuid": group_uuid } }) return ret
def _create(self, term_uuid, group_uuid, week_num, course_ret_list, desktops): group_obj = db_api.get_group_with_first({"uuid": group_uuid}) if not group_obj: return get_error_result("EduGroupNotExist") if group_obj.group_type != constants.EDUCATION_GROUP: return get_error_result("CourseNotEduGroup") course_template = { "uuid": create_uuid(), "desktops": json.dumps(desktops) } course_values_list = [{ "uuid": create_uuid(), "course_template_uuid": course_template["uuid"], "desktop_uuid": _d["desktop_uuid"], "weekday": _d["weekday"], "course_num": _d["course_num"] } for _d in course_ret_list] # 课表创建后默认启用 course_schedule = { "uuid": create_uuid(), "term_uuid": term_uuid, "group_uuid": group_uuid, "course_template_uuid": course_template["uuid"], "week_num": week_num, "course_md5": create_md5(json.dumps(course_ret_list)), "status": constants.COURSE_SCHEDULE_ENABLED } db_api.create_course_template(course_template) logger.info("insert in yzy_course_template success: %s" % course_template) db_api.create_course_many(course_values_list) logger.info( "insert many[%d] in yzy_course success: course_template_uuid[%s]" % (len(course_values_list), course_template["uuid"])) db_api.create_course_schedule_many([course_schedule]) logger.info("insert in yzy_course_schedule success: %s" % course_schedule)
def import_user(self, data): file = data['filepath'] enabled = data['enabled'] logger.info("begin to import user, file:%s", file) success_num = 0 failed_num = 0 book = openpyxl.load_workbook(file) ws = book.active for row in ws.iter_rows(min_row=2, max_col=6): flag = True values = dict() for cell in row: if 1 == cell.column: user_name = cell.value if not user_name: failed_num += 1 flag = False logger.error("the user_name can not be blank, skip", user_name) break user = db_api.get_group_user_with_first( {"user_name": user_name}) if user: failed_num += 1 flag = False logger.error("the user %s already exist in db, skip", user_name) break values['user_name'] = user_name elif 2 == cell.column: passwd = cell.value if not passwd: failed_num += 1 flag = False logger.error("the password can not be blank") break if len(str(passwd)) < 6 or len(str(passwd)) > 16: failed_num += 1 flag = False logger.error("the password len must between 6~16") break values['passwd'] = create_md5(cell.value) elif 3 == cell.column: group_name = cell.value group = db_api.get_group_with_first({"name": group_name}) if not group: failed_num += 1 flag = False logger.error("the group %s not exist, skip", group_name) break else: values['group_uuid'] = group.uuid elif 4 == cell.column: name = cell.value if name: values['name'] = str(name) elif 5 == cell.column: email = cell.value if email: values['email'] = str(email) elif 6 == cell.column: phone = cell.value if phone: values['phone'] = str(phone) else: break if flag: values['enabled'] = int(enabled) values['uuid'] = create_uuid() db_api.create_group_user(values) success_num += 1 return get_error_result("Success", { "failed_num": failed_num, "success_num": success_num })