split all read functions

This commit is contained in:
Simone Robutti 2022-12-09 22:06:19 +01:00
parent fad7d46fee
commit a722d35d89
8 changed files with 56 additions and 55 deletions

View File

@ -10,8 +10,8 @@ from mobilizon_reshare.dataclasses.event import (
get_all_mobilizon_events,
get_published_events,
get_mobilizon_events_with_status,
get_mobilizon_events_without_publications,
)
from mobilizon_reshare.dataclasses.to_split import events_without_publications
from mobilizon_reshare.event.event_selection_strategies import select_unpublished_events
status_to_color = {
@ -38,7 +38,9 @@ def pretty(event: MobilizonEvent):
async def list_unpublished_events(frm: Arrow = None, to: Arrow = None):
return select_unpublished_events(
list(await get_published_events(from_date=frm, to_date=to)),
list(await events_without_publications(from_date=frm, to_date=to)),
list(
await get_mobilizon_events_without_publications(from_date=frm, to_date=to)
),
)

View File

@ -12,7 +12,11 @@ from mobilizon_reshare.dataclasses.event_publication_status import (
_compute_event_status,
)
from mobilizon_reshare.models.event import Event
from mobilizon_reshare.storage.query.read import get_all_events, get_event
from mobilizon_reshare.storage.query.read import (
get_all_events,
get_event,
get_events_without_publications,
)
@dataclass
@ -140,3 +144,14 @@ async def get_mobilizon_events_with_status(
_MobilizonEvent.from_model,
filter(_filter_event_with_status, await get_all_events(from_date, to_date)),
)
async def get_mobilizon_events_without_publications(
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
) -> list[_MobilizonEvent]:
return [
_MobilizonEvent.from_model(event)
for event in await get_events_without_publications(
from_date=from_date, to_date=to_date
)
]

View File

@ -1,38 +0,0 @@
from typing import Optional
from uuid import UUID
from arrow import Arrow
from tortoise.exceptions import DoesNotExist
from tortoise.transactions import atomic
from mobilizon_reshare.dataclasses import MobilizonEvent, EventPublication
from mobilizon_reshare.models.event import Event
from mobilizon_reshare.models.publication import Publication
from mobilizon_reshare.storage.query.read import (
prefetch_event_relations,
_add_date_window,
prefetch_publication_relations,
)
async def events_without_publications(
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
) -> list[MobilizonEvent]:
query = Event.filter(publications__id=None)
events = await prefetch_event_relations(
_add_date_window(query, "begin_datetime", from_date, to_date)
)
return [MobilizonEvent.from_model(event) for event in events]
@atomic()
async def get_publication(publication_id: UUID):
try:
publication = await prefetch_publication_relations(
Publication.get(id=publication_id).first()
)
return EventPublication.from_orm(
event=MobilizonEvent.from_model(publication.event), model=publication
)
except DoesNotExist:
return None

View File

@ -3,12 +3,14 @@ from typing import Optional, Iterator
from mobilizon_reshare.config.command import CommandConfig
from mobilizon_reshare.dataclasses import MobilizonEvent
from mobilizon_reshare.dataclasses.event import get_published_events
from mobilizon_reshare.dataclasses.event import (
get_published_events,
get_mobilizon_events_without_publications,
)
from mobilizon_reshare.dataclasses.publication import (
_EventPublication,
build_publications_for_event,
)
from mobilizon_reshare.dataclasses.to_split import events_without_publications
from mobilizon_reshare.event.event_selection_strategies import select_event_to_publish
from mobilizon_reshare.publishers import get_active_publishers
from mobilizon_reshare.publishers.coordinators.event_publishing.dry_run import (
@ -70,7 +72,7 @@ async def select_and_publish(
:return:
"""
if unpublished_events is None:
unpublished_events = await events_without_publications()
unpublished_events = await get_mobilizon_events_without_publications()
event = select_event_to_publish(
list(await get_published_events()), unpublished_events,

View File

@ -89,3 +89,12 @@ async def get_event(event_mobilizon_id: UUID) -> Event:
raise EventNotFound(f"No event with mobilizon_id {event_mobilizon_id} found.")
return events[0]
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)
)

View File

@ -5,7 +5,9 @@ import arrow
from tortoise.transactions import atomic
from mobilizon_reshare.dataclasses import MobilizonEvent
from mobilizon_reshare.dataclasses.to_split import events_without_publications
from mobilizon_reshare.dataclasses.event import (
get_mobilizon_events_without_publications,
)
from mobilizon_reshare.models.event import Event
from mobilizon_reshare.models.publication import Publication
from mobilizon_reshare.models.publisher import Publisher
@ -82,7 +84,7 @@ async def create_unpublished_events(
await event.to_model(db_id=event_model.id).save(force_update=True)
# Or it's known and unchanged, in which case we do nothing.
return await events_without_publications()
return await get_mobilizon_events_without_publications()
@atomic()

View File

@ -2,8 +2,10 @@ from logging import DEBUG, INFO
import pytest
from mobilizon_reshare.dataclasses.event import get_all_mobilizon_events
from mobilizon_reshare.dataclasses.to_split import events_without_publications
from mobilizon_reshare.dataclasses.event import (
get_all_mobilizon_events,
get_mobilizon_events_without_publications,
)
from mobilizon_reshare.main.pull import pull
from mobilizon_reshare.main.start import start
from tests.commands.conftest import (
@ -72,7 +74,7 @@ async def test_pull(
assert (
f"There are now {len(expected_result)} unpublished events." in caplog.text
)
assert expected_result == await events_without_publications()
assert expected_result == await get_mobilizon_events_without_publications()
@pytest.mark.asyncio
@ -111,7 +113,7 @@ async def test_pull_start(
with caplog.at_level(INFO):
assert await pull() == expected_pull
assert expected_pull == await get_all_mobilizon_events()
assert expected_pull == await events_without_publications()
assert expected_pull == await get_mobilizon_events_without_publications()
report = await start(command_config)
assert report.successful
@ -125,7 +127,8 @@ async def test_pull_start(
event.mobilizon_id for event in await get_all_mobilizon_events()
)
assert (pull_ids - publish_ids) == set(
event.mobilizon_id for event in await events_without_publications()
event.mobilizon_id
for event in await get_mobilizon_events_without_publications()
)
@ -189,7 +192,10 @@ async def test_multiple_pull(
assert await pull()
assert f"There are now {len(expected_first)} unpublished events." in caplog.text
assert expected_first == await get_all_mobilizon_events()
assert await events_without_publications() == await get_all_mobilizon_events()
assert (
await get_mobilizon_events_without_publications()
== await get_all_mobilizon_events()
)
# I clean the message collector
message_collector.data = []
@ -202,4 +208,7 @@ async def test_multiple_pull(
assert set(event.mobilizon_id for event in expected_last) == set(
event.mobilizon_id for event in await get_all_mobilizon_events()
)
assert await events_without_publications() == await get_all_mobilizon_events()
assert (
await get_mobilizon_events_without_publications()
== await get_all_mobilizon_events()
)

View File

@ -7,9 +7,9 @@ from mobilizon_reshare.dataclasses.event import (
_EventPublicationStatus,
get_published_events,
get_mobilizon_events_with_status,
get_mobilizon_events_without_publications,
)
from mobilizon_reshare.dataclasses.publication import build_publications_for_event
from mobilizon_reshare.dataclasses.to_split import events_without_publications
from mobilizon_reshare.models.publication import PublicationStatus
from mobilizon_reshare.storage.query.read import publications_with_status
from tests import today
@ -148,7 +148,7 @@ async def test_event_with_status_window(
)
async def test_events_without_publications(spec, expected_events, generate_models):
await generate_models(spec)
unpublished_events = list(await events_without_publications())
unpublished_events = list(await get_mobilizon_events_without_publications())
assert len(unpublished_events) == len(expected_events)
assert unpublished_events == expected_events