Spaces:
Running on T4
Running on T4
Download backend/app/modules/notifications/repository.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 1.8 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/notifications/repository.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/app/modules/notifications/repository.py
-
curl -L -o repository.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/app/modules/notifications/repository.py
1.8 kB
| from datetime import datetime | |
| from uuid import UUID | |
| from sqlalchemy import func, select, tuple_, update | |
| from app.common.base_repository import BaseRepository | |
| from app.core.pagination import Cursor | |
| from app.modules.notifications.models import Notification | |
| class NotificationRepository(BaseRepository[Notification]): | |
| model = Notification | |
| async def page(self, recipient_id: UUID, cursor: Cursor | None, limit: int) -> list[Notification]: | |
| query = select(Notification).where(Notification.recipient_id == recipient_id) | |
| if cursor: | |
| query = query.where(tuple_(Notification.created_at, Notification.id) < (cursor.created_at, cursor.id)) | |
| result = await self.session.scalars( | |
| query.order_by(Notification.created_at.desc(), Notification.id.desc()).limit(limit)) | |
| return list(result.all()) | |
| async def unread_count(self, recipient_id: UUID) -> int: | |
| return await self.session.scalar(select(func.count()).select_from(Notification).where( | |
| Notification.recipient_id == recipient_id, Notification.read_at.is_(None))) or 0 | |
| async def mark_read(self, recipient_id: UUID, id_: UUID, now: datetime) -> bool: | |
| """False when the notification does not exist or belongs to someone else.""" | |
| row = await self.first_by(id=id_, recipient_id=recipient_id) | |
| if row is None: | |
| return False | |
| if row.read_at is None: | |
| await self.update(row, read_at=now) | |
| return True | |
| async def mark_read_through(self, recipient_id: UUID, through: datetime, now: datetime) -> None: | |
| await self.session.execute(update(Notification).where( | |
| Notification.recipient_id == recipient_id, Notification.read_at.is_(None), | |
| Notification.created_at <= through).values(read_at=now)) | |