From b9d7253a485df391d32741cc70777d1d98eb546d Mon Sep 17 00:00:00 2001 From: Maxime Beauchemin Date: Fri, 4 Dec 2015 09:30:43 -0800 Subject: [PATCH] Save AS and Overwrite --- panoramix/forms.py | 2 + panoramix/models.py | 19 ++++----- panoramix/static/panoramix.js | 11 ++++- .../{datasource.html => explore.html} | 17 +++++--- panoramix/templates/panoramix/viz.html | 2 +- panoramix/views.py | 42 ++++++++++++------- panoramix/viz.py | 2 +- 7 files changed, 61 insertions(+), 34 deletions(-) rename panoramix/templates/panoramix/{datasource.html => explore.html} (92%) diff --git a/panoramix/forms.py b/panoramix/forms.py index d9408f10b0086..198d77546ecdf 100644 --- a/panoramix/forms.py +++ b/panoramix/forms.py @@ -277,6 +277,8 @@ class QueryForm(OmgWtForm): standalone = HiddenField() async = HiddenField() json = HiddenField() + slice_id = HiddenField() + slice_name = HiddenField() previous_viz_type = HiddenField(default=viz.viz_type) filter_cols = datasource.filterable_column_names or [''] diff --git a/panoramix/models.py b/panoramix/models.py index 336e55f7587d0..21e0381f5a544 100644 --- a/panoramix/models.py +++ b/panoramix/models.py @@ -80,20 +80,21 @@ def viz(self): @property def datasource_id(self): - datasource = self.datasource - return datasource.id if datasource else None + return self.table_id or self.druid_datasource_id @property def slice_url(self): try: - d = json.loads(self.params) + slice_params = json.loads(self.params) except Exception as e: - d = {} + slice_params = {} + slice_params['slice_id'] = self.id + slice_params['slice_name'] = self.slice_name from werkzeug.urls import Href href = Href( - "/panoramix/datasource/{self.datasource_type}/" + "/panoramix/explore/{self.datasource_type}/" "{self.datasource_id}/".format(self=self)) - return href(d) + return href(slice_params) @property def edit_url(self): @@ -250,7 +251,7 @@ def name(self): @property def table_link(self): - url = "/panoramix/datasource/{self.type}/{self.id}/".format(self=self) + url = "/panoramix/explore/{self.type}/{self.id}/".format(self=self) return '{self.table_name}'.format(**locals()) @property @@ -694,9 +695,7 @@ def __repr__(self): @property def datasource_link(self): - url = ( - "/panoramix/datasource/" - "{self.type}/{self.id}/").format(self=self) + url = "/panoramix/explore/{self.type}/{self.id}/".format(self=self) return '{self.datasource_name}'.format(**locals()) def get_metric_obj(self, metric_name): diff --git a/panoramix/static/panoramix.js b/panoramix/static/panoramix.js index 79a9893077bac..69903244fd4c4 100644 --- a/panoramix/static/panoramix.js +++ b/panoramix/static/panoramix.js @@ -78,14 +78,21 @@ function initializeDatasourceView() { } $("#plus").click(add_filter); - $("#save").click(function () { + $("#btn_save").click(function () { var slice_name = prompt("Name your slice!"); if (slice_name != "" && slice_name != null) { $("#slice_name").val(slice_name); $("#action").val("save"); druidify(); } - }) + }); + $("#btn_overwrite").click(function () { + var flag = confirm("Overwrite slice [" + $("#slice_name").val() + "] !?"); + if (flag) { + $("#action").val("overwrite"); + druidify(); + } + }); add_filter(); $(".druidify").click(druidify); diff --git a/panoramix/templates/panoramix/datasource.html b/panoramix/templates/panoramix/explore.html similarity index 92% rename from panoramix/templates/panoramix/datasource.html rename to panoramix/templates/panoramix/explore.html index f94316695f973..dedad101e2084 100644 --- a/panoramix/templates/panoramix/datasource.html +++ b/panoramix/templates/panoramix/explore.html @@ -77,14 +77,21 @@

Filters

Slice! - + {% endif %} +
- - + {{ form.slice_id() }} + {{ form.slice_name() }} + diff --git a/panoramix/templates/panoramix/viz.html b/panoramix/templates/panoramix/viz.html index e6872894dc532..61f7bce7e55d0 100644 --- a/panoramix/templates/panoramix/viz.html +++ b/panoramix/templates/panoramix/viz.html @@ -6,7 +6,7 @@ {% if viz.request.args.get("standalone") == "true" %} {% extends 'panoramix/viz_standalone.html' %} {% else %} - {% extends 'panoramix/datasource.html' %} + {% extends 'panoramix/explore.html' %} {% endif %} diff --git a/panoramix/views.py b/panoramix/views.py index 16cb5b29510d2..ddfa4c881f6e4 100644 --- a/panoramix/views.py +++ b/panoramix/views.py @@ -274,9 +274,11 @@ def ping(): class Panoramix(BaseView): + @has_access - @expose("/datasource///") - def datasource(self, datasource_type, datasource_id): + @expose("/explore///") + @expose("/datasource///") # Legacy url + def explore(self, datasource_type, datasource_id): if datasource_type == "table": datasource = ( db.session @@ -302,7 +304,7 @@ def datasource(self, datasource_type, datasource_id): "danger") return redirect('/slicemodelview/list/') action = request.args.get('action') - if action == 'save': + if action in ('save', 'overwrite'): session = db.session() # TODO use form processing form wtforms @@ -326,19 +328,29 @@ def datasource(self, datasource_type, datasource_id): slice_name = request.args.get('slice_name') - obj = models.Slice( - params=json.dumps(d, indent=4, sort_keys=True), - viz_type=request.args.get('viz_type'), - datasource_name=request.args.get('datasource_name'), - druid_datasource_id=druid_datasource_id, - table_id=table_id, - datasource_type=datasource_type, - slice_name=slice_name, - ) - session.add(obj) + if action == "save": + slc = models.Slice() + msg = "Slice [{}] has been saved".format(slice_name) + elif action == "overwrite": + slc = ( + session.query(models.Slice) + .filter_by(id=request.args.get("slice_id")) + .first() + ) + msg = "Slice [{}] has been overwritten".format(slice_name) + + slc.params = json.dumps(d, indent=4, sort_keys=True) + slc.datasource_name = request.args.get('datasource_name') + slc.viz_type = request.args.get('viz_type') + slc.druid_datasource_id = druid_datasource_id + slc.table_id = table_id + slc.datasource_type = datasource_type + slc.slice_name = slice_name + + session.merge(slc) session.commit() - flash("Slice <{}> has been added to the pie".format(slice_name), "info") - return redirect(obj.slice_url) + flash(msg, "info") + return redirect(slc.slice_url) if not datasource: diff --git a/panoramix/viz.py b/panoramix/viz.py index 78ff03fed4211..2fd575b3dbbfb 100644 --- a/panoramix/viz.py +++ b/panoramix/viz.py @@ -93,7 +93,7 @@ def get_url(self, **kwargs): if d[key] == False: del d[key] href = Href( - '/panoramix/datasource/{self.datasource.type}/' + '/panoramix/explore/{self.datasource.type}/' '{self.datasource.id}/'.format(**locals())) return href(d)