[973924]: / qiita_db / handlers / studies.py

Download this file

68 lines (55 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from tornado.web import HTTPError
from qiita_db.sql_connection import TRN
from .oauth2 import OauthBaseHandler, authenticate_oauth
def _generate_study_list_for_api(visibility, only_biom=True):
"""Get general study information
Parameters
----------
visibility : string
The visibility to get studies
Returns
-------
list of dict
The list of studies and their information
"""
artifact_type = ''
if only_biom:
artifact_type = "AND artifact_type = 'BIOM'"
sql = f"""
SELECT study_id, array_agg(DISTINCT artifact_id) FROM qiita.study
INNER JOIN qiita.study_artifact USING (study_id)
INNER JOIN qiita.artifact USING (artifact_id)
INNER JOIN qiita.artifact_type USING (artifact_type_id)
INNER JOIN qiita.visibility USING (visibility_id)
WHERE visibility = %s
{artifact_type}
GROUP BY study_id
"""
with TRN:
TRN.add(sql, [visibility])
return dict(TRN.execute_fetchindex())
class APIStudiesListing(OauthBaseHandler):
@authenticate_oauth
def get(self, visibility):
"""Retrieves the studies and their BIOM artifacts in visibility
Parameters
----------
visibility : str {'public', 'sandbox'}
The visibility of the studies and artifacts requested
Returns
-------
see qiita_db.util.generate_study_list
"""
if visibility not in {'public', 'private'}:
raise HTTPError(
403, reason='You can only request public or private studies')
response = {
'data': _generate_study_list_for_api(visibility=visibility)}
self.write(response)