Skip to content

Commit 7f2ed5c

Browse files
authored
[ENH] project info (#683)
* change to project_info branch * add info to project endpoint * update openapi * style fix
1 parent a45c051 commit 7f2ed5c

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

compose/neurosynth_compose/resources/analysis.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,7 @@ def get(self, id):
224224
record = self._model.query.filter_by(id=id).first_or_404()
225225
args = parser.parse(self._user_args, request, location="query")
226226

227-
return self.__class__._schema(context={"nested": args.get("nested")}).dump(
228-
record
229-
)
227+
return self.__class__._schema(context=args).dump(record)
230228

231229
def put(self, id):
232230
id = id.replace("\x00", "\uFFFD")
@@ -279,6 +277,7 @@ def insert_data(self, id, data):
279277
"dataset_id": fields.String(missing=None),
280278
"export": fields.Boolean(missing=False),
281279
"data_type": fields.String(missing=None),
280+
"info": fields.Boolean(missing=False),
282281
}
283282

284283

@@ -352,10 +351,10 @@ def search(self):
352351
records = q.paginate(
353352
page=args["page"], per_page=args["page_size"], error_out=False
354353
).items
355-
# check if results should be nested
356-
nested = True if args.get("nested") else False
357354
content = self.__class__._schema(
358-
only=self._only, many=True, context={"nested": nested}
355+
only=self._only,
356+
many=True,
357+
context=args,
359358
).dump(records)
360359
response = {
361360
"metadata": {},

compose/neurosynth_compose/schemas/analysis.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,21 @@ def _serialize(self, value, attr, obj, **kwargs):
6767
if self.metadata.get("nested", None) is False
6868
else self.context.get("nested")
6969
)
70+
info = self.context.get("info")
71+
if info:
72+
schema = self.schema
73+
info_fields = [
74+
field
75+
for field, f_obj in schema._declared_fields.items()
76+
if f_obj.metadata.get("info_field")
77+
]
78+
schema.only = schema.set_class(info_fields)
79+
# set exclude to an empty set
80+
schema.exclude = schema.set_class()
81+
schema._init_fields()
82+
7083
nested_attr = self.metadata.get("pluck")
71-
if nested:
84+
if nested or info:
7285
many = self.schema.many or self.many
7386
nested_obj = getattr(obj, self.data_key or self.name)
7487
return self.schema.dump(nested_obj, many=many)
@@ -115,11 +128,13 @@ def _deserialize(self, value, attr, data, **kwargs):
115128

116129

117130
class BaseSchema(Schema):
118-
id = PGSQLString()
131+
id = PGSQLString(metadata={"info_field": True})
119132
created_at = fields.DateTime()
120133
updated_at = fields.DateTime(allow_none=True)
121134
user_id = fields.String(data_key="user")
122-
username = fields.String(attribute="user.name", dump_only=True)
135+
username = fields.String(
136+
attribute="user.name", dump_only=True, metadata={"info_field": True}
137+
)
123138

124139

125140
class ConditionSchema(Schema):
@@ -284,8 +299,8 @@ def process_data(self, data, **kwargs):
284299

285300

286301
class MetaAnalysisSchema(BaseSchema):
287-
name = fields.String(allow_none=True)
288-
description = fields.String(allow_none=True)
302+
name = fields.String(allow_none=True, metadata={"info_field": True})
303+
description = fields.String(allow_none=True, metadata={"info_field": True})
289304
provenance = fields.Dict(allow_none=True)
290305
specification_id = StringOrNested(SpecificationSchema, data_key="specification")
291306
neurostore_analysis = fields.Nested("NeurostoreAnalysisSchema", dump_only=True)
@@ -324,6 +339,8 @@ class MetaAnalysisSchema(BaseSchema):
324339

325340
@post_dump
326341
def create_neurostore_url(self, data, **kwargs):
342+
if self.context.get("info"):
343+
return data
327344
if data.get("neurostore_analysis", None) and data["neurostore_analysis"].get(
328345
"neurostore_id", None
329346
):
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from neurosynth_compose.models import Project
2+
from neurosynth_compose.schemas import MetaAnalysisSchema
3+
4+
5+
def test_project_info(session, app, auth_client, user_data):
6+
proj = Project.query.first()
7+
8+
info_resp = auth_client.get(f"/api/projects/{proj.id}?info=true")
9+
assert info_resp.status_code == 200
10+
11+
info_fields = [
12+
f
13+
for f, v in MetaAnalysisSchema._declared_fields.items()
14+
if v.metadata.get("info_field")
15+
]
16+
17+
meta_analysis = info_resp.json["meta_analyses"][0]
18+
19+
for f in info_fields:
20+
assert f in meta_analysis

0 commit comments

Comments
 (0)