Skip to content

Commit 015dd41

Browse files
josenavasantgonza
authored andcommitted
Analysis refactor gui part3 (#2078)
* fix #1505 * improving some GUI stuff * improving some GUI stuff - missing lines * addressing all comments * ready for review * fix #1987 * initial commit * requested changes * fix filter job list * Fixing server cert (#2051) * fix get_studies * flake8 * fix #503 * fix #2010 * fix #1913 * fix errors * addressing @josenavas comment * flake8 * fix #1010 * fix #1066 (#2058) * addressing @josenavas comments * fix #1961 * fix #1837 * Automatic jobs & new stats (#2057) * fix #814, fix #1636 * fixing error in test-env * fixing stats.html call * adding img * addressing @josenavas comments * rm for loops * addresssing @ElDeveloper comments * generalizing this functionality * fix #1816 * fix #1959 * addressing @josenavas comments * addressing @josenavas comments * fixing error * fixed? * addressing @josenavas comments * addressing @wasade comments * fix flake8 * generate biom and metadata release (#2066) * initial commit * adding portal * addressing @josenavas comments * pid -> qiita_artifact_id * addressing @josenavas comments * addressing @ElDeveloper comments * rm 50.sql * database changes to fix 969 * adding delete * addressing @josenavas comments * addressing @ElDeveloper comments * duh! * fix generate_biom_and_metadata_release (#2072) * fix generate_biom_and_metadata_release * addressing @ElDeveloper comment * Removing qiita ware code that will not be used anymore * Organizing the handlers and new analysis description page * Connecting the analysis creation and making interface responsive * Addressing @antgonza's comments * Initial artifact GUI refactor * Removing unused code * Adding can_edit call to the analysis * Fixing artifact rest API since not all artifacts have study * Adding can_be_publicized call to analysis * Adding QiitaHTTPError to handle errors gracefully * Adding safe_execution contextmanager * Fixing typo * Adding qiita test checker * Adapting some artifact handlers * Fixing typo * Addressing @antgonza's comments * Addressing @antgonza's comments
1 parent 22156ed commit 015dd41

File tree

14 files changed

+983
-9
lines changed

14 files changed

+983
-9
lines changed

qiita_db/analysis.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,26 @@ def pmid(self, pmid):
514514
qdb.sql_connection.TRN.add(sql, [pmid, self._id])
515515
qdb.sql_connection.TRN.execute()
516516

517+
@property
518+
def can_be_publicized(self):
519+
"""Returns whether the analysis can be made public
520+
521+
Returns
522+
-------
523+
bool
524+
Whether the analysis can be publicized or not
525+
"""
526+
# The analysis can be made public if all the artifacts used
527+
# to get the samples from are public
528+
with qdb.sql_connection.TRN:
529+
sql = """SELECT DISTINCT artifact_id
530+
FROM qiita.analysis_sample
531+
WHERE analysis_id = %s"""
532+
qdb.sql_connection.TRN.add(sql, [self.id])
533+
return all(
534+
[qdb.artifact.Artifact(aid).visibility == 'public'
535+
for aid in qdb.sql_connection.TRN.execute_fetchflatten()])
536+
517537
def add_artifact(self, artifact):
518538
"""Adds an artifact to the analysis
519539
@@ -570,6 +590,24 @@ def has_access(self, user):
570590
return self in Analysis.get_by_status('public') | \
571591
user.private_analyses | user.shared_analyses
572592

593+
def can_edit(self, user):
594+
"""Returns whether the given user can edit the analysis
595+
596+
Parameters
597+
----------
598+
user : User object
599+
User we are checking edit permissions for
600+
601+
Returns
602+
-------
603+
bool
604+
Whether user can edit the study or not
605+
"""
606+
# The analysis is editable only if the user is the owner, is in the
607+
# shared list or the user is an admin
608+
return (user.level in {'superuser', 'admin'} or self.owner == user or
609+
user in self.shared_with)
610+
573611
def summary_data(self):
574612
"""Return number of studies, artifacts, and samples selected
575613

qiita_db/handlers/artifact.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def get(self, artifact_id):
7979
"""
8080
with qdb.sql_connection.TRN:
8181
artifact = _get_artifact(artifact_id)
82+
study = artifact.study
8283
response = {
8384
'name': artifact.name,
8485
'timestamp': str(artifact.timestamp),
@@ -89,7 +90,7 @@ def get(self, artifact_id):
8990
'can_be_submitted_to_vamps':
9091
artifact.can_be_submitted_to_vamps,
9192
'prep_information': [p.id for p in artifact.prep_templates],
92-
'study': artifact.study.id}
93+
'study': study.id if study else None}
9394
params = artifact.processing_parameters
9495
response['processing_parameters'] = (
9596
params.values if params is not None else None)

qiita_db/test/test_analysis.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,27 @@ def test_get_by_status(self):
125125
self.assertEqual(
126126
qdb.analysis.Analysis.get_by_status('public'), set([]))
127127

128+
def test_can_be_publicized(self):
129+
analysis = qdb.analysis.Analysis(1)
130+
self.assertFalse(analysis.can_be_publicized)
131+
a4 = qdb.artifact.Artifact(4)
132+
a5 = qdb.artifact.Artifact(5)
133+
a6 = qdb.artifact.Artifact(6)
134+
135+
a4.visibility = 'public'
136+
self.assertFalse(analysis.can_be_publicized)
137+
138+
a5.visibility = 'public'
139+
self.assertFalse(analysis.can_be_publicized)
140+
141+
a6.visibility = 'public'
142+
self.assertTrue(analysis.can_be_publicized)
143+
144+
a4.visibility = 'private'
145+
a5.visibility = 'private'
146+
a6.visibility = 'private'
147+
self.assertFalse(analysis.can_be_publicized)
148+
128149
def test_add_artifact(self):
129150
obs = self._create_analyses_with_samples()
130151
exp = qdb.artifact.Artifact(4)
@@ -162,6 +183,13 @@ def test_has_access_no_access(self):
162183
self.assertFalse(
163184
self.analysis.has_access(qdb.user.User("demo@microbio.me")))
164185

186+
def test_can_edit(self):
187+
a = qdb.analysis.Analysis(1)
188+
self.assertTrue(a.can_edit(qdb.user.User('test@foo.bar')))
189+
self.assertTrue(a.can_edit(qdb.user.User('shared@foo.bar')))
190+
self.assertTrue(a.can_edit(qdb.user.User('admin@foo.bar')))
191+
self.assertFalse(a.can_edit(qdb.user.User('demo@microbio.me')))
192+
165193
def test_create_nonqiita_portal(self):
166194
qiita_config.portal = "EMP"
167195
obs = qdb.analysis.Analysis.create(

qiita_pet/exceptions.py

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

99
from __future__ import division
10+
11+
from tornado.web import HTTPError
12+
1013
from qiita_core.exceptions import QiitaError
1114

1215

16+
class QiitaHTTPError(HTTPError):
17+
def __init__(self, status_code=500, log_message=None, *args, **kwargs):
18+
super(QiitaHTTPError, self).__init__(
19+
status_code, log_message, *args, **kwargs)
20+
# The HTTPError has an attribute named "reason" that will get send to
21+
# the requester if specified. However, the developer need to
22+
# specifically pass the keyword "reason" when raising the exception.
23+
# The vast majority of our code it is not using the keyword "reason"
24+
# but we are using "log_message". By setting up the attribute reason
25+
# with the value in log_message, we make sure that when the answer
26+
# is sent to the requester, it will contain a useful error message,
27+
# rather than a generic error message.
28+
if not self.reason:
29+
self.reason = log_message
30+
31+
1332
class QiitaPetAuthorizationError(QiitaError):
1433
"""When a user tries to access a resource without proper authorization"""
1534
def __init__(self, user_id, resource_name_str):

qiita_pet/handlers/analysis_handlers/tests/test_base_handlers.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111

1212
from tornado.web import HTTPError
1313

14+
from qiita_core.util import qiita_test_checker
1415
from qiita_db.user import User
1516
from qiita_db.analysis import Analysis
1617
from qiita_pet.test.tornado_test_base import TestHandlerBase
1718
from qiita_pet.handlers.analysis_handlers.base_handlers import (
1819
analyisis_graph_handler_get_request)
1920

2021

22+
@qiita_test_checker()
2123
class TestBaseHandlersUtils(TestCase):
2224
def test_analyisis_graph_handler_get_request(self):
2325
obs = analyisis_graph_handler_get_request(1, User('test@foo.bar'))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# -----------------------------------------------------------------------------
2+
# Copyright (c) 2014--, The Qiita Development Team.
3+
#
4+
# Distributed under the terms of the BSD 3-clause License.
5+
#
6+
# The full license is in the file LICENSE, distributed with this software.
7+
# -----------------------------------------------------------------------------
8+
9+
from .base_handlers import ArtifactSummaryAJAX, ArtifactAJAX
10+
11+
__all__ = ['ArtifactSummaryAJAX', 'ArtifactAJAX']

0 commit comments

Comments
 (0)