Skip to content

Commit a9aa1c4

Browse files
authored
Merge pull request #2287 from antgonza/fix-2258
fix #2258
2 parents 955d5cd + b482b4e commit a9aa1c4

File tree

7 files changed

+158
-23
lines changed

7 files changed

+158
-23
lines changed

qiita_pet/handlers/analysis_handlers/sharing_handlers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ def _unshare(self, analysis, user, callback):
4949
def get(self):
5050
analysis_id = int(self.get_argument('id'))
5151
analysis = Analysis(analysis_id)
52-
if self.current_user != analysis.owner and \
53-
self.current_user not in analysis.shared_with:
52+
if not analysis.has_access(self.current_user):
5453
raise HTTPError(403, 'User %s does not have permissions to share '
5554
'analysis %s' % (
5655
self.current_user.id, analysis.id))

qiita_pet/handlers/analysis_handlers/tests/test_sharing_handlers.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88

99
from unittest import main
1010
from json import loads
11+
from mock import Mock
1112

1213
from qiita_db.analysis import Analysis
1314
from qiita_db.user import User
15+
from qiita_pet.handlers.base_handlers import BaseHandler
1416
from qiita_pet.test.tornado_test_base import TestHandlerBase
1517

1618

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

54+
# admins can share
55+
BaseHandler.get_current_user = Mock(return_value=User("admin@foo.bar"))
56+
args = {'deselected': u.id, 'id': a.id}
57+
response = self.get('/analysis/sharing/', args)
58+
self.assertEqual(response.code, 200)
59+
exp = {'users': [], 'links': ''}
60+
self.assertEqual(loads(response.body), exp)
61+
self.assertEqual(a.shared_with, [])
62+
5263
def test_get_no_access(self):
5364
args = {'selected': 'demo@microbio.me', 'id': 2}
5465
response = self.get('/analysis/sharing/', args)

qiita_pet/handlers/download.py

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
# -----------------------------------------------------------------------------
88

99
from tornado.web import authenticated, HTTPError
10+
from tornado.gen import coroutine
1011

12+
from future.utils import viewitems
1113
from os.path import basename, getsize, join
1214
from os import walk
1315
from datetime import datetime
@@ -17,11 +19,14 @@
1719
from qiita_db.study import Study
1820
from qiita_db.util import filepath_id_to_rel_path, get_db_files_base_dir
1921
from qiita_db.meta_util import validate_filepath_access_by_user
22+
from qiita_db.metadata_template.sample_template import SampleTemplate
23+
from qiita_db.metadata_template.prep_template import PrepTemplate
2024
from qiita_core.util import execute_as_transaction, get_release_info
2125

2226

2327
class DownloadHandler(BaseHandler):
2428
@authenticated
29+
@coroutine
2530
@execute_as_transaction
2631
def get(self, filepath_id):
2732
fid = int(filepath_id)
@@ -51,20 +56,36 @@ def get(self, filepath_id):
5156
self.finish()
5257

5358

54-
class DownloadStudyBIOMSHandler(BaseHandler):
59+
class BaseHandlerDownload(BaseHandler):
60+
def _check_permissions(self, sid):
61+
# Check general access to study
62+
study_info = study_get_req(sid, self.current_user.id)
63+
if study_info['status'] != 'success':
64+
raise HTTPError(405, "%s: %s, %s" % (study_info['message'],
65+
self.current_user.email, sid))
66+
return Study(sid)
67+
68+
def _generate_files(self, header_name, accessions, filename):
69+
text = "sample_name\t%s\n%s" % (header_name, '\n'.join(
70+
["%s\t%s" % (k, v) for k, v in viewitems(accessions)]))
71+
72+
self.set_header('Content-Description', 'text/csv')
73+
self.set_header('Expires', '0')
74+
self.set_header('Cache-Control', 'no-cache')
75+
self.set_header('Content-Disposition', 'attachment; '
76+
'filename=%s' % filename)
77+
self.write(text)
78+
self.finish()
79+
80+
81+
class DownloadStudyBIOMSHandler(BaseHandlerDownload):
5582
@authenticated
83+
@coroutine
5684
@execute_as_transaction
5785
def get(self, study_id):
5886
study_id = int(study_id)
59-
# Check access to study
60-
study_info = study_get_req(study_id, self.current_user.id)
61-
62-
if study_info['status'] != 'success':
63-
raise HTTPError(405, "%s: %s, %s" % (study_info['message'],
64-
self.current_user.email,
65-
str(study_id)))
87+
study = self._check_permissions(study_id)
6688

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

126147

127148
class DownloadRelease(BaseHandler):
149+
@coroutine
128150
def get(self, extras):
129151
_, relpath, _ = get_release_info()
130152

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

149171

150-
class DownloadRawData(BaseHandler):
172+
class DownloadRawData(BaseHandlerDownload):
151173
@authenticated
174+
@coroutine
152175
@execute_as_transaction
153176
def get(self, study_id):
154177
study_id = int(study_id)
155-
# Check general access to study
156-
study_info = study_get_req(study_id, self.current_user.id)
157-
if study_info['status'] != 'success':
158-
raise HTTPError(405, "%s: %s, %s" % (study_info['message'],
159-
self.current_user.email,
160-
str(study_id)))
161-
162-
study = Study(study_id)
178+
study = self._check_permissions(study_id)
163179
user = self.current_user
164180
# Check "owner" access to the study
165181
if not study.has_access(user, True):
@@ -222,3 +238,32 @@ def get(self, study_id):
222238
self.set_header('Content-Disposition',
223239
'attachment; filename=%s' % zip_fn)
224240
self.finish()
241+
242+
243+
class DownloadEBISampleAccessions(BaseHandlerDownload):
244+
@authenticated
245+
@coroutine
246+
@execute_as_transaction
247+
def get(self, study_id):
248+
sid = int(study_id)
249+
self._check_permissions(sid)
250+
251+
self._generate_files(
252+
'sample_accession', SampleTemplate(sid).ebi_sample_accessions,
253+
'ebi_sample_accessions_study_%s.tsv' % sid)
254+
255+
256+
class DownloadEBIPrepAccessions(BaseHandlerDownload):
257+
@authenticated
258+
@coroutine
259+
@execute_as_transaction
260+
def get(self, prep_template_id):
261+
pid = int(prep_template_id)
262+
pt = PrepTemplate(pid)
263+
sid = pt.study_id
264+
265+
self._check_permissions(sid)
266+
267+
self._generate_files(
268+
'experiment_accession', pt.ebi_experiment_accessions,
269+
'ebi_experiment_accessions_study_%s_prep_%s.tsv' % (sid, pid))

qiita_pet/templates/study_ajax/base_info.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@
134134
</tr>
135135
</table>
136136

137+
{% if study_info['ebi_submission_status'] == 'submitted' %}
138+
<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>
139+
{% end %}
140+
137141
{% if share_access %}
138142
<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>
139143
{% end %}

qiita_pet/templates/study_ajax/prep_summary.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,14 @@ <h4>
473473
<div class="col-md-12">
474474
There are <b>{{num_samples}}</b> samples and <b>{{num_columns}}</b> columns in this preparation.</br>
475475
{% if is_submitted_to_ebi %}
476-
<b>EBI status: </b> Submitted
476+
<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>
477477
{% end %}
478478
</div>
479479
</div>
480480

481481
{% if editable %}
482482
<!-- Update prep template -->
483+
<hr/>
483484
<div class="row">
484485
<div class="col-md-12">
485486
<b>Update information:</b>
@@ -523,6 +524,7 @@ <h4>
523524
<button class="btn btn-info btn-sm" onclick="add_new_investigation_type_term_and_update();">Add</button>
524525
</div>
525526
</div>
527+
<hr/>
526528
{% end %}
527529

528530
<!-- Files graph or add artifact -->

qiita_pet/test/test_download.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,5 +203,75 @@ def test_download_raw_data(self):
203203
self.assertEqual(response.code, 405)
204204

205205

206+
class TestDownloadEBISampleAccessions(TestHandlerBase):
207+
208+
def setUp(self):
209+
super(TestDownloadEBISampleAccessions, self).setUp()
210+
211+
def tearDown(self):
212+
super(TestDownloadEBISampleAccessions, self).tearDown()
213+
214+
def test_download(self):
215+
# check success
216+
response = self.get('/download_ebi_accessions/samples/1')
217+
exp = ("sample_name\tsample_accession\n1.SKB2.640194\tERS000008\n"
218+
"1.SKM4.640180\tERS000004\n1.SKB3.640195\tERS000024\n"
219+
"1.SKB6.640176\tERS000025\n1.SKD6.640190\tERS000007\n"
220+
"1.SKM6.640187\tERS000022\n1.SKD9.640182\tERS000019\n"
221+
"1.SKM8.640201\tERS000014\n1.SKM2.640199\tERS000015\n"
222+
"1.SKD2.640178\tERS000009\n1.SKB7.640196\tERS000002\n"
223+
"1.SKD4.640185\tERS000023\n1.SKB8.640193\tERS000000\n"
224+
"1.SKM3.640197\tERS000018\n1.SKD5.640186\tERS000017\n"
225+
"1.SKB1.640202\tERS000011\n1.SKM1.640183\tERS000025\n"
226+
"1.SKD1.640179\tERS000012\n1.SKD3.640198\tERS000013\n"
227+
"1.SKB5.640181\tERS000006\n1.SKB4.640189\tERS000020\n"
228+
"1.SKB9.640200\tERS000016\n1.SKM9.640192\tERS000003\n"
229+
"1.SKD8.640184\tERS000001\n1.SKM5.640177\tERS000005\n"
230+
"1.SKM7.640188\tERS000010\n1.SKD7.640191\tERS000021")
231+
self.assertEqual(response.code, 200)
232+
self.assertRegexpMatches(response.body, exp)
233+
234+
# changing user so we can test the failures
235+
BaseHandler.get_current_user = Mock(
236+
return_value=User("demo@microbio.me"))
237+
response = self.get('/download_ebi_accessions/samples/1')
238+
self.assertEqual(response.code, 405)
239+
240+
241+
class TestDownloadEBIPrepAccessions(TestHandlerBase):
242+
243+
def setUp(self):
244+
super(TestDownloadEBIPrepAccessions, self).setUp()
245+
246+
def tearDown(self):
247+
super(TestDownloadEBIPrepAccessions, self).tearDown()
248+
249+
def test_download(self):
250+
# check success
251+
response = self.get('/download_ebi_accessions/experiments/1')
252+
exp = ("sample_name\texperiment_accession\n1.SKB2.640194\tERX0000008\n"
253+
"1.SKM4.640180\tERX0000004\n1.SKB3.640195\tERX0000024\n"
254+
"1.SKB6.640176\tERX0000025\n1.SKD6.640190\tERX0000007\n"
255+
"1.SKM6.640187\tERX0000022\n1.SKD9.640182\tERX0000019\n"
256+
"1.SKM8.640201\tERX0000014\n1.SKM2.640199\tERX0000015\n"
257+
"1.SKD2.640178\tERX0000009\n1.SKB7.640196\tERX0000002\n"
258+
"1.SKD4.640185\tERX0000023\n1.SKB8.640193\tERX0000000\n"
259+
"1.SKM3.640197\tERX0000018\n1.SKD5.640186\tERX0000017\n"
260+
"1.SKB1.640202\tERX0000011\n1.SKM1.640183\tERX0000026\n"
261+
"1.SKD1.640179\tERX0000012\n1.SKD3.640198\tERX0000013\n"
262+
"1.SKB5.640181\tERX0000006\n1.SKB4.640189\tERX0000020\n"
263+
"1.SKB9.640200\tERX0000016\n1.SKM9.640192\tERX0000003\n"
264+
"1.SKD8.640184\tERX0000001\n1.SKM5.640177\tERX0000005\n"
265+
"1.SKM7.640188\tERX0000010\n1.SKD7.640191\tERX0000021")
266+
self.assertEqual(response.code, 200)
267+
self.assertRegexpMatches(response.body, exp)
268+
269+
# changing user so we can test the failures
270+
BaseHandler.get_current_user = Mock(
271+
return_value=User("demo@microbio.me"))
272+
response = self.get('/download_ebi_accessions/experiments/1')
273+
self.assertEqual(response.code, 405)
274+
275+
206276
if __name__ == '__main__':
207277
main()

qiita_pet/webserver.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from qiita_pet.handlers.stats import StatsHandler
4848
from qiita_pet.handlers.download import (
4949
DownloadHandler, DownloadStudyBIOMSHandler, DownloadRelease,
50-
DownloadRawData)
50+
DownloadRawData, DownloadEBISampleAccessions, DownloadEBIPrepAccessions)
5151
from qiita_pet.handlers.prep_template import PrepTemplateHandler
5252
from qiita_pet.handlers.ontology import OntologyHandler
5353
from qiita_db.handlers.processing_job import (
@@ -162,8 +162,12 @@ def __init__(self):
162162
(r"/stats/", StatsHandler),
163163
(r"/download/(.*)", DownloadHandler),
164164
(r"/download_study_bioms/(.*)", DownloadStudyBIOMSHandler),
165-
(r"/release/download/(.*)", DownloadRelease),
166165
(r"/download_raw_data/(.*)", DownloadRawData),
166+
(r"/download_ebi_accessions/samples/(.*)",
167+
DownloadEBISampleAccessions),
168+
(r"/download_ebi_accessions/experiments/(.*)",
169+
DownloadEBIPrepAccessions),
170+
(r"/release/download/(.*)", DownloadRelease),
167171
(r"/vamps/(.*)", VAMPSHandler),
168172
(r"/redbiom/(.*)", RedbiomPublicSearch),
169173
# Plugin handlers - the order matters here so do not change

0 commit comments

Comments
 (0)