Skip to content

fix #2258 #2287

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

Merged
merged 4 commits into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix #2258
  • Loading branch information
antgonza committed Sep 14, 2017
commit 49015c216b3896c8fd0e486a9bd3399bc43d1f26
64 changes: 64 additions & 0 deletions qiita_pet/handlers/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
# -----------------------------------------------------------------------------

from tornado.web import authenticated, HTTPError
from tornado.gen import coroutine

from future.utils import viewitems
from os.path import basename, getsize, join
from os import walk
from datetime import datetime
Expand All @@ -17,11 +19,14 @@
from qiita_db.study import Study
from qiita_db.util import filepath_id_to_rel_path, get_db_files_base_dir
from qiita_db.meta_util import validate_filepath_access_by_user
from qiita_db.metadata_template.sample_template import SampleTemplate
from qiita_db.metadata_template.prep_template import PrepTemplate
from qiita_core.util import execute_as_transaction, get_release_info


class DownloadHandler(BaseHandler):
@authenticated
@coroutine
@execute_as_transaction
def get(self, filepath_id):
fid = int(filepath_id)
Expand Down Expand Up @@ -53,6 +58,7 @@ def get(self, filepath_id):

class DownloadStudyBIOMSHandler(BaseHandler):
@authenticated
@coroutine
@execute_as_transaction
def get(self, study_id):
study_id = int(study_id)
Expand Down Expand Up @@ -125,6 +131,7 @@ def get(self, study_id):


class DownloadRelease(BaseHandler):
@coroutine
def get(self, extras):
_, relpath, _ = get_release_info()

Expand All @@ -149,6 +156,7 @@ def get(self, extras):

class DownloadRawData(BaseHandler):
@authenticated
@coroutine
@execute_as_transaction
def get(self, study_id):
study_id = int(study_id)
Expand Down Expand Up @@ -222,3 +230,59 @@ def get(self, study_id):
self.set_header('Content-Disposition',
'attachment; filename=%s' % zip_fn)
self.finish()


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two handlers are doing pretty much the same, can this be factored in to a base class or in a helper function?

class DownloadEBISampleAccessions(BaseHandler):
@authenticated
@coroutine
@execute_as_transaction
def get(self, study_id):
sid = int(study_id)
# Check general access to study
study_info = study_get_req(sid, self.current_user.id)
if study_info['status'] != 'success':
raise HTTPError(405, "%s: %s, %s" % (study_info['message'],
self.current_user.email,
study_id))

st = SampleTemplate(sid)

text = "sample_name\tsample_accession\n%s" % '\n'.join(
["%s\t%s" % (k, v)
for k, v in viewitems(st.ebi_sample_accessions)])

self.set_header('Content-Description', 'text/csv')
self.set_header('Expires', '0')
self.set_header('Cache-Control', 'no-cache')
self.set_header('Content-Disposition', 'attachment; '
'filename=ebi_sample_accessions_study_%s.tsv' % sid)
self.write(text)
self.finish()


class DownloadEBIPrepAccessions(BaseHandler):
@authenticated
@coroutine
@execute_as_transaction
def get(self, prep_template_id):
pid = int(prep_template_id)
pt = PrepTemplate(pid)
sid = pt.study_id
# Check general access to study
study_info = study_get_req(sid, self.current_user.id)
if study_info['status'] != 'success':
raise HTTPError(405, "%s: %s, %s" % (study_info['message'],
self.current_user.email,
str(sid)))

text = "sample_name\texperiment_accession\n%s" % '\n'.join(
["%s\t%s" % (k, v)
for k, v in viewitems(pt.ebi_experiment_accessions)])

self.set_header('Content-Description', 'text/csv')
self.set_header('Expires', '0')
self.set_header('Cache-Control', 'no-cache')
self.set_header('Content-Disposition', 'attachment; '
'filename=ebi_sample_accessions_study_%s.tsv' % sid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the name of the file here so it's not the same as above? I would say something along the lines of ebi_experiment_accessions_study_%s_prep_%s.tsv

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, I forgot to change that!

self.write(text)
self.finish()
4 changes: 4 additions & 0 deletions qiita_pet/templates/study_ajax/base_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,10 @@
</tr>
</table>

{% if study_info['ebi_submission_status'] == 'submitted' %}
<a class="btn btn-default" href="{% raw qiita_config.portal_dir %}/download_ebi_accessions/samples/{{study_info['study_id']}}"><span class="glyphicon glyphicon-download-alt"></span> EBI sample accessions</a>
{% end %}

{% if share_access %}
<a class="btn btn-default" data-toggle="modal" data-target="#share-study-modal-view" onclick="modify_sharing({{study_info['study_id']}});"><span class="glyphicon glyphicon-share"></span> Share</a>
{% end %}
Expand Down
4 changes: 3 additions & 1 deletion qiita_pet/templates/study_ajax/prep_summary.html
Original file line number Diff line number Diff line change
Expand Up @@ -473,13 +473,14 @@ <h4>
<div class="col-md-12">
There are <b>{{num_samples}}</b> samples and <b>{{num_columns}}</b> columns in this preparation.</br>
{% if is_submitted_to_ebi %}
<b>EBI status: </b> Submitted
<a class="btn btn-default" href="{% raw qiita_config.portal_dir %}/download_ebi_accessions/experiments/{{prep_id}}"><span class="glyphicon glyphicon-download-alt"></span> EBI experiment accessions</a>
{% end %}
</div>
</div>

{% if editable %}
<!-- Update prep template -->
<hr/>
<div class="row">
<div class="col-md-12">
<b>Update information:</b>
Expand Down Expand Up @@ -523,6 +524,7 @@ <h4>
<button class="btn btn-info btn-sm" onclick="add_new_investigation_type_term_and_update();">Add</button>
</div>
</div>
<hr/>
{% end %}

<!-- Files graph or add artifact -->
Expand Down
70 changes: 70 additions & 0 deletions qiita_pet/test/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,5 +203,75 @@ def test_download_raw_data(self):
self.assertEqual(response.code, 405)


class TestDownloadEBISampleAccessions(TestHandlerBase):

def setUp(self):
super(TestDownloadEBISampleAccessions, self).setUp()

def tearDown(self):
super(TestDownloadEBISampleAccessions, self).tearDown()

def test_download(self):
# check success
response = self.get('/download_ebi_accessions/samples/1')
exp = ("sample_name\tsample_accession\n1.SKB2.640194\tERS000008\n"
"1.SKM4.640180\tERS000004\n1.SKB3.640195\tERS000024\n"
"1.SKB6.640176\tERS000025\n1.SKD6.640190\tERS000007\n"
"1.SKM6.640187\tERS000022\n1.SKD9.640182\tERS000019\n"
"1.SKM8.640201\tERS000014\n1.SKM2.640199\tERS000015\n"
"1.SKD2.640178\tERS000009\n1.SKB7.640196\tERS000002\n"
"1.SKD4.640185\tERS000023\n1.SKB8.640193\tERS000000\n"
"1.SKM3.640197\tERS000018\n1.SKD5.640186\tERS000017\n"
"1.SKB1.640202\tERS000011\n1.SKM1.640183\tERS000025\n"
"1.SKD1.640179\tERS000012\n1.SKD3.640198\tERS000013\n"
"1.SKB5.640181\tERS000006\n1.SKB4.640189\tERS000020\n"
"1.SKB9.640200\tERS000016\n1.SKM9.640192\tERS000003\n"
"1.SKD8.640184\tERS000001\n1.SKM5.640177\tERS000005\n"
"1.SKM7.640188\tERS000010\n1.SKD7.640191\tERS000021")
self.assertEqual(response.code, 200)
self.assertRegexpMatches(response.body, exp)

# changing user so we can test the failures
BaseHandler.get_current_user = Mock(
return_value=User("demo@microbio.me"))
response = self.get('/download_ebi_accessions/samples/1')
self.assertEqual(response.code, 405)


class TestDownloadEBIPrepAccessions(TestHandlerBase):

def setUp(self):
super(TestDownloadEBIPrepAccessions, self).setUp()

def tearDown(self):
super(TestDownloadEBIPrepAccessions, self).tearDown()

def test_download(self):
# check success
response = self.get('/download_ebi_accessions/experiments/1')
exp = ("sample_name\texperiment_accession\n1.SKB2.640194\tERX0000008\n"
"1.SKM4.640180\tERX0000004\n1.SKB3.640195\tERX0000024\n"
"1.SKB6.640176\tERX0000025\n1.SKD6.640190\tERX0000007\n"
"1.SKM6.640187\tERX0000022\n1.SKD9.640182\tERX0000019\n"
"1.SKM8.640201\tERX0000014\n1.SKM2.640199\tERX0000015\n"
"1.SKD2.640178\tERX0000009\n1.SKB7.640196\tERX0000002\n"
"1.SKD4.640185\tERX0000023\n1.SKB8.640193\tERX0000000\n"
"1.SKM3.640197\tERX0000018\n1.SKD5.640186\tERX0000017\n"
"1.SKB1.640202\tERX0000011\n1.SKM1.640183\tERX0000026\n"
"1.SKD1.640179\tERX0000012\n1.SKD3.640198\tERX0000013\n"
"1.SKB5.640181\tERX0000006\n1.SKB4.640189\tERX0000020\n"
"1.SKB9.640200\tERX0000016\n1.SKM9.640192\tERX0000003\n"
"1.SKD8.640184\tERX0000001\n1.SKM5.640177\tERX0000005\n"
"1.SKM7.640188\tERX0000010\n1.SKD7.640191\tERX0000021")
self.assertEqual(response.code, 200)
self.assertRegexpMatches(response.body, exp)

# changing user so we can test the failures
BaseHandler.get_current_user = Mock(
return_value=User("demo@microbio.me"))
response = self.get('/download_ebi_accessions/experiments/1')
self.assertEqual(response.code, 405)


if __name__ == '__main__':
main()
8 changes: 6 additions & 2 deletions qiita_pet/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from qiita_pet.handlers.stats import StatsHandler
from qiita_pet.handlers.download import (
DownloadHandler, DownloadStudyBIOMSHandler, DownloadRelease,
DownloadRawData)
DownloadRawData, DownloadEBISampleAccessions, DownloadEBIPrepAccessions)
from qiita_pet.handlers.prep_template import PrepTemplateHandler
from qiita_pet.handlers.ontology import OntologyHandler
from qiita_db.handlers.processing_job import (
Expand Down Expand Up @@ -162,8 +162,12 @@ def __init__(self):
(r"/stats/", StatsHandler),
(r"/download/(.*)", DownloadHandler),
(r"/download_study_bioms/(.*)", DownloadStudyBIOMSHandler),
(r"/release/download/(.*)", DownloadRelease),
(r"/download_raw_data/(.*)", DownloadRawData),
(r"/download_ebi_accessions/samples/(.*)",
DownloadEBISampleAccessions),
(r"/download_ebi_accessions/experiments/(.*)",
DownloadEBIPrepAccessions),
(r"/release/download/(.*)", DownloadRelease),
(r"/vamps/(.*)", VAMPSHandler),
(r"/redbiom/(.*)", RedbiomPublicSearch),
# Plugin handlers - the order matters here so do not change
Expand Down