Skip to content

Commit 03e7924

Browse files
committed
Move score set search limits into constants.
1 parent 40b656c commit 03e7924

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/mavedb/routers/score_sets.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878

7979
logger = logging.getLogger(__name__)
8080

81+
SCORE_SET_SEARCH_MAX_LIMIT = 100
82+
SCORE_SET_SEARCH_MAX_PUBLICATION_IDENTIFIERS = 40
83+
8184

8285
async def fetch_score_set_by_urn(
8386
db, urn: str, user: Optional[UserData], owner_or_contributor: Optional[UserData], only_published: bool
@@ -152,14 +155,21 @@ def search_score_sets(
152155
)
153156
search.published = True
154157

155-
# Require a limit of at most 100 when the search query does not include publication identifiers. We allow unlimited
156-
# searches with publication identifiers, presuming that such a search will not have excessive results.
157-
if search.publication_identifiers is None and (search.limit is None or search.limit > 100):
158-
search.limit = 100
158+
# Require a limit of at most SCORE_SET_SEARCH_MAX_LIMIT when the search query does not include publication
159+
# identifiers. We allow unlimited searches with publication identifiers, presuming that such a search will not have
160+
# excessive results.
161+
if search.publication_identifiers is None and (search.limit is None or search.limit > SCORE_SET_SEARCH_MAX_LIMIT):
162+
raise HTTPException(
163+
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
164+
detail=f"Cannot search for more than {SCORE_SET_SEARCH_MAX_LIMIT} score sets at a time. Please use the offset and limit parameters to run a paginated search.",
165+
)
159166

160-
# Also limit the search to at most 40 publication identifiers, to prevent artificially constructed searches that
161-
# return very large result sets.
162-
if search.publication_identifiers is not None and len(search.publication_identifiers) > 40:
167+
# Also limit the search to at most SCORE_SET_SEARCH_MAX_PUBLICATION_IDENTIFIERS publication identifiers, to prevent
168+
# artificially constructed searches that return very large result sets.
169+
if (
170+
search.publication_identifiers is not None
171+
and len(search.publication_identifiers) > SCORE_SET_SEARCH_MAX_PUBLICATION_IDENTIFIERS
172+
):
163173
raise HTTPException(
164174
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
165175
detail="Cannot search for score sets belonging to more than 40 publication identifiers at once.",

0 commit comments

Comments
 (0)