def filter(self, qs, value): if value in EMPTY_VALUES: return qs body = self.parent.request.data if not isinstance(body, dict): return qs.none() items = list( filter(lambda x: x.get('name') == value, body.get('segments', []))) if len(items) == 0: return qs.none() query = items[0].get('query') serializer = SqlSerializer(data={'query': query}) serializer.is_valid(raise_exception=True) result = serializer.execute(serializer.validated_data) columns = list(result['columns']) rows = result['data'] if len(columns) == 0 or len(rows) == 0: return qs.none() ids = list(map(lambda x: list(x)[0], rows)) return qs.filter(pk__in=ids)
def post(self, request, *args, **kwargs): serializer = SqlSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.execute())