Skip to content

Commit

Permalink
Hooked and fixed CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
mistercrunch committed Sep 11, 2016
1 parent e21067c commit 1b3ea85
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
5 changes: 4 additions & 1 deletion caravel/assets/javascripts/SqlLab/components/ResultSet.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class ResultSet extends React.Component {
changeSearch(event) {
this.setState({ searchText: event.target.value });
}
csv() {
window.location = '/caravel/csv/' + this.props.query.id;
}
showModal() {
this.setState({ showModal: true });
}
Expand All @@ -43,7 +46,7 @@ class ResultSet extends React.Component {
>
<i className="fa fa-line-chart m-l-1" /> Visualize
</Button>
<Button bsSize="small">
<Button bsSize="small" onClick={this.csv.bind(this)}>
<i className="fa fa-file-text-o" /> .CSV
</Button>
</ButtonGroup>
Expand Down
11 changes: 9 additions & 2 deletions caravel/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import functools
import json
import logging
import re
import textwrap
from collections import namedtuple
from copy import deepcopy, copy
Expand Down Expand Up @@ -1880,7 +1881,7 @@ class Query(Model):

__tablename__ = 'query'
id = Column(Integer, primary_key=True)
client_id = Column(String(11))
client_id = Column(String(11), unique=True)

database_id = Column(Integer, ForeignKey('dbs.id'), nullable=False)

Expand All @@ -1889,7 +1890,6 @@ class Query(Model):
user_id = Column(
Integer, ForeignKey('ab_user.id'), nullable=True)
status = Column(String(16), default=QueryStatus.PENDING)
name = Column(String(256))
tab_name = Column(String(256))
sql_editor_id = Column(String(256))
schema = Column(String(256))
Expand Down Expand Up @@ -1946,3 +1946,10 @@ def to_dict(self):
'tempTable': self.tmp_table_name,
'userId': self.user_id,
}
@property
def name(self):
ts = datetime.now().isoformat()
ts = ts.replace('-', '').replace(':', '').split('.')[0]
tab = self.tab_name.replace(' ', '_').lower()
tab = re.sub(r'\W+', '', tab)
return "sqllab_{tab}_{ts}".format(**locals())
11 changes: 7 additions & 4 deletions caravel/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1598,12 +1598,15 @@ def sql_json(self):
mimetype="application/json")

@has_access
@expose("/csv/<query_id>")
@expose("/csv/<client_id>")
@log_this
def csv(self, query_id):
def csv(self, client_id):
"""Download the query results as csv."""
s = db.session()
query = s.query(models.Query).filter_by(id=int(query_id)).first()
query = (
db.session.query(models.Query)
.filter_by(client_id=client_id)
.first()
)

if not self.database_access(query.database):
flash(get_database_access_error_msg(query.database.database_name))
Expand Down

0 comments on commit 1b3ea85

Please sign in to comment.