Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql explorer save query params #489

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions explorer/migrations/0011_auto_20220906_0744.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('explorer', '0010_sql_required'),
]

operations = [
migrations.CreateModel(
name='QueryParam',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('key', models.CharField(blank=True, max_length=128, null=True)),
('value', models.CharField(blank=True, max_length=255, null=True)),
('query', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='query_params', to='explorer.Query')),
],
),
migrations.AlterUniqueTogether(
name='queryparam',
unique_together=set([('query', 'key')]),
),
]
35 changes: 34 additions & 1 deletion explorer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class Query(models.Model):
)

def __init__(self, *args, **kwargs):
self.params = kwargs.get('params')
kwargs.pop('params', None)
super().__init__(*args, **kwargs)

Expand All @@ -63,6 +62,10 @@ class Meta:
def __str__(self):
return str(self.title)

@property
def params(self):
return {k: v for k, v in self.query_params.values_list('key', 'value')}

def get_run_count(self):
return self.querylog_set.count()

Expand Down Expand Up @@ -146,6 +149,21 @@ def snapshots(self):
) for k in keys_s
]

def create_query_params(self, data):
'''
Method to create the QueryParam objects and associate them to a Query obj
Args:
- `data`: dict
'''
# First remove all associated QueryParam objs before creating new ones
self.query_params.all().delete()
query_params_to_create = [
QueryParam(query=self, key=k, value=v)
for k, v in data.items()
]
# Now bulk create new ones
QueryParam.objects.bulk_create(query_params_to_create)


class SnapShot:

Expand Down Expand Up @@ -181,6 +199,21 @@ class Meta:
ordering = ['-run_at']


class QueryParam(models.Model):
key = models.CharField(blank=True, null=True, max_length=128)
value = models.CharField(blank=True, null=True, max_length=255)
query = models.ForeignKey(
Query,
null=True,
blank=True,
on_delete=models.CASCADE,
related_name='query_params'
)

class Meta:
unique_together = ('query', 'key')


class QueryResult:

def __init__(self, sql, connection):
Expand Down
4 changes: 3 additions & 1 deletion explorer/views/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
def _export(request, query, download=True):
_fmt = request.GET.get('format', 'csv')
exporter_class = get_exporter_class(_fmt)
query.params = url_get_params(request)
query_params = url_get_params(request)
if query_params:
query.create_query_params(query_params)
delim = request.GET.get('delim')
exporter = exporter_class(query)
try:
Expand Down
4 changes: 3 additions & 1 deletion explorer/views/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def post(self, request, query_id):
@staticmethod
def get_instance_and_form(request, query_id):
query = get_object_or_404(Query, pk=query_id)
query.params = url_get_params(request)
query_params = url_get_params(request)
if query_params:
query.create_query_params(query_params)
form = QueryForm(
request.POST if len(request.POST) else None,
instance=query
Expand Down