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 get(self, request, *args, **kwargs): serializer = SqlSerializer(data=request.GET) serializer.is_valid(raise_exception=True) with connection.cursor() as cursor: try: cursor.execute(serializer.data['query'], serializer.data.get('params', '').split(',')) except ProgrammingError as e: raise SqlError(e) rows = cursor.fetchall() return Response(rows)
def post(self, request, *args, **kwargs): serializer = SqlsSerializer(data=request.data) if 'queries' in request.data \ else SqlSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.execute(serializer.validated_data))
def post(self, request, *args, **kwargs): serializer = SqlSerializer(data=request.data) serializer.is_valid(raise_exception=True) return Response(serializer.execute())