Skip to content

Commit

Permalink
Adding endpoint to retrieve list of person (#2103)
Browse files Browse the repository at this point in the history
* Adding missing endpoint

* Addressing @ElDeveloper comment
  • Loading branch information
josenavas authored and ElDeveloper committed Apr 10, 2017
1 parent a60cec2 commit dc0b029
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
36 changes: 24 additions & 12 deletions qiita_pet/handlers/rest/study_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------

from tornado.escape import json_encode
from tornado.web import MissingArgumentError

from qiita_db.handlers.oauth2 import authenticate_oauth
from qiita_db.study import StudyPerson
from qiita_db.exceptions import QiitaDBLookupError
Expand All @@ -15,18 +18,27 @@
class StudyPersonHandler(RESTHandler):
@authenticate_oauth
def get(self, *args, **kwargs):
name = self.get_argument('name')
affiliation = self.get_argument('affiliation')

try:
p = StudyPerson.from_name_and_affiliation(name, affiliation)
except QiitaDBLookupError:
self.fail('Person not found', 404)
return

self.write({'address': p.address, 'phone': p.phone, 'email': p.email,
'id': p.id})
self.finish()
name = self.get_argument('name', None)
affiliation = self.get_argument('affiliation', None)

if name is None and affiliation is None:
# Retrieve the list of all the StudyPerson
sp = [{'name': p.name, 'affiliation': p.affiliation}
for p in StudyPerson.iter()]
self.write(json_encode(sp))
self.finish()
elif name is not None and affiliation is not None:
try:
p = StudyPerson.from_name_and_affiliation(name, affiliation)
except QiitaDBLookupError:
self.fail('Person not found', 404)
return
self.write({'address': p.address, 'phone': p.phone,
'email': p.email, 'id': p.id})
self.finish()
else:
arg_name = 'name' if name is None else 'affiliation'
raise MissingArgumentError(arg_name)

@authenticate_oauth
def post(self, *args, **kwargs):
Expand Down
4 changes: 3 additions & 1 deletion qiita_pet/support_files/doc/source/dev/rest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ following endpoints:
+--------+-----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|PATCH | ``/api/v1/study/<int>/samples`` | Update sample metadata or add samples to the sample information. |
+--------+-----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|GET | ``/api/v1/study/<int>/samples?categories=foo,bar`` | Get metadata categories foo and bar for all samples in the study. |
|GET | ``/api/v1/study/<int>/samples?categories=foo,bar`` | Get metadata categories foo and bar for all samples in the study. |
+--------+-----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|GET | ``/api/v1/study/<int>/status`` | The status of a study (whether or not the study: is public, has sample information, sample information has warnings and a list of existing preparations. |
+--------+-----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|GET | ``/api/v1/person`` | Get list of persons. |
+--------+-----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|GET | ``/api/v1/person?name=foo&affiliation=bar`` | See if a person exists. |
+--------+-----------------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------------------+
|POST | ``/api/v1/study`` | Create a study (mirrors study creation on qiita UI with minimal requirements). |
Expand Down
14 changes: 14 additions & 0 deletions qiita_pet/test/rest/test_study_person.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@


class StudyPersonHandlerTests(RESTHandlerTestCase):
def test_get_list(self):
exp = [{'name': 'LabDude', 'affiliation': 'knight lab'},
{'name': 'empDude', 'affiliation': 'broad'},
{'name': 'PIDude', 'affiliation': 'Wash U'}]
response = self.get('/api/v1/person', headers=self.headers)
self.assertEqual(response.code, 200)
obs = json_decode(response.body)
self.assertItemsEqual(obs, exp)

def test_exist(self):
exp = {'email': 'lab_dude@foo.bar', 'phone': '121-222-3333',
'address': '123 lab street', 'id': 1}
Expand Down Expand Up @@ -45,6 +54,11 @@ def test_get_invalid_query_string(self):
headers=self.headers)
self.assertEqual(response.code, 400)

def test_get_invalid_query_string_2(self):
response = self.get('/api/v1/person?affiliation=knight%20lab',
headers=self.headers)
self.assertEqual(response.code, 400)

def test_get_valid_extra_arguments(self):
exp = {'email': 'lab_dude@foo.bar', 'phone': '121-222-3333',
'address': '123 lab street', 'id': 1}
Expand Down

0 comments on commit dc0b029

Please sign in to comment.