mistercrunch commented on a change in pull request #5524: A tagging system for
dashboards, charts and queries
URL:
https://github.com/apache/incubator-superset/pull/5524#discussion_r207063419
##########
File path: superset/views/tags.py
##########
@@ -0,0 +1,197 @@
+# -*- coding: utf-8 -*-
+# pylint: disable=C,R,W
+from __future__ import absolute_import
+from __future__ import division
+from __future__ import print_function
+from __future__ import unicode_literals
+
+from flask import request, Response
+from flask_appbuilder import expose
+from jinja2.sandbox import SandboxedEnvironment
+import simplejson as json
+from sqlalchemy import and_, func
+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.sql_lab import SavedQuery
+from superset.models.tags import ObjectTypes, Tag, TaggedObject, TagTypes
+from .base import BaseSupersetView
+
+
+class ObjectTypeConverter(BaseConverter):
+
+ """Validate that object_type is indeed an object type."""
+
+ def to_python(self, object_type):
+ return ObjectTypes[object_type]
+
+ def to_url(self, object_type):
+ return object_type.name
+
+
+def process_template(content):
+ env = SandboxedEnvironment()
+ template = env.from_string(content)
+ context = {
+ 'current_user_id': current_user_id,
+ 'current_username': current_username,
+ }
+ 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):
+
+ @expose('/tags/suggestions/', methods=['GET'])
+ def suggestions(self):
+ query = db.session.query(
+ TaggedObject,
+ ).group_by(TaggedObject.tag_id).order_by(func.count().desc()).all()
+ tags = json.dumps([
+ {'id': obj.tag.id, 'name': obj.tag.name} for obj in query])
+
+ return Response(tags, status=200, content_type='application/json')
Review comment:
I think you can use return `self.json_success(tags)` where tag is the
object, not the json string.
optional side-mission: Looks like we may want to move
https://github.com/apache/incubator-superset/blob/master/superset/views/core.py#L86
to `view/base.py` as a method of `BaseSupersetView`
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]