def zk_info(request): """ 获取zk节点信息 :param request: :return: """ context = dict() try: user_name = request.POST.get('username') # password + timestamp 生成的md5校验 md5 = request.POST.get('md5') timestamp = request.POST.get("timestamp") zk_name = request.POST.get('zkName') except: raise ActionError("parameter error") if type(timestamp) is not str and type(timestamp) is not float: raise ActionError("parameter error") user = User.objects.filter(user_name=user_name).first() if not user: log.tag_info(DbStatus.Inquire, "User: %s not Exist In table user" % user_name) raise ActionError("User not exist") if type(timestamp) is str: timestamp = float(timestamp) if not md5_check_in_time(md5, user.password, int(timestamp)): raise ActionError("MD5校验失败") # 验证node是否属于该用户 zk_path = verify_node_by_path(user, zk_name) # 获取zk节点信息 zk_node = get_node_by_path(zk_path) context["obj"] = zk_node context["status"] = WebResp.ActionSuccess context["errMsg"] = "" return HttpResponse(json.dumps(context))
def get_person_all_node(username: str): """ 获取个人所有节点信息 :param username: :return: """ all_node_info = list() # 获取唯一的user_id user = User.objects.filter(user_name=username).first() if not user: log.tag_error(DbStatus.Inquire, "User: %s not Exist In table user" % username) raise ActionError("User error") user_id = user.user_id all_node = UserNode.objects.filter(user_id=user_id).all() # 没有数据 if len(all_node) == 0: return all_node_info node_id_list = [node.node_id for node in all_node] node_info = Node.objects.filter(node_id__in=node_id_list).all() node_single = dict() for node in node_info: node_single["nodeId"] = node.node_id node_single["nodeName"] = node.node_name node_single["nodePath"] = node.node_path node_single["nodeInfo"] = get_node_by_path(node.node_path) all_node_info.append(deepcopy(node_single)) return all_node_info
def get_node(node_path: str): """ 获取节点信息 :param node_path: :return: """ try: data, _ = ZK.get(node_path) except Exception as e: if type(e) == NoNodeError: log.tag_error(ZkControlStatus.Get, "Node not exist") raise ActionError(KazooMsg.Node_Not_Exist) else: log.tag_error(ZkControlStatus.Get, "get node with error: " + str(e)) raise ActionError(KazooMsg.Get_Failed) return data
def get_children(node_path: str): """ get a list of child nodes of a path :param node_path: :return: """ try: children_list = ZK.get_children(node_path) except Exception as e: if type(e) is NoNodeError: log.tag_error(ZkControlStatus.Get, "Node not exist") raise ActionError(KazooMsg.Node_Not_Exist) else: log.tag_error(ZkControlStatus.Get, "inquire children list with error: " + str(e)) raise ActionError(KazooMsg.Delete_Failed.Get_Children_Failed) return children_list
def verify_node_by_path(user: User, node_name: str): """ 根据user_name和node_path判断用户是否具有对应节点权限 :param user: :param node_name: :return: """ user_id = user.user_id node = Node.objects.filter(node_name=node_name).first() if not node: raise ActionError("Node Path Not Exist") node_id = node.node_id all_user_node = UserNode.objects.filter(user_id=user_id).all() all_node_id = [user_node.node_id for user_node in all_user_node] if node_id not in all_node_id: raise ActionError("Node not belongs to user") return node.node_path
def verify_node(user_name: str, node_id: str): """ 验证节点归属并返回节点记录 :param user_name: :param node_id: :return: """ # 验证node归属 user = User.objects.filter(user_name=user_name).first() if not user: log.tag_error(DbStatus.Inquire, "User: %s not Exist In table user" % user_name) raise ActionError("User error") user_id = user.user_id all_node = UserNode.objects.filter(user_id=user_id).all() node_id_list = [node.node_id for node in all_node] if node_id not in node_id_list: raise ActionError("The node: %s is not belongs to user: %s" % (node_id, user_name)) # 获取node_path node_record = Node.objects.get(node_id=node_id) return node_record
def str_to_datetime(time_str, raise_error=False): """ "2017-04-06 14:06:51" 字符串转日期时间对象 """ try: dt = parser.parse(time_str) except Exception as e: if raise_error: raise ActionError("时间字符串格式错误") else: raise e return dt
def update_node(node_path: str, new_node_data: b""): """ 更新节点 :param node_path: :param new_node_data: :return: """ with ZK.transaction(): try: ZK.set(node_path, new_node_data) except Exception as e: if type(e) is NoNodeError: log.tag_error(ZkControlStatus.Update, "Node not exist") raise ActionError(KazooMsg.Node_Not_Exist) else: log.tag_error(ZkControlStatus.Update, "update node with error: " + str(e)) raise ActionError(KazooMsg.Update_Failed) # 返回更新后的数据 data = get_node(node_path) return data
def create_node(node_path: str, node_data: b"" = b""): """ 创建新节点并set data, 目前只支持json格式的data :param node_path: 节点路径 :param node_data: 节点值 :return: """ with ZK.transaction(): try: ZK.create(node_path, node_data) except Exception as e: if type(e) == NodeExistsError: log.tag_error(ZkControlStatus.Create, "Node exist") raise ActionError(KazooMsg.Node_Exist) else: log.tag_error(ZkControlStatus.Create, "create node with error: " + str(e)) raise ActionError(KazooMsg.Create_Failed) # 返回创建后的数据 data = get_node(node_path) return data
def delete_node(node_path: str): """ 删除节点 :param node_path: :return: """ try: ZK.delete(node_path, recursive=True) except Exception as e: log.tag_error(ZkControlStatus.Delete, "delete node with error: " + str(e)) raise ActionError(KazooMsg.Delete_Failed)
def de_json(data: b""): """ 将b""型数据解码成json(dict) :param data: :return: """ if data == b"": return dict() decode_data = data.decode("utf-8") try: data_json = json.loads(decode_data) except Exception as e: log.tag_error(Code.DeCode, str(e)) raise ActionError("node data is not json type") return data_json
def verify_node_by_path(user_name: str, password: str, node_path: str): """ 根据user_name和node_path判断用户是否具有对应节点权限 :param user_name: :param password: :param node_path: :return: """ user = User.objects.filter(user_name=user_name).first() if not user: log.tag_info(DbStatus.Inquire, "User: %s not Exist In table user" % user_name) raise ActionError("User not exist") if user.password != password: raise ActionError("password error") user_id = user.user_id node = Node.objects.filter(node_path=node_path).first() if not node: raise ActionError("Node Path Not Exist") node_id = node.node_id all_user_node = UserNode.objects.filter(user_id=user_id).all() all_node_id = [user_node.node_id for user_node in all_user_node] if node_id not in all_node_id: raise ActionError("Node not belongs to user") return True
def get_node_by_path(node_path: str): """ 根据node_path获取节点存储信息 :param node_path: :return: """ try: node_info = de_json(get_node(node_path)) except: # # TODO 解决不识别bool类型 # node_info = { # "Error": "没有该节点信息", # "test": "test", # "num": 1111, # "another": { # "test1": "11222", # "other": { # "szg": 13434 # } # } # } raise ActionError("Get node info error") return node_info