From acce3a83fea7f21264c0b7f9ffaf53d9634f0882 Mon Sep 17 00:00:00 2001 From: Giacomo Leidi Date: Sun, 16 Jul 2023 15:09:08 +0200 Subject: [PATCH] Enable publishing events by UUID. (#187) --- mobilizon_reshare/cli/cli.py | 35 ++++++++++--------- .../cli/commands/list/list_publication.py | 3 +- .../cli/commands/publish/main.py | 15 ++++++-- mobilizon_reshare/dataclasses/event.py | 9 ++++- mobilizon_reshare/main/publish.py | 10 ++++++ .../coordinators/event_publishing/dry_run.py | 11 +++++- tests/commands/test_publish.py | 6 ++-- 7 files changed, 64 insertions(+), 25 deletions(-) diff --git a/mobilizon_reshare/cli/cli.py b/mobilizon_reshare/cli/cli.py index 9abefa5..b767d31 100644 --- a/mobilizon_reshare/cli/cli.py +++ b/mobilizon_reshare/cli/cli.py @@ -88,26 +88,21 @@ publication_status_argument = click.argument( default="all", expose_value=True, ) -event_uuid_option = click.option( - "-E", - "--event", +force_publish_option = click.option( + "-F", + "--force", type=click.UUID, expose_value=True, - help="Publish the given event.", -) -publication_uuid_option = click.option( - "-P", - "--publication", - type=click.UUID, - expose_value=True, - help="Publish the given publication.", + help="Publish the given event, bypassing all selection logic. This command WILL publish" + "regardless of the configured strategy, so use it with care.", ) platform_name_option = click.option( "-p", "--platform", type=str, expose_value=True, - help="Publish to the given platform. This makes sense only for events.", + help="Restrict the platforms where the event will be published. This makes sense only in" + " case of force-publishing.", ) list_supported_option = click.option( "--list-platforms", @@ -182,11 +177,19 @@ def pull(): help="Select an event with the current configured strategy" " and publish it to all active platforms." ) -@event_uuid_option -@publication_uuid_option +@force_publish_option @platform_name_option -def publish(): - safe_execution(publish_main,) +@click.option( + "--dry-run", + "dry_run", + is_flag=True, + help="Prevents data to be published to platforms.", + default=False, +) +def publish(event, platform, dry_run): + safe_execution(functools.partial( + publish_main, event, platform + ), CommandConfig(dry_run=dry_run)) @mobilizon_reshare.group(help="Operations that pertain to events") diff --git a/mobilizon_reshare/cli/commands/list/list_publication.py b/mobilizon_reshare/cli/commands/list/list_publication.py index 1d8878d..02735c0 100644 --- a/mobilizon_reshare/cli/commands/list/list_publication.py +++ b/mobilizon_reshare/cli/commands/list/list_publication.py @@ -24,7 +24,7 @@ def pretty(publication: Publication): return ( f"{str(publication.id) : <40}{publication.timestamp.isoformat() : <36}" f"{click.style(publication.status.name, fg=status_to_color[publication.status]) : <22}" - f"{publication.publisher.name : <12}{str(publication.event.id)}" + f"{publication.publisher.name : <12}{str(publication.event.mobilizon_id)}" ) @@ -33,7 +33,6 @@ async def list_publications( frm: Optional[datetime] = None, to: Optional[datetime] = None, ): - frm = Arrow.fromdatetime(frm) if frm else None to = Arrow.fromdatetime(to) if to else None if status is None: diff --git a/mobilizon_reshare/cli/commands/publish/main.py b/mobilizon_reshare/cli/commands/publish/main.py index 27ad294..e9f7a22 100644 --- a/mobilizon_reshare/cli/commands/publish/main.py +++ b/mobilizon_reshare/cli/commands/publish/main.py @@ -1,14 +1,23 @@ import logging +import click -from mobilizon_reshare.main.publish import select_and_publish +from mobilizon_reshare.config.command import CommandConfig +from mobilizon_reshare.main.publish import select_and_publish, publish_by_mobilizon_id logger = logging.getLogger(__name__) -async def publish_command(): +async def publish_command(event_mobilizon_id: click.UUID, platform: str, command_config: CommandConfig): """ Select an event with the current configured strategy and publish it to all active platforms. """ - report = await select_and_publish() + if event_mobilizon_id is not None: + report = await publish_by_mobilizon_id( + event_mobilizon_id, + command_config, + [platform] if platform is not None else None, + ) + else: + report = await select_and_publish(command_config) return 0 if report and report.successful else 1 diff --git a/mobilizon_reshare/dataclasses/event.py b/mobilizon_reshare/dataclasses/event.py index ec13bc7..a2a6489 100644 --- a/mobilizon_reshare/dataclasses/event.py +++ b/mobilizon_reshare/dataclasses/event.py @@ -108,7 +108,7 @@ class _MobilizonEvent: async def get_all_mobilizon_events( from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None, ) -> list[_MobilizonEvent]: - return [_MobilizonEvent.from_model(event) for event in await get_all_events()] + return [_MobilizonEvent.from_model(event) for event in await get_all_events(from_date, to_date)] async def get_published_events( @@ -155,3 +155,10 @@ async def get_mobilizon_events_without_publications( from_date=from_date, to_date=to_date ) ] + + +async def get_mobilizon_event_by_id( + event_id: UUID, +) -> _MobilizonEvent: + event = await get_event(event_id) + return _MobilizonEvent.from_model(event) diff --git a/mobilizon_reshare/main/publish.py b/mobilizon_reshare/main/publish.py index c2f2cdd..8e22513 100644 --- a/mobilizon_reshare/main/publish.py +++ b/mobilizon_reshare/main/publish.py @@ -6,6 +6,7 @@ from mobilizon_reshare.dataclasses import MobilizonEvent from mobilizon_reshare.dataclasses.event import ( get_published_events, get_mobilizon_events_without_publications, + get_mobilizon_event_by_id, ) from mobilizon_reshare.dataclasses.publication import ( _EventPublication, @@ -68,6 +69,15 @@ async def publish_event( return await publish_publications(publications) +async def publish_by_mobilizon_id( + event_mobilizon_id, + command_config: CommandConfig, + publishers: Optional[Iterator[str]] = None, +): + event = await get_mobilizon_event_by_id(event_mobilizon_id) + return await publish_event(event, command_config, publishers) + + async def select_and_publish( command_config: CommandConfig, unpublished_events: Optional[list[MobilizonEvent]] = None, diff --git a/mobilizon_reshare/publishers/coordinators/event_publishing/dry_run.py b/mobilizon_reshare/publishers/coordinators/event_publishing/dry_run.py index 4d9308b..66a4478 100644 --- a/mobilizon_reshare/publishers/coordinators/event_publishing/dry_run.py +++ b/mobilizon_reshare/publishers/coordinators/event_publishing/dry_run.py @@ -1,3 +1,4 @@ +import logging from typing import List, Sequence from mobilizon_reshare.dataclasses import _EventPublication @@ -7,6 +8,8 @@ from mobilizon_reshare.publishers.coordinators.event_publishing.publish import ( EventPublicationReport, ) +logger = logging.getLogger(__name__) + class DryRunPublisherCoordinator(PublisherCoordinator): """ @@ -14,7 +17,7 @@ class DryRunPublisherCoordinator(PublisherCoordinator): """ def _publish(self, publications: Sequence[_EventPublication]) -> List[EventPublicationReport]: - return [ + reports = [ EventPublicationReport( status=PublicationStatus.COMPLETED, publication=publication, @@ -25,3 +28,9 @@ class DryRunPublisherCoordinator(PublisherCoordinator): ) for publication in publications ] + logger.info("The following events would be published:") + for r in reports: + event_name = r.publication.event.name + publisher_name = r.publication.publisher.name + logger.info(f"{event_name} → {publisher_name}") + return reports diff --git a/tests/commands/test_publish.py b/tests/commands/test_publish.py index eace785..9c088af 100644 --- a/tests/commands/test_publish.py +++ b/tests/commands/test_publish.py @@ -5,7 +5,7 @@ import pytest from mobilizon_reshare.dataclasses import EventPublicationStatus from mobilizon_reshare.dataclasses import MobilizonEvent -from mobilizon_reshare.main.publish import select_and_publish, publish_event +from mobilizon_reshare.main.publish import select_and_publish, publish_by_mobilizon_id from mobilizon_reshare.models.notification import NotificationStatus, Notification from mobilizon_reshare.models.event import Event from mobilizon_reshare.models.publication import PublicationStatus @@ -104,7 +104,9 @@ async def test_publish_event( await generate_models(one_unpublished_event_specification) with caplog.at_level(DEBUG): # calling mobilizon-reshare publish -E -p - report = await publish_event(event_0, command_config, publishers) + report = await publish_by_mobilizon_id( + event_0.mobilizon_id, command_config, publishers + ) assert report is not None assert report.successful