Skip to content

Commit

Permalink
refactor how records are looked up
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkent committed Nov 20, 2023
1 parent 9cf5366 commit 864c14d
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions store/neurostore/resources/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,30 +213,31 @@ def post(self):
# in the list scenerio, try to find an existing record
# then return the best version and return that study id
data = parser.parse(self.__class__._schema(many=True), request)
search_keys = ["pmid", "doi", "name"]
base_studies = []
to_commit = []
for study_data in data:
filter_params = {
k: study_data.get(k) for k in search_keys if study_data.get(k)
}
if "name" in filter_params and (set(filter_params) - {"name"}) != set():
del filter_params["name"]

record = (
BaseStudy.query.filter_by(**filter_params)
.options(
joinedload(BaseStudy.versions).options(
pmids = [sd["pmid"] for sd in data if sd.get("pmid")]
dois = [sd["doi"] for sd in data if sd.get("doi")]
names = [sd["name"] for sd in data if sd.get("name")]
results = (
BaseStudy.query.filter(
(BaseStudy.doi.in_(dois)) |
(BaseStudy.pmid.in_(pmids)) |
(BaseStudy.name.in_(names))
).options(
joinedload(BaseStudy.versions).options(
joinedload(Study.studyset_studies).joinedload(
StudysetStudy.studyset
),
joinedload(Study.user),
),
joinedload(BaseStudy.user),
)
.one_or_none()
)

.all()
)
hashed_results = {(bs.doi or '') + (bs.pmid or ''): bs for bs in results}
for study_data in data:
lookup_hash = study_data.get("doi", "") + study_data.get("pmid", "")
record = hashed_results.get(lookup_hash)
if record is None:
with db.session.no_autoflush:
record = self.__class__.update_or_create(study_data, commit=False)
Expand Down

0 comments on commit 864c14d

Please sign in to comment.