Exemplo n.º 1
0
 def post(self, request, *args, **kwargs):
     sql = request.POST.get('sql')
     connection = request.POST.get('connection')
     query = Query(sql=sql, connection=connection, title='')
     ql = query.log(request.user)
     query.title = 'Playground - %s' % ql.id
     return _export(request, query)
Exemplo n.º 2
0
    def post(self, request):
        sql = request.POST.get("sql")
        action = request.POST.get("action", "")
        existing_query_id = url_get_query_id(request)

        if existing_query_id:
            query = get_object_or_404(
                Query, pk=existing_query_id, created_by_user=self.request.user
            )
        else:
            query = Query(sql=sql, title="Playground", connection=request.POST.get("connection"))

        if action == "save":
            play_sql, _ = PlaygroundSQL.objects.get_or_create(
                sql=sql, created_by_user=request.user
            )
            play_sql.save()

            if existing_query_id:
                redirect_url = reverse(
                    "explorer:query_detail",
                    kwargs={"query_id": existing_query_id},
                )
            else:
                redirect_url = reverse("explorer:query_create")

            query_params = (("play_id", play_sql.id),)
            return redirect(redirect_url + f"?{urlencode(query_params)}")

        elif action == "run":
            query.params = url_get_params(request)
            rows = url_get_rows(request)
            return self.render_with_sql(request, query, run_query=True, rows=rows)

        elif action == "fetch-page":
            query.params = url_get_params(request)
            rows = url_get_rows(request)
            page = url_get_page(request)
            return self.render_with_sql(request, query, run_query=True, rows=rows, page=page)

        elif action == "share":
            play_sql, _ = PlaygroundSQL.objects.get_or_create(
                sql=sql, created_by_user=request.user
            )
            play_sql.save()
            return HttpResponseRedirect(f"{reverse('explorer:share_query')}?play_id={play_sql.id}")

        else:
            return HttpResponse(f"Unknown form action: {action}", 400)
Exemplo n.º 3
0
    def post(self, request):
        sql = request.POST.get('sql')
        action = request.POST.get('action', '')
        existing_query_id = url_get_query_id(request)

        if action.startswith("download"):
            download_format = action.split('-')[1]
            query_params = (('sql', sql), ('format', download_format))
            return HttpResponseRedirect(
                reverse('explorer:download_sql') +
                f"?{urlencode(query_params)}")

        if existing_query_id:
            query = get_object_or_404(Query,
                                      pk=existing_query_id,
                                      created_by_user=self.request.user)
        else:
            query = Query(sql=sql,
                          title="Playground",
                          connection=request.POST.get('connection'))

        if action == 'save':
            play_sql, _ = PlaygroundSQL.objects.get_or_create(
                sql=sql, created_by_user=request.user)
            play_sql.save()

            if existing_query_id:
                redirect_url = reverse(
                    'explorer:query_detail',
                    kwargs={"query_id": existing_query_id},
                )
            else:
                redirect_url = reverse('explorer:query_create')

            query_params = (("play_id", play_sql.id), )
            return redirect(redirect_url + f"?{urlencode(query_params)}")

        elif action in {'run', 'fetch-page'}:
            query.params = url_get_params(request)
            response = self.render_with_sql(request,
                                            query,
                                            run_query=True,
                                            log=True)

            return response

        else:
            return HttpResponse(f"Unknown form action: {action}", 400)
Exemplo n.º 4
0
    def get(self, request):
        if url_get_query_id(request):
            query = get_object_or_404(Query,
                                      pk=url_get_query_id(request),
                                      created_by_user=self.request.user)
            return self.render_with_sql(request, query, run_query=False)

        if url_get_log_id(request):
            log = get_object_or_404(QueryLog,
                                    pk=url_get_log_id(request),
                                    run_by_user=self.request.user)
            query = Query(sql=log.sql,
                          title="Playground",
                          connection=log.connection)
            return self.render_with_sql(request, query)

        initial_data = {"sql": request.GET.get('sql')}

        play_sql = get_playground_sql_from_request(request)
        if play_sql:
            initial_data['sql'] = play_sql.sql

        schema, tables_columns = self._schema_info(request)
        return render(
            self.request,
            'explorer/home.html',
            {
                'title': 'Playground',
                'form': QueryForm(initial=initial_data),
                'form_action': self.get_form_action(request),
                'schema': schema,
                'schema_tables': tables_columns,
            },
        )
Exemplo n.º 5
0
    def get(self, request):
        if url_get_query_id(request):
            query = get_object_or_404(
                Query, pk=url_get_query_id(request), created_by_user=self.request.user
            )
            return self.render_with_sql(request, query, run_query=False)

        if url_get_log_id(request):
            log = get_object_or_404(
                QueryLog, pk=url_get_log_id(request), run_by_user=self.request.user
            )
            query = Query(sql=log.sql, title="Playground", connection=log.connection)
            return self.render_with_sql(request, query, run_query=False)

        initial_data = {"sql": request.GET.get("sql")}

        play_sql = get_playground_sql_from_request(request)
        if play_sql:
            initial_data["sql"] = play_sql.sql

        schema, tables_columns = get_user_schema_info(request)
        schema = match_datasets_with_schema_info(schema)
        return render(
            self.request,
            "explorer/home.html",
            {
                "title": "Playground",
                "form": QueryForm(initial=initial_data),
                "form_action": self.get_form_action(request),
                "schema": schema,
                "schema_tables": tables_columns,
            },
        )
Exemplo n.º 6
0
    def test_playground_query_log(self):
        query = Query(sql='select 1;', title="Playground")
        query.log(None)
        log = QueryLog.objects.first()

        assert log.is_playground is True