|
1 | 1 | import contextlib |
| 2 | +import logging |
2 | 3 | from typing import Any |
3 | 4 |
|
4 | 5 | import sqlalchemy as sa |
|
48 | 49 | UserNotFoundError, |
49 | 50 | ) |
50 | 51 |
|
| 52 | +_logger = logging.getLogger(__name__) |
| 53 | + |
51 | 54 |
|
52 | 55 | def _parse_as_user(user_id: Any) -> UserID: |
53 | 56 | try: |
@@ -381,45 +384,33 @@ async def list_users_for_admin( |
381 | 384 | engine: AsyncEngine, |
382 | 385 | connection: AsyncConnection | None = None, |
383 | 386 | *, |
384 | | - filter_approved: bool | None = None, |
385 | | - limit: int = 50, |
386 | | - offset: int = 0, |
387 | | - include_deleted: bool = False, |
| 387 | + product_name: ProductName, |
| 388 | + filter_account_request_status: AccountRequestStatus | None = None, |
| 389 | + filter_include_deleted: bool = False, |
| 390 | + pagination_limit: int = 50, |
| 391 | + pagination_offset: int = 0, |
388 | 392 | ) -> tuple[list[dict[str, Any]], int]: |
389 | 393 | """ |
390 | 394 | Gets users data for admin with pagination support using SQLAlchemy expressions |
391 | 395 |
|
392 | | - Args: |
393 | | - engine: The database engine |
394 | | - connection: Optional existing connection to reuse |
395 | | - filter_approved: If set, filters users by their approval status |
396 | | - limit: Maximum number of users to return |
397 | | - offset: Number of users to skip for pagination |
398 | | - include_deleted: Whether to include users marked as deleted |
399 | | -
|
400 | 396 | Returns: |
401 | 397 | Tuple of (list of user data, total count) |
402 | 398 | """ |
403 | 399 | joined_user_tables = users.outerjoin( |
404 | 400 | users_pre_registration_details, |
405 | | - users.c.id == users_pre_registration_details.c.user_id, |
| 401 | + (users.c.id == users_pre_registration_details.c.user_id) |
| 402 | + & (users_pre_registration_details.c.product_name == product_name), |
406 | 403 | ) |
407 | 404 |
|
408 | 405 | where_conditions = [] |
409 | | - if not include_deleted: |
| 406 | + if not filter_include_deleted: |
410 | 407 | where_conditions.append(users.c.status != UserStatus.DELETED) |
411 | 408 |
|
412 | | - if filter_approved is not None: |
413 | | - if filter_approved: |
414 | | - where_conditions.append( |
415 | | - users_pre_registration_details.c.account_request_status |
416 | | - == AccountRequestStatus.APPROVED |
417 | | - ) |
418 | | - else: |
419 | | - where_conditions.append( |
420 | | - users_pre_registration_details.c.account_request_status |
421 | | - != AccountRequestStatus.APPROVED |
422 | | - ) |
| 409 | + if filter_account_request_status is not None: |
| 410 | + where_conditions.append( |
| 411 | + users_pre_registration_details.c.account_request_status |
| 412 | + == filter_account_request_status |
| 413 | + ) |
423 | 414 |
|
424 | 415 | where_clause = sa.and_(*where_conditions) if where_conditions else sa.true() |
425 | 416 |
|
@@ -478,18 +469,17 @@ async def list_users_for_admin( |
478 | 469 | users_pre_registration_details.c.created.desc(), # newest pre-registered first |
479 | 470 | users_pre_registration_details.c.pre_email, |
480 | 471 | ) |
481 | | - .limit(limit) |
482 | | - .offset(offset) |
| 472 | + .limit(pagination_limit) |
| 473 | + .offset(pagination_offset) |
483 | 474 | ) |
484 | 475 |
|
485 | | - print( |
| 476 | + _logger.debug( |
| 477 | + "%s\n%s\n%s\n%s", |
486 | 478 | "-" * 100, |
487 | | - "\n", |
488 | 479 | as_postgres_sql_query_str(main_query), |
489 | 480 | "-" * 100, |
490 | | - "\n", |
491 | 481 | as_postgres_sql_query_str(count_query), |
492 | | - ) # DEBUG |
| 482 | + ) |
493 | 483 |
|
494 | 484 | async with pass_or_acquire_connection(engine, connection) as conn: |
495 | 485 | # Get total count |
|
0 commit comments