[973924]: / qiita_pet / handlers / rest / study_preparation.py

Download this file

89 lines (70 with data), 3.0 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# -----------------------------------------------------------------------------
# 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.
# -----------------------------------------------------------------------------
import os
import pandas as pd
from tornado.escape import json_decode
from qiita_db.util import get_mountpoint
from qiita_db.artifact import Artifact
from qiita_pet.handlers.util import to_int
from qiita_db.exceptions import QiitaDBUnknownIDError, QiitaError
from qiita_db.metadata_template.prep_template import PrepTemplate
from qiita_db.handlers.oauth2 import authenticate_oauth
from .rest_handler import RESTHandler
class StudyPrepCreatorHandler(RESTHandler):
# TODO: do something smart about warnings, perhaps this should go in its
# own endpoint i.e. /api/v1/study/<int>/preparation/validate
# See also: https://github.com/biocore/qiita/issues/2096
@authenticate_oauth
def post(self, study_id, *args, **kwargs):
data_type = self.get_argument('data_type')
investigation_type = self.get_argument('investigation_type', None)
study_id = self.safe_get_study(study_id)
if study_id is None:
return
data = pd.DataFrame.from_dict(json_decode(self.request.body),
orient='index')
try:
p = PrepTemplate.create(data, study_id, data_type,
investigation_type)
except QiitaError as e:
self.fail(str(e), 406)
return
self.write({'id': p.id})
self.set_status(201)
self.finish()
class StudyPrepArtifactCreatorHandler(RESTHandler):
@authenticate_oauth
def post(self, study_id, prep_id):
study = self.safe_get_study(study_id)
if study is None:
return
prep_id = to_int(prep_id)
try:
p = PrepTemplate(prep_id)
except QiitaDBUnknownIDError:
self.fail('Preparation not found', 404)
return
if p.study_id != study.id:
self.fail('Preparation ID not associated with the study', 409)
return
artifact_deets = json_decode(self.request.body)
_, upload = get_mountpoint('uploads')[0]
base = os.path.join(upload, study_id)
filepaths = [(os.path.join(base, fp), fp_type)
for fp, fp_type in artifact_deets['filepaths']]
try:
art = Artifact.create(filepaths,
artifact_deets['artifact_type'],
artifact_deets['artifact_name'],
p)
except QiitaError as e:
self.fail(str(e), 406)
return
self.write({'id': art.id})
self.set_status(201)
self.finish()