2022-12-11 14:15:04 +01:00
|
|
|
from typing import Iterable, Optional
|
2021-08-05 00:29:50 +02:00
|
|
|
from uuid import UUID
|
|
|
|
|
2021-08-04 18:53:58 +02:00
|
|
|
from arrow import Arrow
|
2021-07-15 18:13:11 +02:00
|
|
|
from tortoise.queryset import QuerySet
|
2021-07-05 23:07:12 +02:00
|
|
|
from tortoise.transactions import atomic
|
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.models.event import Event
|
|
|
|
from mobilizon_reshare.models.publication import Publication, PublicationStatus
|
2022-02-09 00:54:56 +01:00
|
|
|
from mobilizon_reshare.models.publisher import Publisher
|
2022-02-14 21:10:27 +01:00
|
|
|
from mobilizon_reshare.storage.query.exceptions import EventNotFound
|
2021-07-12 22:17:49 +02:00
|
|
|
|
2021-08-05 00:29:50 +02:00
|
|
|
|
2021-12-01 01:08:37 +01:00
|
|
|
async def get_all_publications(
|
2022-09-24 16:46:03 +02:00
|
|
|
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
|
2022-05-17 23:14:52 +02:00
|
|
|
) -> Iterable[Publication]:
|
2021-12-01 01:08:37 +01:00
|
|
|
return await prefetch_publication_relations(
|
|
|
|
_add_date_window(Publication.all(), "timestamp", from_date, to_date)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-12-07 21:46:57 +01:00
|
|
|
async def get_all_events(
|
|
|
|
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None
|
|
|
|
):
|
|
|
|
return await prefetch_event_relations(
|
|
|
|
_add_date_window(Event.all(), "begin_datetime", from_date, to_date)
|
|
|
|
)
|
2021-07-05 23:07:12 +02:00
|
|
|
|
|
|
|
|
2022-05-17 23:14:52 +02:00
|
|
|
async def get_all_publishers() -> list[Publisher]:
|
|
|
|
return await Publisher.all()
|
|
|
|
|
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
async def prefetch_event_relations(queryset: QuerySet[Event]) -> list[Event]:
|
|
|
|
return (
|
|
|
|
await queryset.prefetch_related("publications__publisher")
|
|
|
|
.order_by("begin_datetime")
|
|
|
|
.distinct()
|
2021-08-05 00:29:50 +02:00
|
|
|
)
|
2021-07-12 22:17:49 +02:00
|
|
|
|
|
|
|
|
2022-01-04 21:35:43 +01:00
|
|
|
async def prefetch_publication_relations(
|
|
|
|
queryset: QuerySet[Publication],
|
|
|
|
) -> list[Publication]:
|
2022-02-14 21:10:27 +01:00
|
|
|
publication = (
|
2022-12-11 14:15:04 +01:00
|
|
|
await queryset.prefetch_related(
|
|
|
|
"publisher",
|
|
|
|
"event",
|
|
|
|
"event__publications",
|
|
|
|
"event__publications__publisher",
|
|
|
|
)
|
2021-12-01 01:08:37 +01:00
|
|
|
.order_by("timestamp")
|
|
|
|
.distinct()
|
|
|
|
)
|
2022-02-14 21:10:27 +01:00
|
|
|
return publication
|
2021-12-01 01:08:37 +01:00
|
|
|
|
|
|
|
|
2021-11-11 15:18:04 +01:00
|
|
|
def _add_date_window(
|
|
|
|
query,
|
|
|
|
field_name: str,
|
|
|
|
from_date: Optional[Arrow] = None,
|
|
|
|
to_date: Optional[Arrow] = None,
|
|
|
|
):
|
|
|
|
if from_date:
|
|
|
|
query = query.filter(**{f"{field_name}__gt": from_date.to("utc").datetime})
|
|
|
|
if to_date:
|
|
|
|
query = query.filter(**{f"{field_name}__lt": to_date.to("utc").datetime})
|
|
|
|
return query
|
2021-08-05 00:29:50 +02:00
|
|
|
|
|
|
|
|
2022-07-26 22:23:47 +02:00
|
|
|
@atomic()
|
2021-11-11 15:18:04 +01:00
|
|
|
async def publications_with_status(
|
|
|
|
status: PublicationStatus,
|
|
|
|
from_date: Optional[Arrow] = None,
|
|
|
|
to_date: Optional[Arrow] = None,
|
2022-03-06 10:41:02 +01:00
|
|
|
) -> Iterable[Publication]:
|
2021-11-11 15:18:04 +01:00
|
|
|
query = Publication.filter(status=status)
|
2021-08-05 00:29:50 +02:00
|
|
|
|
2021-12-01 01:08:37 +01:00
|
|
|
return await prefetch_publication_relations(
|
|
|
|
_add_date_window(query, "timestamp", from_date, to_date)
|
|
|
|
)
|
2021-07-05 23:07:12 +02:00
|
|
|
|
|
|
|
|
2022-02-09 00:54:56 +01:00
|
|
|
async def get_event(event_mobilizon_id: UUID) -> Event:
|
|
|
|
events = await prefetch_event_relations(
|
|
|
|
Event.filter(mobilizon_id=event_mobilizon_id)
|
|
|
|
)
|
|
|
|
if not events:
|
|
|
|
raise EventNotFound(f"No event with mobilizon_id {event_mobilizon_id} found.")
|
2021-07-05 23:07:12 +02:00
|
|
|
|
2022-02-09 00:54:56 +01:00
|
|
|
return events[0]
|
2021-11-11 15:18:04 +01:00
|
|
|
|
2022-02-09 00:54:56 +01:00
|
|
|
|
2022-12-11 14:15:04 +01:00
|
|
|
async def get_events_without_publications(
|
|
|
|
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
|
|
|
|
) -> list[Event]:
|
|
|
|
query = Event.filter(publications__id=None)
|
|
|
|
return await prefetch_event_relations(
|
|
|
|
_add_date_window(query, "begin_datetime", from_date, to_date)
|
2022-03-06 10:41:02 +01:00
|
|
|
)
|