def list(self, request, *args, **kwargs): """ List node :param request: query parameter :return: node list :rtype: list """ serializer = NodeQuery(data=request.GET) if serializer.is_valid(raise_exception=True): page = serializer.validated_data.get("page") per_page = serializer.validated_data.get("per_page") node_type = serializer.validated_data.get("type") name = serializer.validated_data.get("name") agent_id = serializer.validated_data.get("agent_id") # if agent_id is not None and not request.user.is_operator: # raise PermissionDenied query_filter = {} if node_type: query_filter.update({"type": node_type}) if name: query_filter.update({"name__icontains": name}) # if request.user.is_administrator: # query_filter.update( # {"organization": request.user.organization} # ) # elif request.user.is_common_user: # query_filter.update({"user": request.user}) if agent_id: query_filter.update({"agent__id": agent_id}) nodes = Node.objects.filter(**query_filter) p = Paginator(nodes, per_page) nodes = p.page(page) nodes = [{ "id": str(node.id), "name": node.name, "type": node.type, "org": node.org, "urls": node.urls, "network": str(node.org.network.id) if node.org.network else None, "agents": node.agent if node.agent else None, "channel": str(node.org.channel.id) if node.org.channel else None, "ports": node.port, "created_at": node.created_at, } for node in nodes] response = NodeListSerializer({"total": p.count, "data": nodes}) return Response(data=response.data, status=status.HTTP_200_OK)
def list(self, request, *args, **kwargs): """ List Nodes Filter nodes with query parameters. """ serializer = NodeQuery(data=request.GET) if serializer.is_valid(raise_exception=True): page = serializer.validated_data.get("page") per_page = serializer.validated_data.get("per_page") node_type = serializer.validated_data.get("type") name = serializer.validated_data.get("name") network_type = serializer.validated_data.get("network_type") network_version = serializer.validated_data.get("network_version") agent_id = serializer.validated_data.get("agent_id") if agent_id is not None and not request.user.is_operator: raise PermissionDenied query_filter = {} if node_type: query_filter.update({"type": node_type}) if name: query_filter.update({"name__icontains": name}) if network_type: query_filter.update({"network_type": network_type}) if network_version: query_filter.update({"network_version": network_version}) if request.user.is_administrator: query_filter.update( {"organization": request.user.organization} ) elif request.user.is_common_user: query_filter.update({"user": request.user}) if agent_id: query_filter.update({"agent__id": agent_id}) nodes = Node.objects.filter(**query_filter) p = Paginator(nodes, per_page) nodes = p.page(page) nodes = [node.__dict__ for node in nodes] response = NodeListSerializer( data={"total": p.count, "data": nodes} ) if response.is_valid(raise_exception=True): return Response( data=response.validated_data, status=status.HTTP_200_OK )