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 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
3 changes: 1 addition & 2 deletions qiita_pet/handlers/analysis_handlers/sharing_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ def _unshare(self, analysis, user, callback):
def get(self):
analysis_id = int(self.get_argument('id'))
analysis = Analysis(analysis_id)
if self.current_user != analysis.owner and \
self.current_user not in analysis.shared_with:
if not analysis.has_access(self.current_user):
raise HTTPError(403, 'User %s does not have permissions to share '
'analysis %s' % (
self.current_user.id, analysis.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from unittest import main
from json import loads
from mock import Mock

from qiita_db.analysis import Analysis
from qiita_db.user import User
from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_pet.test.tornado_test_base import TestHandlerBase


Expand Down Expand Up @@ -49,6 +51,15 @@ def test_get(self):
'Analysis <a href="/analysis/description/1">\'SomeAnalysis\'</a> '
'has been shared with you.', u.messages()[0][1])

# admins can share
BaseHandler.get_current_user = Mock(return_value=User("admin@foo.bar"))
args = {'deselected': u.id, 'id': a.id}
response = self.get('/analysis/sharing/', args)
self.assertEqual(response.code, 200)
exp = {'users': [], 'links': ''}
self.assertEqual(loads(response.body), exp)
self.assertEqual(a.shared_with, [])

def test_get_no_access(self):
args = {'selected': 'demo@microbio.me', 'id': 2}
response = self.get('/analysis/sharing/', args)
Expand Down
81 changes: 63 additions & 18 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 @@ -51,20 +56,36 @@ def get(self, filepath_id):
self.finish()


class DownloadStudyBIOMSHandler(BaseHandler):
class BaseHandlerDownload(BaseHandler):
def _check_permissions(self, sid):
# 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, sid))
return Study(sid)

def _generate_files(self, header_name, accessions, filename):
text = "sample_name\t%s\n%s" % (header_name, '\n'.join(
["%s\t%s" % (k, v) for k, v in viewitems(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=%s' % filename)
self.write(text)
self.finish()


class DownloadStudyBIOMSHandler(BaseHandlerDownload):
@authenticated
@coroutine
@execute_as_transaction
def get(self, study_id):
study_id = int(study_id)
# Check access to study
study_info = study_get_req(study_id, self.current_user.id)

if study_info['status'] != 'success':
raise HTTPError(405, "%s: %s, %s" % (study_info['message'],
self.current_user.email,
str(study_id)))
study = self._check_permissions(study_id)

study = Study(study_id)
basedir = get_db_files_base_dir()
basedir_len = len(basedir) + 1
# loop over artifacts and retrieve those that we have access to
Expand Down Expand Up @@ -125,6 +146,7 @@ def get(self, study_id):


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

Expand All @@ -147,19 +169,13 @@ def get(self, extras):
self.finish()


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

study = Study(study_id)
study = self._check_permissions(study_id)
user = self.current_user
# Check "owner" access to the study
if not study.has_access(user, True):
Expand Down Expand Up @@ -222,3 +238,32 @@ 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(BaseHandlerDownload):
@authenticated
@coroutine
@execute_as_transaction
def get(self, study_id):
sid = int(study_id)
self._check_permissions(sid)

self._generate_files(
'sample_accession', SampleTemplate(sid).ebi_sample_accessions,
'ebi_sample_accessions_study_%s.tsv' % sid)


class DownloadEBIPrepAccessions(BaseHandlerDownload):
@authenticated
@coroutine
@execute_as_transaction
def get(self, prep_template_id):
pid = int(prep_template_id)
pt = PrepTemplate(pid)
sid = pt.study_id

self._check_permissions(sid)

self._generate_files(
'experiment_accession', pt.ebi_experiment_accessions,
'ebi_experiment_accessions_study_%s_prep_%s.tsv' % (sid, pid))
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