From 7b86c84f7757be3997da6c6caac09a6299afe24d Mon Sep 17 00:00:00 2001 From: Cannon Lock Date: Thu, 9 May 2024 12:09:01 -0500 Subject: [PATCH] Add more control over grouping --- api/database.py | 7 +------ api/query_parser.py | 16 +++++++++++++--- api/routes/ingest.py | 1 + 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/api/database.py b/api/database.py index e56812a..1cc8ce7 100644 --- a/api/database.py +++ b/api/database.py @@ -243,6 +243,7 @@ async def select_sources_sub_table( stmt = ( select(*selected_columns) + .order_by(*query_parser.get_order_by_columns()) .limit(page_size) .offset(page_size * page) .where(query_parser.where_expressions()) @@ -253,12 +254,6 @@ async def select_sources_sub_table( query_parser.get_group_by_column() ) - if ( - query_parser.get_order_by_columns() is not None - and query_parser.get_group_by_column() is None - ): - stmt = stmt.order_by(*query_parser.get_order_by_columns()) - x = str(stmt.compile(compile_kwargs={ "literal_binds": True })) diff --git a/api/query_parser.py b/api/query_parser.py index 5c72184..f83d0a2 100644 --- a/api/query_parser.py +++ b/api/query_parser.py @@ -29,7 +29,7 @@ class ParserException(Exception): def get_filter_query_params(request: Request) -> list[tuple[str, str]]: """Returns the query params that are not page or page_size""" - return [*filter(lambda x: x[0] not in ["page", "page_size"], request.query_params.items())] + return [*filter(lambda x: x[0] not in ["page", "page_size"], request.query_params.multi_items())] def cast_to_column_type(column: Column, value): @@ -224,7 +224,17 @@ def get_order_by_columns(self): continue if query_param.operators[0] == "order_by": - order_by_columns.append(query_param.column) + # Check if the order_by is valid + if query_param.value not in ["asc", "desc"]: + raise HTTPException( + status_code=400, + detail=f"Query is invalid. Use asc or desc for order_by" + ) + + if query_param.value == "asc": + order_by_columns.append(query_param.column.asc()) + else: + order_by_columns.append(query_param.column.desc()) return order_by_columns @@ -257,7 +267,7 @@ def _decompose_encoded_expression(self, encoded_expression) -> tuple: # If group_by or order_by, then there is no value if len(encoded_expression_split) == 1: - if encoded_expression_split[0] not in ["group_by", "order_by"]: + if encoded_expression_split[0] not in ["group_by"]: raise ParserException(f"Query is invalid.") return encoded_expression_split[:1], "" diff --git a/api/routes/ingest.py b/api/routes/ingest.py index 80d5879..ccd3a63 100644 --- a/api/routes/ingest.py +++ b/api/routes/ingest.py @@ -46,6 +46,7 @@ async def get_multiple_ingest_process(page: int = 0, page_size: int = 50, .limit(page_size) \ .offset(page_size * page) \ .where(and_(query_parser.where_expressions())) \ + .order_by(*query_parser.get_order_by_columns()) \ .options(joinedload(IngestProcessSchema.source).defer(Sources.rgeom).defer(Sources.web_geom)) \ .options(selectinload(IngestProcessSchema.tags))