Skip to content

Adding endpoint to retrieve list of person #2103

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

Merged
merged 2 commits into from
Apr 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this will need to be updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nope - the response code of a missing argument is 400 :-) I was basically replicating the behavior of that exception with less readable code xD

Copy link
Contributor

Choose a reason for hiding this comment

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

👍


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