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

allow only specific columns in superset dataset #1567

Merged
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
1 change: 1 addition & 0 deletions questionnaires/superset/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALLOWED_COLUMNS = ['id', 'form_data', 'submit_time', 'page_id', 'user_id']
7 changes: 6 additions & 1 deletion questionnaires/superset/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def _get_headers(self):
headers.update(self._get_csrf_token())
return headers

def _get_params(self):
return {
'override_columns': 'true'
}

def _get_validated_response(self, response):
if response.ok:
return response.json()
Expand Down Expand Up @@ -82,7 +87,7 @@ def get_dataset(self, id):
return self._api_caller(request)

def update_dataset(self, id, data):
request = Request(method='PUT', url=f'{self.dataset_url}{id}', headers=self._get_headers(), json=data)
request = Request(method='PUT', url=f'{self.dataset_url}{id}', params=self._get_params(), headers=self._get_headers(), json=data)
return self._api_caller(request)

def create_chart(self, data):
Expand Down
6 changes: 5 additions & 1 deletion questionnaires/superset/datasets.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from questionnaires.superset import ALLOWED_COLUMNS


class Dataset:
def __init__(self, database_id, owner_id, table_name, dataset_name, page_id):
self.database_id = database_id
Expand All @@ -16,9 +19,10 @@ def post_body(self):
}

def put_body(self, columns, metrics):
sql = f"SELECT * " \
sql = f"SELECT {', '.join(ALLOWED_COLUMNS)} " \
f"FROM {self.table_name} " \
f"WHERE page_id = {self.page_id}"

return {
'sql': sql,
'table_name': self.dataset_name,
Expand Down
13 changes: 7 additions & 6 deletions questionnaires/superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from questionnaires.superset.client import SupersetClient
from questionnaires.superset.dashboard import Dashboard
from questionnaires.superset.datasets import Dataset
from questionnaires.superset import ALLOWED_COLUMNS

CHART_TYPE_MAP = {
'checkbox': PieChart,
Expand Down Expand Up @@ -85,12 +86,12 @@ def _create_dataset(self, database_id, owner_id):
dataset_id = resp.get('id')

dataset_detail = self.client.get_dataset(dataset_id)
columns = copy(dataset_detail.get('result', {}).get('columns'))
for column in columns:
column.pop('changed_on', None)
column.pop('created_on', None)
column.pop('type_generic', None)
column.pop('uuid', None)

columns = [
{'column_name': column.get('column_name')}
for column in dataset_detail.get('result', {}).get('columns')
if column.get('column_name') in ALLOWED_COLUMNS
]

for question in self.questions:
calculated_column_expression = CALCULATED_COLUMN_EXPRESSION_MAP.get(question.field_type)
Expand Down