datavorous commited on
Commit
d5d1eec
·
verified ·
1 Parent(s): cf11cba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -15
app.py CHANGED
@@ -325,24 +325,71 @@ async def generate_test(
325
 
326
  return test
327
 
328
- @app.get("/dpps/{dpp_name}", summary="Get all questions from a specific DPP", tags=["DPPs"])
 
 
 
 
329
  @cached(ttl=300, cache=SimpleMemoryCache, serializer=JsonSerializer())
330
  async def get_dpp_questions(
331
- dpp_name: str = Path(..., description="Name of the DPP file without .db extension")
 
 
332
  ):
333
- db_path = f"dpps/{dpp_name}.db"
334
- if not os.path.exists(db_path):
335
- raise HTTPException(status_code=404, detail="DPP file not found.")
336
-
337
- # Connect to the database asynchronously
338
- async with aiosqlite.connect(db_path) as db:
339
- cursor = await db.execute("SELECT * FROM selected_questions")
340
- questions = await cursor.fetchall()
341
-
342
- if not questions:
343
- raise HTTPException(status_code=404, detail="No questions found in the DPP.")
344
-
345
- return {"dpp": dpp_name, "questions": [dict(q) for q in questions]}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
346
 
347
  # Main application runner
348
  if __name__ == "__main__":
 
325
 
326
  return test
327
 
328
+ @app.get("/dpps/{dpp_name}",
329
+ summary="Get all questions from a specific DPP",
330
+ tags=["DPPs"],
331
+ response_description="List of questions from the specified DPP."
332
+ )
333
  @cached(ttl=300, cache=SimpleMemoryCache, serializer=JsonSerializer())
334
  async def get_dpp_questions(
335
+ dpp_name: str = Path(..., description="Name of the DPP file without .db extension"),
336
+ offset: int = Query(0, ge=0, description="Offset for pagination"),
337
+ limit: int = Query(1000, ge=1, le=1000, description="Limit for pagination")
338
  ):
339
+ """
340
+ Get questions from a specific DPP file with pagination support.
341
+
342
+ Args:
343
+ dpp_name: Name of the DPP file (without .db extension)
344
+ offset: Number of questions to skip
345
+ limit: Maximum number of questions to return
346
+
347
+ Returns:
348
+ Dictionary containing DPP name and list of questions
349
+ """
350
+ db_path = Path("dpps") / f"{dpp_name}.db"
351
+
352
+ if not db_path.exists():
353
+ raise HTTPException(
354
+ status_code=404,
355
+ detail=f"DPP file '{dpp_name}' not found"
356
+ )
357
+
358
+ try:
359
+ async with aiosqlite.connect(str(db_path)) as db:
360
+ db.row_factory = aiosqlite.Row
361
+
362
+ # First get total count
363
+ async with db.execute("SELECT COUNT(*) as count FROM selected_questions") as cursor:
364
+ total_count = (await cursor.fetchone())['count']
365
+
366
+ # Then get paginated questions
367
+ query = "SELECT * FROM selected_questions LIMIT ? OFFSET ?"
368
+ async with db.execute(query, [limit, offset]) as cursor:
369
+ questions = await cursor.fetchall()
370
+
371
+ if not questions:
372
+ raise HTTPException(
373
+ status_code=404,
374
+ detail=f"No questions found in DPP '{dpp_name}' for the specified page"
375
+ )
376
+
377
+ return {
378
+ "dpp_name": dpp_name,
379
+ "total_questions": total_count,
380
+ "questions": [dict(q) for q in questions],
381
+ "pagination": {
382
+ "offset": offset,
383
+ "limit": limit,
384
+ "total": total_count
385
+ }
386
+ }
387
+
388
+ except aiosqlite.Error as e:
389
+ raise HTTPException(
390
+ status_code=500,
391
+ detail=f"Database error occurred while accessing DPP '{dpp_name}': {str(e)}"
392
+ )
393
 
394
  # Main application runner
395
  if __name__ == "__main__":