90% сеньоров плачат на таких коммитах

This commit is contained in:
Artem Tsyrulnikov
2025-12-15 19:10:41 +03:00
parent fda91cb6d8
commit 71e5a38efc
42 changed files with 403 additions and 302 deletions

View File

@@ -177,22 +177,17 @@ class Postgres(DatabaseBase):
async def get_channel(
self,
workspace_id: uuid.UUID,
channel_id: uuid.UUID | None = None,
telegram_id: int | None = None,
username: str | None = None,
) -> domain.Channel | None:
query = domain.Channel.filter(workspace_id=workspace_id)
if channel_id:
query = query.filter(id=channel_id)
return await domain.Channel.get_or_none(id=channel_id)
if telegram_id:
query = query.filter(telegram_id=telegram_id)
return await domain.Channel.get_or_none(telegram_id=telegram_id)
if username:
query = query.filter(username=username)
return await query.first()
async def get_workspace_channels(self, workspace_id: uuid.UUID) -> list[domain.Channel]:
return await domain.Channel.filter(workspace_id=workspace_id).all()
return await domain.Channel.filter(username__iexact=username).first()
return None
async def create_channel(self, channel: domain.Channel) -> None:
await channel.save()
@@ -345,7 +340,7 @@ class Postgres(DatabaseBase):
async def get_placement(self, workspace_id: uuid.UUID, placement_id: uuid.UUID) -> domain.Placement | None:
return await domain.Placement.get_or_none(id=placement_id, workspace_id=workspace_id).prefetch_related(
'project', 'project__channel', 'placement_channel', 'creative'
'project', 'project__channel', 'placement_channel', 'creative', 'post', 'post__channel'
)
async def create_placement(self, placement: domain.Placement) -> None:
@@ -380,7 +375,9 @@ class Postgres(DatabaseBase):
query = query.filter(status=domain.PlacementStatus.ACTIVE)
return (
await query.prefetch_related('project', 'project__channel', 'placement_channel', 'creative')
await query.prefetch_related(
'project', 'project__channel', 'placement_channel', 'creative', 'post', 'post__channel'
)
.order_by('-placement_date')
.all()
)
@@ -430,17 +427,28 @@ class Postgres(DatabaseBase):
.first()
)
async def create_placement_views_history(self, history: domain.PlacementViewsHistory) -> None:
# Post methods
async def create_post(self, post: domain.Post) -> None:
await post.save()
async def get_post(self, post_id: uuid.UUID) -> domain.Post | None:
return await domain.Post.get_or_none(id=post_id).prefetch_related('channel')
async def get_post_by_channel_and_message(self, channel_id: uuid.UUID, message_id: int) -> domain.Post | None:
return await domain.Post.get_or_none(channel_id=channel_id, message_id=message_id).prefetch_related('channel')
# Post views history
async def create_post_views_history(self, history: domain.PostViewsHistory) -> None:
await history.save()
async def get_views_history(
self,
placement_id: uuid.UUID,
post_id: uuid.UUID,
*,
from_date: datetime.datetime | None = None,
to_date: datetime.datetime | None = None,
) -> list[domain.PlacementViewsHistory]:
query = domain.PlacementViewsHistory.filter(placement_id=placement_id)
) -> list[domain.PostViewsHistory]:
query = domain.PostViewsHistory.filter(post_id=post_id)
if from_date:
query = query.filter(fetched_at__gte=from_date)
@@ -449,6 +457,26 @@ class Postgres(DatabaseBase):
return await query.order_by('fetched_at').all()
async def get_latest_views_data(self, post_id: uuid.UUID) -> tuple[int, datetime.datetime] | None:
latest = await domain.PostViewsHistory.filter(post_id=post_id).order_by('-fetched_at').first()
if latest:
return (latest.views_count, latest.fetched_at)
return None
async def get_latest_views_data_batch(
self, post_ids: list[uuid.UUID]
) -> dict[uuid.UUID, tuple[int, datetime.datetime]]:
if not post_ids:
return {}
results: dict[uuid.UUID, tuple[int, datetime.datetime]] = {}
for post_id in post_ids:
latest = await domain.PostViewsHistory.filter(post_id=post_id).order_by('-fetched_at').first()
if latest:
results[post_id] = (latest.views_count, latest.fetched_at)
return results
async def get_telegram_state(self, telegram_id: int) -> domain.TelegramState | None:
return await domain.TelegramState.get_or_none(telegram_id=telegram_id)