-
Notifications
You must be signed in to change notification settings - Fork 80
Fix #3149 #3152
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
Fix #3149 #3152
Changes from all commits
93f90ef
90f96a2
5601606
e0ab7f9
a899853
ff35131
c141406
5bdd632
80ea238
7e6f5b2
20ba195
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# ----------------------------------------------------------------------------- | ||
# 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. | ||
# ----------------------------------------------------------------------------- | ||
|
||
from tornado.web import HTTPError | ||
|
||
from qiita_db.sql_connection import TRN | ||
from .oauth2 import OauthBaseHandler, authenticate_oauth | ||
|
||
|
||
def _generate_study_list_for_api(visibility, only_biom=True): | ||
"""Get general study information | ||
|
||
Parameters | ||
---------- | ||
visibility : string | ||
The visibility to get studies | ||
|
||
Returns | ||
------- | ||
list of dict | ||
The list of studies and their information | ||
""" | ||
artifact_type = '' | ||
if only_biom: | ||
artifact_type = "AND artifact_type = 'BIOM'" | ||
|
||
sql = f""" | ||
SELECT study_id, array_agg(DISTINCT artifact_id) FROM qiita.study | ||
INNER JOIN qiita.study_artifact USING (study_id) | ||
INNER JOIN qiita.artifact USING (artifact_id) | ||
INNER JOIN qiita.artifact_type USING (artifact_type_id) | ||
INNER JOIN qiita.visibility USING (visibility_id) | ||
WHERE visibility = %s | ||
{artifact_type} | ||
GROUP BY study_id | ||
""" | ||
with TRN: | ||
TRN.add(sql, [visibility]) | ||
return dict(TRN.execute_fetchindex()) | ||
|
||
|
||
class APIStudiesListing(OauthBaseHandler): | ||
@authenticate_oauth | ||
def get(self, visibility): | ||
"""Retrieves the studies and their BIOM artifacts in visibility | ||
|
||
Parameters | ||
---------- | ||
visibility : str {'public', 'sandbox'} | ||
The visibility of the studies and artifacts requested | ||
|
||
Returns | ||
------- | ||
see qiita_db.util.generate_study_list | ||
""" | ||
if visibility not in {'public', 'private'}: | ||
raise HTTPError( | ||
403, reason='You can only request public or private studies') | ||
|
||
response = { | ||
'data': _generate_study_list_for_api(visibility=visibility)} | ||
self.write(response) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# ----------------------------------------------------------------------------- | ||
# 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. | ||
# ----------------------------------------------------------------------------- | ||
|
||
from unittest import main | ||
from json import loads | ||
|
||
from qiita_db.handlers.tests.oauthbase import OauthTestingBase | ||
|
||
|
||
class TestAPIStudiesListing(OauthTestingBase): | ||
def setUp(self): | ||
super(TestAPIStudiesListing, self).setUp() | ||
|
||
def test_get_studies_failure(self): | ||
obs = self.get('/qiita_db/studies/not-valid', headers=self.header) | ||
self.assertEqual(obs.code, 403) | ||
self.assertEqual(str(obs.error), 'HTTP 403: You can only request ' | ||
'public or private studies') | ||
|
||
def test_get_studies_private(self): | ||
obs = self.get('/qiita_db/studies/private', headers=self.header) | ||
exp = {'data': {'1': [4, 5, 6, 7]}} | ||
self.assertEqual(loads(obs.body), exp) | ||
|
||
def test_get_studies_public(self): | ||
obs = self.get('/qiita_db/studies/public', headers=self.header) | ||
exp = {'data': {}} | ||
self.assertEqual(loads(obs.body), exp) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
# The full license is in the file LICENSE, distributed with this software. | ||
# ----------------------------------------------------------------------------- | ||
from collections import defaultdict | ||
import io | ||
from qiita_db.metadata_template.util import load_template_to_dataframe | ||
|
||
from tornado.escape import json_encode, json_decode | ||
import pandas as pd | ||
|
@@ -150,13 +152,20 @@ def patch(self, study_id): | |
else: | ||
sample_info = study.sample_template.to_dataframe() | ||
|
||
data = pd.DataFrame.from_dict(json_decode(self.request.body), | ||
orient='index') | ||
|
||
if len(data.index) == 0: | ||
# convert from json into a format that qiita can validate | ||
rawdata = pd.DataFrame.from_dict(json_decode(self.request.body), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the load_template_to_dataframe accepts string so perhaps worth not converting to dataframe and then back and just do: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code looks like it opens a filehandle, so I don't think a regular string would work. The spec for the REST endpoint as well is not a str representation of a TSV, but a JSON representation derived from |
||
orient='index') | ||
rawdata.index.name = 'sample_name' | ||
if len(rawdata.index) == 0: | ||
self.fail('No samples provided', 400) | ||
return | ||
|
||
buffer = io.StringIO() | ||
wasade marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rawdata.to_csv(buffer, sep='\t', index=True, header=True) | ||
buffer.seek(0) | ||
# validate on load | ||
data = load_template_to_dataframe(buffer) | ||
|
||
categories = set(study.sample_template.categories) | ||
|
||
if set(data.columns) != categories: | ||
|
Uh oh!
There was an error while loading. Please reload this page.