refactor
This commit is contained in:
@@ -392,9 +392,9 @@ class Postgres(DatabaseBase):
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_publication(workspace_id: uuid.UUID, publication_id: uuid.UUID) -> domain.Publication | None:
|
||||
return await domain.Publication.get_or_none(
|
||||
id=publication_id, placement__project__workspace_id=workspace_id
|
||||
async def get_placement_post(workspace_id: uuid.UUID, placement_post_id: uuid.UUID) -> domain.PlacementPost | None:
|
||||
return await domain.PlacementPost.get_or_none(
|
||||
id=placement_post_id, placement__project__workspace_id=workspace_id
|
||||
).prefetch_related(
|
||||
'placement',
|
||||
'placement__project',
|
||||
@@ -406,7 +406,7 @@ class Postgres(DatabaseBase):
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def get_workspace_publications(
|
||||
async def get_workspace_placement_posts(
|
||||
workspace_id: uuid.UUID,
|
||||
project_id: uuid.UUID | None = None,
|
||||
placement_channel_id: uuid.UUID | None = None,
|
||||
@@ -415,8 +415,8 @@ class Postgres(DatabaseBase):
|
||||
allowed_project_ids: set[uuid.UUID] | None = None,
|
||||
date_from: datetime.datetime | None = None,
|
||||
date_to: datetime.datetime | None = None,
|
||||
) -> list[domain.Publication]:
|
||||
query = domain.Publication.filter(placement__project__workspace_id=workspace_id)
|
||||
) -> list[domain.PlacementPost]:
|
||||
query = domain.PlacementPost.filter(placement__project__workspace_id=workspace_id)
|
||||
|
||||
if project_id:
|
||||
query = query.filter(placement__project_id=project_id)
|
||||
@@ -435,7 +435,7 @@ class Postgres(DatabaseBase):
|
||||
query = query.filter(created_at__lte=date_to)
|
||||
|
||||
if not include_archived:
|
||||
query = query.filter(status=domain.PublicationStatus.ACTIVE)
|
||||
query = query.filter(status=domain.PlacementPostStatus.ACTIVE)
|
||||
|
||||
return (
|
||||
await query.prefetch_related(
|
||||
@@ -452,12 +452,12 @@ class Postgres(DatabaseBase):
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def create_publication(publication: domain.Publication) -> None:
|
||||
await publication.save()
|
||||
async def create_placement_post(placement_post: domain.PlacementPost) -> None:
|
||||
await placement_post.save()
|
||||
|
||||
@staticmethod
|
||||
async def get_publication_by_invite_link(invite_link: str) -> domain.Publication | None:
|
||||
return await domain.Publication.get_or_none(placement__invite_link=invite_link).prefetch_related(
|
||||
async def get_placement_post_by_invite_link(invite_link: str) -> domain.PlacementPost | None:
|
||||
return await domain.PlacementPost.get_or_none(placement__invite_link=invite_link).prefetch_related(
|
||||
'placement',
|
||||
'placement__project',
|
||||
'placement__project__channel',
|
||||
@@ -471,26 +471,28 @@ class Postgres(DatabaseBase):
|
||||
await subscription.save()
|
||||
|
||||
@staticmethod
|
||||
async def get_subscription_by_subscriber_and_publication(
|
||||
telegram_user_id: uuid.UUID, publication_id: uuid.UUID
|
||||
async def get_subscription_by_subscriber_and_placement_post(
|
||||
telegram_user_id: uuid.UUID, placement_post_id: uuid.UUID
|
||||
) -> domain.Subscription | None:
|
||||
return await domain.Subscription.get_or_none(telegram_user_id=telegram_user_id, publication_id=publication_id)
|
||||
return await domain.Subscription.get_or_none(
|
||||
telegram_user_id=telegram_user_id, placement_post_id=placement_post_id
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
async def update_subscription(subscription: domain.Subscription) -> None:
|
||||
await subscription.save()
|
||||
|
||||
@staticmethod
|
||||
async def get_subscriptions_for_publications(
|
||||
publication_ids: list[uuid.UUID],
|
||||
async def get_subscriptions_for_placement_posts(
|
||||
placement_post_ids: list[uuid.UUID],
|
||||
*,
|
||||
date_from: datetime.datetime | None = None,
|
||||
date_to: datetime.datetime | None = None,
|
||||
) -> list[domain.Subscription]:
|
||||
if not publication_ids:
|
||||
if not placement_post_ids:
|
||||
return []
|
||||
|
||||
query = domain.Subscription.filter(publication_id__in=publication_ids)
|
||||
query = domain.Subscription.filter(placement_post_id__in=placement_post_ids)
|
||||
|
||||
if date_from:
|
||||
query = query.filter(created_at__gte=date_from)
|
||||
@@ -506,10 +508,10 @@ class Postgres(DatabaseBase):
|
||||
return (
|
||||
await domain.Subscription.filter(
|
||||
telegram_user_id=telegram_user_id,
|
||||
publication__placement__project_id=project_id,
|
||||
placement_post__placement__project_id=project_id,
|
||||
status=domain.SubscriptionStatus.ACTIVE,
|
||||
)
|
||||
.prefetch_related('publication', 'telegram_user')
|
||||
.prefetch_related('placement_post', 'telegram_user')
|
||||
.all()
|
||||
)
|
||||
|
||||
@@ -520,10 +522,10 @@ class Postgres(DatabaseBase):
|
||||
return (
|
||||
await domain.Subscription.filter(
|
||||
telegram_user_id=telegram_user_id,
|
||||
publication__placement__project_id=project_id,
|
||||
placement_post__placement__project_id=project_id,
|
||||
status=domain.SubscriptionStatus.ACTIVE,
|
||||
)
|
||||
.prefetch_related('publication', 'telegram_user')
|
||||
.prefetch_related('placement_post', 'telegram_user')
|
||||
.first()
|
||||
)
|
||||
|
||||
@@ -555,22 +557,22 @@ class Postgres(DatabaseBase):
|
||||
|
||||
# Count methods
|
||||
@staticmethod
|
||||
async def count_publications_by_creative(creative_id: uuid.UUID) -> int:
|
||||
return await domain.Publication.filter(placement__creative_id=creative_id).count()
|
||||
async def count_placement_posts_by_creative(creative_id: uuid.UUID) -> int:
|
||||
return await domain.PlacementPost.filter(placement__creative_id=creative_id).count()
|
||||
|
||||
@staticmethod
|
||||
async def count_subscriptions_by_publication(publication_id: uuid.UUID) -> int:
|
||||
return await domain.Subscription.filter(publication_id=publication_id).count()
|
||||
async def count_subscriptions_by_placement_post(placement_post_id: uuid.UUID) -> int:
|
||||
return await domain.Subscription.filter(placement_post_id=placement_post_id).count()
|
||||
|
||||
@staticmethod
|
||||
async def count_publications_by_creative_batch(creative_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
||||
async def count_placement_posts_by_creative_batch(creative_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
||||
if not creative_ids:
|
||||
return {}
|
||||
|
||||
from tortoise.functions import Count
|
||||
|
||||
results = (
|
||||
await domain.Publication.filter(placement__creative_id__in=creative_ids)
|
||||
await domain.PlacementPost.filter(placement__creative_id__in=creative_ids)
|
||||
.group_by('placement__creative_id')
|
||||
.annotate(count=Count('id'))
|
||||
.values('placement__creative_id', 'count')
|
||||
@@ -580,22 +582,22 @@ class Postgres(DatabaseBase):
|
||||
return {cid: counts.get(cid, 0) for cid in creative_ids}
|
||||
|
||||
@staticmethod
|
||||
async def count_subscriptions_by_publication_batch(publication_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
||||
if not publication_ids:
|
||||
async def count_subscriptions_by_placement_post_batch(placement_post_ids: list[uuid.UUID]) -> dict[uuid.UUID, int]:
|
||||
if not placement_post_ids:
|
||||
return {}
|
||||
|
||||
from tortoise.functions import Count
|
||||
|
||||
results = (
|
||||
await domain.Subscription.filter(publication_id__in=publication_ids)
|
||||
.group_by('publication_id')
|
||||
await domain.Subscription.filter(placement_post_id__in=placement_post_ids)
|
||||
.group_by('placement_post_id')
|
||||
.annotate(count=Count('id'))
|
||||
.values('publication_id', 'count')
|
||||
.values('placement_post_id', 'count')
|
||||
)
|
||||
|
||||
counts = {row['publication_id']: row['count'] for row in results}
|
||||
return {pid: counts.get(pid, 0) for pid in publication_ids}
|
||||
counts = {row['placement_post_id']: row['count'] for row in results}
|
||||
return {pid: counts.get(pid, 0) for pid in placement_post_ids}
|
||||
|
||||
@staticmethod
|
||||
async def has_publications_for_creative(creative_id: uuid.UUID) -> bool:
|
||||
return await domain.Publication.filter(placement__creative_id=creative_id).exists()
|
||||
async def has_placement_posts_for_creative(creative_id: uuid.UUID) -> bool:
|
||||
return await domain.PlacementPost.filter(placement__creative_id=creative_id).exists()
|
||||
|
||||
Reference in New Issue
Block a user