Get practice cards for a session, optionally filtering, ordering, and limiting.
(
session: Session,
practice_session_id: uuid.UUID,
status: Literal["pending", "completed", "all"] | None = None,
limit: int | None = None,
order: Literal["asc", "desc", "random"] | None = None,
)
| 280 | |
| 281 | |
| 282 | def get_practice_cards( |
| 283 | session: Session, |
| 284 | practice_session_id: uuid.UUID, |
| 285 | status: Literal["pending", "completed", "all"] | None = None, |
| 286 | limit: int | None = None, |
| 287 | order: Literal["asc", "desc", "random"] | None = None, |
| 288 | ) -> tuple[list[PracticeCard], int]: |
| 289 | """Get practice cards for a session, optionally filtering, ordering, and limiting.""" |
| 290 | base_statement = select(PracticeCard).where( |
| 291 | PracticeCard.session_id == practice_session_id |
| 292 | ) |
| 293 | |
| 294 | if status == "pending": |
| 295 | statement = base_statement.where(PracticeCard.is_practiced.is_not(True)) |
| 296 | elif status == "completed": |
| 297 | statement = base_statement.where(PracticeCard.is_practiced.is_(True)) |
| 298 | else: |
| 299 | statement = base_statement |
| 300 | |
| 301 | count_statement = select(func.count()).select_from(statement.subquery()) |
| 302 | count = session.exec(count_statement).one() |
| 303 | |
| 304 | if order == "asc": |
| 305 | statement = statement.order_by(PracticeCard.created_at.asc()) |
| 306 | elif order == "desc": |
| 307 | statement = statement.order_by(PracticeCard.created_at.desc()) |
| 308 | elif order != "random": |
| 309 | if status == "pending": |
| 310 | statement = statement.order_by(PracticeCard.created_at) |
| 311 | else: |
| 312 | statement = statement.order_by(PracticeCard.updated_at.desc()) |
| 313 | |
| 314 | if order == "random": |
| 315 | practice_cards = session.exec(statement).all() |
| 316 | random.shuffle(practice_cards) |
| 317 | |
| 318 | if limit is not None: |
| 319 | practice_cards = practice_cards[:limit] |
| 320 | else: |
| 321 | if limit is not None: |
| 322 | statement = statement.limit(limit) |
| 323 | practice_cards = session.exec(statement).all() |
| 324 | |
| 325 | return practice_cards, count |
| 326 | |
| 327 | |
| 328 | def get_practice_card( |
no outgoing calls