Skip to content

Commit 58520ac

Browse files
authored
[REF] refactor marshmallow schema logic (#681)
* wip: refactoring of schema * tmp commit neurovault away * style fix * import style fixes * clean comments * fix info endpoint * remove comment
1 parent fda44fe commit 58520ac

File tree

8 files changed

+186
-233
lines changed

8 files changed

+186
-233
lines changed

store/neurostore/resources/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,12 @@ def post(self):
532532
if source_id:
533533
data = self._load_from_source(source, source_id)
534534
else:
535-
data = parser.parse(self.__class__._schema, request)
535+
unknown = self.__class__._schema.opts.unknown
536+
data = parser.parse(
537+
self.__class__._schema(exclude=("id",)), request, unknown=unknown
538+
)
536539
args["nested"] = bool(args.get("nested") or request.args.get("source_id"))
540+
537541
with db.session.no_autoflush:
538542
record = self.__class__.update_or_create(data)
539543

store/neurostore/resources/data.py

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import string
22

33
from flask import request
4-
from marshmallow import EXCLUDE
54
from webargs.flaskparser import parser
65
from webargs import fields
76
import sqlalchemy.sql.expression as sae
@@ -207,14 +206,12 @@ def load_from_neurostore(cls, source_id):
207206
parent_source = parent.source
208207
parent_source_id = parent.source_id
209208

210-
schema = cls._schema(copy=True)
209+
context = {
210+
"clone": True,
211+
"nested": True,
212+
}
213+
schema = cls._schema(context=context)
211214
tmp_data = schema.dump(annotation)
212-
for note in tmp_data["notes"]:
213-
note.pop("analysis_name")
214-
note.pop("study_name")
215-
note.pop("study_year")
216-
note.pop("publication")
217-
note.pop("authors")
218215
data = schema.load(tmp_data)
219216
data["source"] = "neurostore"
220217
data["source_id"] = source_id
@@ -266,12 +263,6 @@ def view_search(self, q, args):
266263

267264
return q
268265

269-
def serialize_records(self, records, args, exclude=tuple()):
270-
if args.get("flat"):
271-
exclude = ("versions",)
272-
273-
return super().serialize_records(records, args, exclude)
274-
275266
def join_tables(self, q):
276267
"join relevant tables to speed up query"
277268
q = q.options(joinedload("versions"))
@@ -382,8 +373,6 @@ class StudiesView(ObjectView, ListView):
382373
"pmid",
383374
)
384375

385-
# _default_exclude = ("has_coordinates", "has_images", "studysets")
386-
387376
def view_search(self, q, args):
388377
# search studies for data_type
389378
if args.get("data_type"):
@@ -426,10 +415,6 @@ def serialize_records(self, records, args, exclude=tuple()):
426415
study.studysets = study.studysets.filter(
427416
Studyset.user_id == args.get("studyset_owner")
428417
).all()
429-
if args.get("flat"):
430-
exclude += ("analyses",)
431-
432-
exclude += ("studysets", "has_coordinates", "has_images")
433418
return super().serialize_records(records, args, exclude)
434419

435420
@classmethod
@@ -452,8 +437,10 @@ def load_from_neurostore(cls, source_id):
452437
parent_source = parent.source
453438
parent_source_id = parent.source_id
454439

455-
schema = cls._schema(copy=True)
456-
data = schema.load(schema.dump(study), unknown=EXCLUDE)
440+
context = {"clone": True, "nested": True}
441+
schema = cls._schema(context=context)
442+
dump_study = schema.dump(study)
443+
data = schema.load(dump_study)
457444
data["source"] = "neurostore"
458445
data["source_id"] = source_id
459446
data["source_updated_at"] = study.updated_at or study.created_at

0 commit comments

Comments
 (0)