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

Add: get_by_id & get_by_name methods for Query and DataSource classes #1513

Merged
merged 1 commit into from
Jan 10, 2017
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
13 changes: 12 additions & 1 deletion redash/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,14 @@ def update_group_permission(self, group, view_only):
def query_runner(self):
return get_query_runner(self.type, self.options)

@classmethod
def get_by_id(cls, _id):
return cls.query.filter(cls.id == _id).one()

@classmethod
def get_by_name(cls, name):
return cls.query.filter(cls.name == name).one()

@classmethod
def all(cls, org, group_ids=None):
data_sources = cls.query.filter(cls.org == org).order_by(cls.id.asc())
Expand Down Expand Up @@ -830,6 +838,10 @@ def recent(cls, group_ids, user_id=None, limit=20):

return query

@classmethod
def get_by_id(cls, _id):
return cls.query.filter(cls.id == _id).one()

def fork(self, user):
forked_list = ['org', 'data_source', 'latest_query_data', 'description',
'query_text', 'query_hash']
Expand Down Expand Up @@ -1497,4 +1509,3 @@ def init_db():
#XXX remove after fixing User.group_ids
db.session.commit()
return default_org, admin_group, default_group

8 changes: 4 additions & 4 deletions redash/query_runner/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ def execute_query(self, data_source_name_or_id, query):
if type(data_source_name_or_id) == int:
data_source = models.DataSource.get_by_id(data_source_name_or_id)
else:
data_source = models.DataSource.get(models.DataSource.name==data_source_name_or_id)
except models.DataSource.DoesNotExist:
raise Exception("Wrong data source name/id: %s." % data_source_name_or_id)
data_source = models.DataSource.get_by_name(data_source_name_or_id)
except models.NoResultFound:
raise Exception("Wrong data source name/id: %s." % data_source_name_or_id)

# TODO: pass the user here...
data, error = data_source.query_runner.run_query(query, None)
Expand All @@ -175,7 +175,7 @@ def get_query_result(self, query_id):
"""
try:
query = models.Query.get_by_id(query_id)
except models.Query.DoesNotExist:
except models.NoResultFound:
raise Exception("Query id %s does not exist." % query_id)

if query.latest_query_data is None:
Expand Down