superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject [incubator-superset] branch lyftga updated: Fix for tagging backend (#7240)
Date Mon, 08 Apr 2019 18:06:53 GMT
This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch lyftga
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/lyftga by this push:
     new c6876f7  Fix for tagging backend (#7240)
c6876f7 is described below

commit c6876f7829ee0adc3430ae5305ec05005402cb0d
Author: Beto Dealmeida <roberto@dealmeida.net>
AuthorDate: Mon Apr 8 11:06:45 2019 -0700

    Fix for tagging backend (#7240)
---
 superset/models/tags.py |   4 +-
 superset/views/tags.py  | 172 ++++++++++++++++++++++++++----------------------
 2 files changed, 96 insertions(+), 80 deletions(-)

diff --git a/superset/models/tags.py b/superset/models/tags.py
index 897c189..9c30f55 100644
--- a/superset/models/tags.py
+++ b/superset/models/tags.py
@@ -123,7 +123,7 @@ class ObjectUpdater(object):
             tagged_object = TaggedObject(
                 tag_id=tag.id,
                 object_id=target.id,
-                object_type=ObjectTypes.chart,
+                object_type=cls.object_type,
             )
             session.add(tagged_object)
 
@@ -141,7 +141,7 @@ class ObjectUpdater(object):
         tagged_object = TaggedObject(
             tag_id=tag.id,
             object_id=target.id,
-            object_type=ObjectTypes.query,
+            object_type=cls.object_type,
         )
         session.add(tagged_object)
 
diff --git a/superset/views/tags.py b/superset/views/tags.py
index fc34490..65cbbc9 100644
--- a/superset/views/tags.py
+++ b/superset/views/tags.py
@@ -30,7 +30,7 @@ from werkzeug.routing import BaseConverter
 
 from superset import app, appbuilder, db, utils
 from superset.jinja_context import current_user_id, current_username
-import superset.models.core
+from superset.models.core import Dashboard, Slice
 from superset.models.sql_lab import SavedQuery
 from superset.models.tags import ObjectTypes, Tag, TaggedObject, TagTypes
 from .base import BaseSupersetView, json_success
@@ -57,33 +57,6 @@ def process_template(content):
     return template.render(context)
 
 
-def get_name(obj):
-    if obj.Dashboard:
-        return obj.Dashboard.dashboard_title
-    elif obj.Slice:
-        return obj.Slice.slice_name
-    elif obj.SavedQuery:
-        return obj.SavedQuery.label
-
-
-def get_creator(obj):
-    if obj.Dashboard:
-        return obj.Dashboard.creator()
-    elif obj.Slice:
-        return obj.Slice.creator()
-    elif obj.SavedQuery:
-        return obj.SavedQuery.creator()
-
-
-def get_attribute(obj, attr):
-    if obj.Dashboard:
-        return getattr(obj.Dashboard, attr)
-    elif obj.Slice:
-        return getattr(obj.Slice, attr)
-    elif obj.SavedQuery:
-        return getattr(obj.SavedQuery, attr)
-
-
 class TagView(BaseSupersetView):
 
     @has_access_api
@@ -91,11 +64,13 @@ class TagView(BaseSupersetView):
     def suggestions(self):
         query = (
             db.session.query(TaggedObject)
-            .group_by(TaggedObject.tag_id)
+            .join(Tag)
+            .with_entities(TaggedObject.tag_id, Tag.name)
+            .group_by(TaggedObject.tag_id, Tag.name)
             .order_by(func.count().desc())
             .all()
         )
-        tags = [{'id': obj.tag.id, 'name': obj.tag.name} for obj in query]
+        tags = [{'id': id, 'name': name} for id, name in query]
         return json_success(json.dumps(tags))
 
     @has_access_api
@@ -157,60 +132,101 @@ class TagView(BaseSupersetView):
     @has_access_api
     @expose('/tagged_objects/', methods=['GET', 'POST'])
     def tagged_objects(self):
-        query = db.session.query(
-            TaggedObject,
-            superset.models.core.Dashboard,
-            superset.models.core.Slice,
-            SavedQuery,
-        ).join(Tag)
-
-        tags = request.args.get('tags')
+        tags = [
+            process_template(tag)
+            for tag in request.args.get('tags', '').split(',') if tag
+        ]
         if not tags:
             return json_success(json.dumps([]))
 
-        tags = [process_template(tag) for tag in tags.split(',')]
-        query = query.filter(Tag.name.in_(tags))
-
         # filter types
-        types = request.args.get('types')
-        if types:
-            query = query.filter(TaggedObject.object_type.in_(types.split(',')))
-
-        # get names
-        query = query.outerjoin(
-            superset.models.core.Dashboard,
-            and_(
-                TaggedObject.object_id == superset.models.core.Dashboard.id,
-                TaggedObject.object_type == ObjectTypes.dashboard,
-            ),
-        ).outerjoin(
-            superset.models.core.Slice,
-            and_(
-                TaggedObject.object_id == superset.models.core.Slice.id,
-                TaggedObject.object_type == ObjectTypes.chart,
-            ),
-        ).outerjoin(
-            SavedQuery,
-            and_(
-                TaggedObject.object_id == SavedQuery.id,
-                TaggedObject.object_type == ObjectTypes.query,
-            ),
-        ).group_by(TaggedObject.object_id, TaggedObject.object_type)
-
-        objects = [
-            {
-                'id': get_attribute(obj, 'id'),
-                'type': obj.TaggedObject.object_type.name,
-                'name': get_name(obj),
-                'url': get_attribute(obj, 'url'),
-                'changed_on': get_attribute(obj, 'changed_on'),
-                'created_by': get_attribute(obj, 'created_by_fk'),
-                'creator': get_creator(obj),
-            }
-            for obj in query if get_attribute(obj, 'id')
+        types = [
+            type_
+            for type_ in request.args.get('types', '').split(',')
+            if type_
         ]
 
-        return json_success(json.dumps(objects, default=utils.core.json_int_dttm_ser))
+        results = []
+
+        # dashboards
+        if not types or 'dashboard' in types:
+            dashboards = (
+                db.session.query(Dashboard)
+                .join(
+                    TaggedObject,
+                    and_(
+                        TaggedObject.object_id == Dashboard.id,
+                        TaggedObject.object_type == ObjectTypes.dashboard,
+                    ),
+                )
+                .join(Tag, TaggedObject.tag_id == Tag.id)
+                .filter(Tag.name.in_(tags))
+            )
+            results.extend(
+                {
+                    'id': obj.id,
+                    'type': ObjectTypes.dashboard.name,
+                    'name': obj.dashboard_title,
+                    'url': obj.url,
+                    'changed_on': obj.changed_on,
+                    'created_by': obj.created_by_fk,
+                    'creator': obj.creator(),
+                } for obj in dashboards
+            )
+
+        # charts
+        if not types or 'chart' in types:
+            charts = (
+                db.session.query(Slice)
+                .join(
+                    TaggedObject,
+                    and_(
+                        TaggedObject.object_id == Slice.id,
+                        TaggedObject.object_type == ObjectTypes.chart,
+                    ),
+                )
+                .join(Tag, TaggedObject.tag_id == Tag.id)
+                .filter(Tag.name.in_(tags))
+            )
+            results.extend(
+                {
+                    'id': obj.id,
+                    'type': ObjectTypes.chart.name,
+                    'name': obj.slice_name,
+                    'url': obj.url,
+                    'changed_on': obj.changed_on,
+                    'created_by': obj.created_by_fk,
+                    'creator': obj.creator(),
+                } for obj in charts
+            )
+
+        # saved queries
+        if not types or 'query' in types:
+            saved_queries = (
+                db.session.query(SavedQuery)
+                .join(
+                    TaggedObject,
+                    and_(
+                        TaggedObject.object_id == SavedQuery.id,
+                        TaggedObject.object_type == ObjectTypes.query,
+                    ),
+                )
+                .join(Tag, TaggedObject.tag_id == Tag.id)
+                .filter(Tag.name.in_(tags))
+            )
+            results.extend(
+                {
+                    'id': obj.id,
+                    'type': ObjectTypes.query.name,
+                    'name': obj.label,
+                    'url': obj.url,
+                    'changed_on': obj.changed_on,
+                    'created_by': obj.created_by_fk,
+                    'creator': obj.creator(),
+                } for obj in saved_queries
+            )
+
+        return json_success(json.dumps(results, default=utils.core.json_int_dttm_ser))
 
 
 app.url_map.converters['object_type'] = ObjectTypeConverter


Mime
View raw message