Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding prep template get handler #1877

Merged
merged 2 commits into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions qiita_db/handlers/prep_template.py
Original file line number Diff line number Diff line change
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,45 @@ 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,
'prep-file': prep_files[0]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add why [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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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