Skip to content

Commit

Permalink
Lyft view - amundsen edition
Browse files Browse the repository at this point in the history
(cherry picked from commit 9b3420c8ef51739206282514ca4a69fb01a2066f)
(cherry picked from commit 2df0fb8d286616cd4d5b9f582d337c726edd1d7a)
  • Loading branch information
mistercrunch committed Oct 17, 2018
1 parent 3008951 commit 2829ead
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions superset/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from . import lyft # noqa
from . import base # noqa
from . import core # noqa
from . import sql_lab # noqa
Expand Down
5 changes: 4 additions & 1 deletion superset/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2360,6 +2360,9 @@ def stop_query(self):
@expose('/sql_json/', methods=['POST', 'GET'])
@log_this
def sql_json(self):
return self.sql_json_call(request)

def sql_json_call(self, request):
"""Runs arbitrary sql and returns and json"""
async = request.form.get('runAsync') == 'true'
sql = request.form.get('sql')
Expand Down Expand Up @@ -2400,7 +2403,7 @@ def sql_json(self):
status=QueryStatus.PENDING if async else QueryStatus.RUNNING,
sql_editor_id=request.form.get('sql_editor_id'),
tmp_table_name=tmp_table_name,
user_id=int(g.user.get_id()),
user_id=int(g.user.get_id()) if g.user and g.user.get_id() else None,
client_id=request.form.get('client_id'),
)
session.add(query)
Expand Down
55 changes: 55 additions & 0 deletions superset/views/lyft.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from flask import Response, request, g
from flask_appbuilder import expose

from superset import app, appbuilder, security_manager
from superset.exceptions import SupersetException
import superset.models.core as models
from superset.views.core import Superset
from .base import json_error_response

config = app.config
stats_logger = config.get('STATS_LOGGER')
log_this = models.Log.log_this
DAR = models.DatasourceAccessRequest


class UserDontExistException(SupersetException):
pass


def json_success(json_msg, status=200):
return Response(json_msg, status=status, mimetype='application/json')


class Lyft(Superset):

def authorize(self):
"""Provides access if token, impersonates if specified"""
if not security_manager.has_tom_key():
raise SupersetException("Wrong key")

email = request.headers.get('IMPERSONATE')
if email:
user = security_manager.find_user(email=email)
if not user:
raise UserDontExistException("Email to impersonate not found")
g.user = user

@expose('/sql_json/', methods=['POST', 'GET'])
@log_this
def sql_json(self):
try:
self.authorize()
except UserDontExistException as e:
return json_error_response('{}'.format(e), status=412)
except Exception as e:
return json_error_response('{}'.format(e))
return self.sql_json_call(request)


appbuilder.add_view_no_menu(Lyft)

0 comments on commit 2829ead

Please sign in to comment.