Skip to content

Commit

Permalink
Merge pull request #1877 from josenavas/adding-prep-info
Browse files Browse the repository at this point in the history
Adding prep template get handler
  • Loading branch information
antgonza committed Jul 6, 2016
2 parents 3932dc6 + bd70612 commit 4430c07
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
42 changes: 42 additions & 0 deletions qiita_db/handlers/prep_template.py
Expand Up @@ -7,6 +7,7 @@
# -----------------------------------------------------------------------------

from json import loads
from os.path import basename

from tornado.web import HTTPError
import pandas as pd
Expand Down Expand Up @@ -46,6 +47,47 @@ def _get_prep_template(pid):
return pt


class PrepTemplateDBHandler(OauthBaseHandler):
@authenticate_oauth
def get(self, prep_id):
"""Retrieves the prep template information
Parameters
----------
prep_id: str
The id of the prep template whose information is being retrieved
Returns
-------
dict
The prep information:
'data_type': prep info data type
'artifact': artifact attached to the given prep
'investigation_type': prep info investigation type
'study': study that the prep info belongs to
'status': prep info status
'qiime-map': the path to the qiime mapping file
'prep-file': the path to the prep info file
"""
with qdb.sql_connection.TRN:
pt = _get_prep_template(prep_id)
prep_files = [fp for _, fp in pt.get_filepaths()
if 'qiime' not in basename(fp)]
response = {
'data_type': pt.data_type(),
'artifact': pt.artifact.id,
'investigation_type': pt.investigation_type,
'study': pt.study_id,
'status': pt.status,
'qiime-map': pt.qiime_map_fp,
# The first element in the prep_files is the newest
# prep information file - hence the correct one
'prep-file': prep_files[0]
}

self.write(response)


class PrepTemplateDataHandler(OauthBaseHandler):
@authenticate_oauth
def get(self, prep_id):
Expand Down
29 changes: 29 additions & 0 deletions qiita_db/handlers/tests/test_prep_template.py
Expand Up @@ -8,6 +8,8 @@

from unittest import main, TestCase
from json import loads, dumps
from os.path import join
from functools import partial

from tornado.web import HTTPError

Expand All @@ -27,6 +29,33 @@ def test_get_prep_template(self):
_get_prep_template(100)


class PrepTemplateHandlerTests(OauthTestingBase):
def test_get_does_not_exist(self):
obs = self.get('/qiita_db/prep_template/100/', headers=self.header)
self.assertEqual(obs.code, 404)

def test_get_no_header(self):
obs = self.get('/qiita_db/prep_template/1/')
self.assertEqual(obs.code, 400)

def test_get(self):
obs = self.get('/qiita_db/prep_template/1/', headers=self.header)
self.assertEqual(obs.code, 200)

db_test_template_dir = qdb.util.get_mountpoint('templates')[0][1]
path_builder = partial(join, db_test_template_dir)

obs = loads(obs.body)
exp = {'data_type': '18S',
'artifact': 1,
'investigation_type': 'Metagenomics',
'study': 1,
'status': 'private',
'qiime-map': path_builder('1_prep_1_qiime_19700101-000000.txt'),
'prep-file': path_builder('1_prep_1_19700101-000000.txt')}
self.assertEqual(obs, exp)


class PrepTemplateDataHandlerTests(OauthTestingBase):
def test_get_does_not_exist(self):
obs = self.get('/qiita_db/prep_template/100/data/',
Expand Down
4 changes: 3 additions & 1 deletion qiita_pet/webserver.py
Expand Up @@ -46,7 +46,8 @@
ProcessingJobAPItestHandler)
from qiita_db.handlers.artifact import ArtifactHandler
from qiita_db.handlers.prep_template import (
PrepTemplateDataHandler, PrepTemplateAPItestHandler)
PrepTemplateDataHandler, PrepTemplateAPItestHandler,
PrepTemplateDBHandler)
from qiita_db.handlers.oauth2 import TokenAuthHandler
from qiita_db.handlers.reference import ReferenceFilepathsHandler
from qiita_db.handlers.core import ResetAPItestHandler
Expand Down Expand Up @@ -151,6 +152,7 @@ def __init__(self):
(r"/qiita_db/jobs/(.*)", JobHandler),
(r"/qiita_db/artifacts/(.*)/", ArtifactHandler),
(r"/qiita_db/prep_template/(.*)/data/", PrepTemplateDataHandler),
(r"/qiita_db/prep_template/(.*)/", PrepTemplateDBHandler),
(r"/qiita_db/references/(.*)/filepaths/",
ReferenceFilepathsHandler)
]
Expand Down

0 comments on commit 4430c07

Please sign in to comment.