@@ -64,6 +64,11 @@ def parse_query_params(
6464 None , description = "Maximum number of records to return"
6565 ),
6666 offset : Optional [int ] = Query (0 , description = "Number of records to skip" ),
67+ ordering : Optional [str ] = Query (
68+ None ,
69+ description = "Column to sort by. Prefix with '-' for descending order. "
70+ "Allowed values: accession, amino_acids, organism, curation_status, predicted_ec" ,
71+ ),
6772) -> CLEANSearchQueryParams :
6873 """Parse and validate query parameters."""
6974 try :
@@ -85,6 +90,7 @@ def parse_query_params(
8590 format = format ,
8691 limit = limit ,
8792 offset = offset ,
93+ ordering = ordering ,
8894 )
8995 except Exception as e :
9096 logger .error (f"Error parsing query parameters: { e } " )
@@ -104,19 +110,14 @@ async def get_data(
104110 """
105111
106112 try :
107- # # Get total count for the query (without pagination)
108- # total_count = await get_total_count(db, params)
109-
110- # # Apply automatic pagination if results exceed threshold and no explicit limit provided
111- # if total_count > settings.AUTO_PAGINATION_THRESHOLD and params.limit is None:
112- # params.auto_paginated = True
113- # params.limit = settings.AUTO_PAGINATION_THRESHOLD
114- # logger.info(
115- # f"Auto-pagination applied. Results limited to {params.limit} records."
116- # )
117-
118- params .limit = 500
119- # Get data from database (now with potential auto-pagination applied)
113+ # Apply default page size if no explicit limit provided
114+ if params .limit is None :
115+ params .limit = settings .AUTO_PAGINATION_THRESHOLD
116+
117+ # Get total count for the query (without pagination)
118+ total_count = await get_total_count (db , params )
119+
120+ # Get data from database
120121 data = await get_filtered_data (db , params )
121122
122123 # Handle response format
@@ -148,12 +149,10 @@ async def get_data(
148149 headers = {"Content-Disposition" : "attachment; filename=CLEAN_data.csv" },
149150 )
150151 else :
151- # TODO don't we want total_count to be the value returned by get_total_count?
152- total_count = len (data )
153152 response = CLEANSearchResponse (
154153 total = total_count ,
155154 offset = params .offset ,
156- limit = total_count if total_count < params . limit else params .limit ,
155+ limit = params .limit ,
157156 data = [CLEANDataBase (
158157 predictions_uniprot_annot_id = record ["predictions_uniprot_annot_id" ],
159158 uniprot = record ["uniprot_id" ],
@@ -177,54 +176,48 @@ async def get_data(
177176 ) for record in data ],
178177 )
179178
180- # Add pagination links if automatic pagination was applied
181- if params .auto_paginated :
182- # Add flag indicating automatic pagination was applied
183- response .auto_paginated = True
184-
185- if request :
186- base_url = str (request .url ).split ("?" )[0 ]
187-
188- # Prepare query parameters for pagination links
189- # For Pydantic v2 compatibility
190- query_params = {
191- k : v
192- for k , v in params .model_dump ().items ()
193- if k not in ["auto_paginated" , "offset" , "limit" ]
194- and v is not None
179+ # Add pagination links
180+ if request :
181+ base_url = str (request .url ).split ("?" )[0 ]
182+
183+ # Prepare query parameters for pagination links
184+ query_params = {
185+ k : v
186+ for k , v in params .model_dump ().items ()
187+ if k not in ["auto_paginated" , "offset" , "limit" ]
188+ and v is not None
189+ }
190+
191+ # Set format explicitly if it was provided
192+ if params .format != ResponseFormat .JSON :
193+ query_params ["format" ] = params .format
194+
195+ current_offset = params .offset or 0
196+ current_limit = params .limit
197+
198+ # Next page link if there are more records
199+ if current_offset + current_limit < total_count :
200+ next_offset = current_offset + current_limit
201+ next_params = {
202+ ** query_params ,
203+ "offset" : next_offset ,
204+ "limit" : current_limit ,
195205 }
196-
197- # Set format explicitly if it was provided
198- if params .format != ResponseFormat .JSON :
199- query_params ["format" ] = params .format
200-
201- # Calculate next page link if there are more records
202- current_offset = params .offset or 0
203- current_limit = params .limit or total_count
204- if current_offset + current_limit < total_count :
205- next_offset = current_offset + current_limit
206- next_params = {
207- ** query_params ,
208- "offset" : next_offset ,
209- "limit" : current_limit ,
210- }
211- response .next = (
212- f"{ base_url } ?{ urlencode (next_params , doseq = True )} "
213- )
214-
215- # Calculate previous page link if not on first page
216- current_offset = params .offset or 0
217- current_limit = params .limit or total_count
218- if current_offset > 0 :
219- prev_offset = max (0 , current_offset - current_limit )
220- prev_params = {
221- ** query_params ,
222- "offset" : prev_offset ,
223- "limit" : current_limit ,
224- }
225- response .previous = (
226- f"{ base_url } ?{ urlencode (prev_params , doseq = True )} "
227- )
206+ response .next = (
207+ f"{ base_url } ?{ urlencode (next_params , doseq = True )} "
208+ )
209+
210+ # Previous page link if not on first page
211+ if current_offset > 0 :
212+ prev_offset = max (0 , current_offset - current_limit )
213+ prev_params = {
214+ ** query_params ,
215+ "offset" : prev_offset ,
216+ "limit" : current_limit ,
217+ }
218+ response .previous = (
219+ f"{ base_url } ?{ urlencode (prev_params , doseq = True )} "
220+ )
228221
229222 return response
230223
0 commit comments