|  | 
| 17 | 17 | from functools import partial | 
| 18 | 18 | 
 | 
| 19 | 19 | from tornado.web import authenticated, HTTPError, StaticFileHandler | 
|  | 20 | +from tornado.gen import coroutine, Task | 
| 20 | 21 | from moi import ctx_default, r_client | 
| 21 | 22 | from moi.job import submit | 
| 22 | 23 | from moi.group import get_id_from_user, create_info | 
| 23 | 24 | 
 | 
| 24 | 25 | from qiita_pet.util import is_localhost | 
| 25 | 26 | from qiita_pet.handlers.base_handlers import BaseHandler | 
| 26 |  | -from qiita_pet.handlers.util import download_link_or_path | 
|  | 27 | +from qiita_pet.handlers.util import ( | 
|  | 28 | +    download_link_or_path, get_shared_links) | 
| 27 | 29 | from qiita_pet.exceptions import QiitaPetAuthorizationError | 
| 28 | 30 | from qiita_ware.dispatchable import run_analysis | 
| 29 | 31 | from qiita_db.analysis import Analysis | 
| 30 | 32 | from qiita_db.artifact import Artifact | 
| 31 | 33 | from qiita_db.job import Command | 
| 32 |  | -from qiita_db.util import (get_db_files_base_dir, | 
|  | 34 | +from qiita_db.user import User | 
|  | 35 | +from qiita_db.util import (get_db_files_base_dir, add_message, | 
| 33 | 36 |                            check_access_to_analysis_result, | 
| 34 | 37 |                            filepath_ids_to_rel_paths, get_filepath_id) | 
| 35 | 38 | from qiita_db.exceptions import QiitaDBUnknownIDError | 
| @@ -175,10 +178,13 @@ def get(self, analysis_id): | 
| 175 | 178 |             data_type = proc_data.data_type | 
| 176 | 179 |             dropped[data_type].append((proc_data.study.title, len(samples), | 
| 177 | 180 |                                        ', '.join(samples))) | 
|  | 181 | +        share_access = (self.current_user.id in analysis.shared_with or | 
|  | 182 | +                        self.current_user.id == analysis.owner) | 
| 178 | 183 | 
 | 
| 179 | 184 |         self.render("analysis_results.html", analysis_id=analysis_id, | 
| 180 | 185 |                     jobres=jobres, aname=analysis.name, dropped=dropped, | 
| 181 |  | -                    basefolder=get_db_files_base_dir()) | 
|  | 186 | +                    basefolder=get_db_files_base_dir(), | 
|  | 187 | +                    share_access=share_access) | 
| 182 | 188 | 
 | 
| 183 | 189 |     @authenticated | 
| 184 | 190 |     @execute_as_transaction | 
| @@ -354,3 +360,49 @@ class AnalysisSummaryAJAX(BaseHandler): | 
| 354 | 360 |     def get(self): | 
| 355 | 361 |         info = self.current_user.default_analysis.summary_data() | 
| 356 | 362 |         self.write(dumps(info)) | 
|  | 363 | + | 
|  | 364 | + | 
|  | 365 | +class ShareAnalysisAJAX(BaseHandler): | 
|  | 366 | +    @execute_as_transaction | 
|  | 367 | +    def _get_shared_for_study(self, analysis, callback): | 
|  | 368 | +        shared_links = get_shared_links(analysis) | 
|  | 369 | +        users = [u.email for u in analysis.shared_with] | 
|  | 370 | +        callback((users, shared_links)) | 
|  | 371 | + | 
|  | 372 | +    @execute_as_transaction | 
|  | 373 | +    def _share(self, analysis, user, callback): | 
|  | 374 | +        user = User(user) | 
|  | 375 | +        add_message('Analysis <a href="/analysis/results/%d">\'%s\'</a> ' | 
|  | 376 | +                    'has been shared with you.' % | 
|  | 377 | +                    (analysis.id, analysis.name), [user]) | 
|  | 378 | +        callback(analysis.share(user)) | 
|  | 379 | + | 
|  | 380 | +    @execute_as_transaction | 
|  | 381 | +    def _unshare(self, analysis, user, callback): | 
|  | 382 | +        user = User(user) | 
|  | 383 | +        add_message('Analysis \'%s\' has been unshared from you.' % | 
|  | 384 | +                    analysis.name, [user]) | 
|  | 385 | +        callback(analysis.unshare(user)) | 
|  | 386 | + | 
|  | 387 | +    @authenticated | 
|  | 388 | +    @coroutine | 
|  | 389 | +    @execute_as_transaction | 
|  | 390 | +    def get(self): | 
|  | 391 | +        analysis_id = int(self.get_argument('id')) | 
|  | 392 | +        analysis = Analysis(analysis_id) | 
|  | 393 | +        if self.current_user != analysis.owner: | 
|  | 394 | +            raise HTTPError(403, 'User %s does not have permissions to share ' | 
|  | 395 | +                            'analysis %s' % ( | 
|  | 396 | +                                self.current_user.id, analysis.id)) | 
|  | 397 | + | 
|  | 398 | +        selected = self.get_argument('selected', None) | 
|  | 399 | +        deselected = self.get_argument('deselected', None) | 
|  | 400 | + | 
|  | 401 | +        if selected is not None: | 
|  | 402 | +            yield Task(self._share, analysis, selected) | 
|  | 403 | +        if deselected is not None: | 
|  | 404 | +            yield Task(self._unshare, analysis, deselected) | 
|  | 405 | + | 
|  | 406 | +        users, links = yield Task(self._get_shared_for_study, analysis) | 
|  | 407 | + | 
|  | 408 | +        self.write(dumps({'users': users, 'links': links})) | 
0 commit comments