Skip to content

Commit 16908b1

Browse files
authored
[FIX] ignore string if doi/pmid/name string is empty (#598)
* do not create new base study if string is empty * update openapi
1 parent 454ea54 commit 16908b1

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

store/neurostore/resources/data.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
import string
2+
13
from flask import request
24
from marshmallow import EXCLUDE
35
from webargs.flaskparser import parser
46
from webargs import fields
57
import sqlalchemy.sql.expression as sae
68
from sqlalchemy.orm import joinedload
9+
from sqlalchemy.sql import func
10+
711

812
from .utils import view_maker
913
from .base import BaseView, ObjectView, ListView
@@ -216,7 +220,7 @@ def post(self):
216220
filter_params = {
217221
k: study_data.get(k)
218222
for k in search_keys
219-
if study_data.get(k) is not None
223+
if study_data.get(k)
220224
}
221225
if "name" in filter_params and (set(filter_params) - {"name"}) != set():
222226
del filter_params["name"]
@@ -393,28 +397,34 @@ def pre_nested_record_update(record):
393397
return record
394398

395399
query = BaseStudy.query
396-
has_doi = has_pmid = False
400+
has_doi = has_pmid = has_name = False
397401
base_study = None
398402
if record.doi:
399403
query = query.filter_by(doi=record.doi)
400404
has_doi = True
401405
if record.pmid:
402406
query = query.filter_by(pmid=record.pmid)
403407
has_pmid = True
408+
if record.name and not record.doi and not record.pmid:
409+
name_search = func.regexp_replace(
410+
record.name, r'[' + string.punctuation + ']', '', 'g'
411+
)
412+
query = query.filter(BaseStudy.name.ilike(f"%{name_search}%"))
413+
has_name = True
404414

405-
if query.count() >= 1 and (has_doi or has_pmid):
415+
if query.count() >= 1 and (has_doi or has_pmid or has_name):
406416
base_study = query.first()
407417
elif has_doi or has_pmid:
408418
base_study = BaseStudy(
409419
name=record.name,
410-
doi=record.doi,
420+
doi=record.doi if record.doi else None,
411421
pmid=record.pmid,
412422
description=record.description,
413423
publication=record.publication,
414424
year=record.year,
415-
level=record.level,
416425
authors=record.authors,
417426
metadata_=record.metadata_,
427+
level=record.level if record.level else "group",
418428
)
419429
else:
420430
# there is no published study to associate

store/neurostore/tests/api/test_base_studies.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ def test_post_list_of_studies(auth_client, ingest_neuroquery):
1515
"doi": "new_doi",
1616
"name": "new name",
1717
},
18+
{
19+
"pmid": base_studies[1].pmid,
20+
"doi": "",
21+
},
22+
{
23+
"name": "another new name",
24+
},
1825
]
1926

2027
result = auth_client.post("/api/base-studies/", data=test_input)

0 commit comments

Comments
 (0)