|
a |
|
b/qiita_pet/handlers/public.py |
|
|
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 tornado.web import HTTPError |
|
|
10 |
from tornado.gen import coroutine |
|
|
11 |
|
|
|
12 |
from .base_handlers import BaseHandler |
|
|
13 |
from qiita_db.study import Study |
|
|
14 |
from qiita_db.artifact import Artifact |
|
|
15 |
from qiita_db.util import get_artifacts_information |
|
|
16 |
from qiita_db.exceptions import QiitaDBUnknownIDError |
|
|
17 |
from qiita_core.util import execute_as_transaction |
|
|
18 |
from qiita_pet.util import EBI_LINKIFIER |
|
|
19 |
from qiita_pet.handlers.util import doi_linkifier, pubmed_linkifier |
|
|
20 |
|
|
|
21 |
|
|
|
22 |
class PublicHandler(BaseHandler): |
|
|
23 |
@coroutine |
|
|
24 |
@execute_as_transaction |
|
|
25 |
def get(self): |
|
|
26 |
study_id = self.get_argument("study_id", None) |
|
|
27 |
artifact_id = self.get_argument("artifact_id", None) |
|
|
28 |
|
|
|
29 |
if study_id is None and artifact_id is None: |
|
|
30 |
raise HTTPError( |
|
|
31 |
422, reason='You need to specify study_id or artifact_id') |
|
|
32 |
self.finish() |
|
|
33 |
elif study_id is not None: |
|
|
34 |
try: |
|
|
35 |
study = Study(int(study_id)) |
|
|
36 |
except QiitaDBUnknownIDError: |
|
|
37 |
raise HTTPError( |
|
|
38 |
422, reason="Study %s doesn't exist" % study_id) |
|
|
39 |
self.finish() |
|
|
40 |
artifact_ids = [a.id for a in study.artifacts() |
|
|
41 |
if a.visibility == 'public'] |
|
|
42 |
else: |
|
|
43 |
try: |
|
|
44 |
artifact = Artifact(int(artifact_id)) |
|
|
45 |
except QiitaDBUnknownIDError: |
|
|
46 |
raise HTTPError( |
|
|
47 |
422, reason="Artifact %s doesn't exist" % artifact_id) |
|
|
48 |
self.finish() |
|
|
49 |
if artifact.visibility != 'public': |
|
|
50 |
raise HTTPError( |
|
|
51 |
422, reason="Artifact %s is not public" % artifact_id) |
|
|
52 |
self.finish() |
|
|
53 |
|
|
|
54 |
study = artifact.study |
|
|
55 |
if study is None: |
|
|
56 |
raise HTTPError(422, reason="Artifact %s doesn't belong to " |
|
|
57 |
"a study" % artifact_id) |
|
|
58 |
self.finish() |
|
|
59 |
artifact_ids = [artifact.id] |
|
|
60 |
|
|
|
61 |
if study.status != 'public': |
|
|
62 |
raise HTTPError( |
|
|
63 |
422, reason='Not a public study') |
|
|
64 |
self.finish() |
|
|
65 |
|
|
|
66 |
study_info = study.info |
|
|
67 |
study_info['study_id'] = study.id |
|
|
68 |
study_info['study_title'] = study.title |
|
|
69 |
study_info['shared_with'] = [s.id for s in study.shared_with] |
|
|
70 |
study_info['status'] = study.status |
|
|
71 |
study_info['ebi_study_accession'] = study.ebi_study_accession |
|
|
72 |
study_info['ebi_submission_status'] = study.ebi_submission_status |
|
|
73 |
|
|
|
74 |
# Clean up StudyPerson objects to string for display |
|
|
75 |
email = '<a href="mailto:{email}">{name} ({affiliation})</a>' |
|
|
76 |
pi = study.info['principal_investigator'] |
|
|
77 |
study_info['principal_investigator'] = email.format(**{ |
|
|
78 |
'name': pi.name, |
|
|
79 |
'email': pi.email, |
|
|
80 |
'affiliation': pi.affiliation}) |
|
|
81 |
|
|
|
82 |
study_info['owner'] = study.owner.id |
|
|
83 |
# Add needed info that is not part of the initial info pull |
|
|
84 |
study_info['publications'] = [] |
|
|
85 |
for pub, is_doi in study.publications: |
|
|
86 |
if is_doi: |
|
|
87 |
study_info['publications'].append(pubmed_linkifier([pub])) |
|
|
88 |
else: |
|
|
89 |
study_info['publications'].append(doi_linkifier([pub])) |
|
|
90 |
study_info['publications'] = ', '.join(study_info['publications']) |
|
|
91 |
|
|
|
92 |
if study_info['ebi_study_accession']: |
|
|
93 |
links = ''.join([EBI_LINKIFIER.format(a) for a in study_info[ |
|
|
94 |
'ebi_study_accession'].split(',')]) |
|
|
95 |
study_info['ebi_study_accession'] = '%s (%s)' % ( |
|
|
96 |
links, study_info['ebi_submission_status']) |
|
|
97 |
|
|
|
98 |
self.render("public.html", study_info=study_info, |
|
|
99 |
artifacts_info=get_artifacts_information( |
|
|
100 |
artifact_ids, False)) |