Enable publishing events by UUID. (#187)
This commit is contained in:
parent
775fb89cf6
commit
acce3a83fe
|
@ -88,26 +88,21 @@ publication_status_argument = click.argument(
|
||||||
default="all",
|
default="all",
|
||||||
expose_value=True,
|
expose_value=True,
|
||||||
)
|
)
|
||||||
event_uuid_option = click.option(
|
force_publish_option = click.option(
|
||||||
"-E",
|
"-F",
|
||||||
"--event",
|
"--force",
|
||||||
type=click.UUID,
|
type=click.UUID,
|
||||||
expose_value=True,
|
expose_value=True,
|
||||||
help="Publish the given event.",
|
help="Publish the given event, bypassing all selection logic. This command WILL publish"
|
||||||
)
|
"regardless of the configured strategy, so use it with care.",
|
||||||
publication_uuid_option = click.option(
|
|
||||||
"-P",
|
|
||||||
"--publication",
|
|
||||||
type=click.UUID,
|
|
||||||
expose_value=True,
|
|
||||||
help="Publish the given publication.",
|
|
||||||
)
|
)
|
||||||
platform_name_option = click.option(
|
platform_name_option = click.option(
|
||||||
"-p",
|
"-p",
|
||||||
"--platform",
|
"--platform",
|
||||||
type=str,
|
type=str,
|
||||||
expose_value=True,
|
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_supported_option = click.option(
|
||||||
"--list-platforms",
|
"--list-platforms",
|
||||||
|
@ -182,11 +177,19 @@ def pull():
|
||||||
help="Select an event with the current configured strategy"
|
help="Select an event with the current configured strategy"
|
||||||
" and publish it to all active platforms."
|
" and publish it to all active platforms."
|
||||||
)
|
)
|
||||||
@event_uuid_option
|
@force_publish_option
|
||||||
@publication_uuid_option
|
|
||||||
@platform_name_option
|
@platform_name_option
|
||||||
def publish():
|
@click.option(
|
||||||
safe_execution(publish_main,)
|
"--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")
|
@mobilizon_reshare.group(help="Operations that pertain to events")
|
||||||
|
|
|
@ -24,7 +24,7 @@ def pretty(publication: Publication):
|
||||||
return (
|
return (
|
||||||
f"{str(publication.id) : <40}{publication.timestamp.isoformat() : <36}"
|
f"{str(publication.id) : <40}{publication.timestamp.isoformat() : <36}"
|
||||||
f"{click.style(publication.status.name, fg=status_to_color[publication.status]) : <22}"
|
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,
|
frm: Optional[datetime] = None,
|
||||||
to: Optional[datetime] = None,
|
to: Optional[datetime] = None,
|
||||||
):
|
):
|
||||||
|
|
||||||
frm = Arrow.fromdatetime(frm) if frm else None
|
frm = Arrow.fromdatetime(frm) if frm else None
|
||||||
to = Arrow.fromdatetime(to) if to else None
|
to = Arrow.fromdatetime(to) if to else None
|
||||||
if status is None:
|
if status is None:
|
||||||
|
|
|
@ -1,14 +1,23 @@
|
||||||
import logging
|
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__)
|
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
|
Select an event with the current configured strategy
|
||||||
and publish it to all active platforms.
|
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
|
return 0 if report and report.successful else 1
|
||||||
|
|
|
@ -108,7 +108,7 @@ class _MobilizonEvent:
|
||||||
async def get_all_mobilizon_events(
|
async def get_all_mobilizon_events(
|
||||||
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
|
from_date: Optional[Arrow] = None, to_date: Optional[Arrow] = None,
|
||||||
) -> list[_MobilizonEvent]:
|
) -> 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(
|
async def get_published_events(
|
||||||
|
@ -155,3 +155,10 @@ async def get_mobilizon_events_without_publications(
|
||||||
from_date=from_date, to_date=to_date
|
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)
|
||||||
|
|
|
@ -6,6 +6,7 @@ from mobilizon_reshare.dataclasses import MobilizonEvent
|
||||||
from mobilizon_reshare.dataclasses.event import (
|
from mobilizon_reshare.dataclasses.event import (
|
||||||
get_published_events,
|
get_published_events,
|
||||||
get_mobilizon_events_without_publications,
|
get_mobilizon_events_without_publications,
|
||||||
|
get_mobilizon_event_by_id,
|
||||||
)
|
)
|
||||||
from mobilizon_reshare.dataclasses.publication import (
|
from mobilizon_reshare.dataclasses.publication import (
|
||||||
_EventPublication,
|
_EventPublication,
|
||||||
|
@ -68,6 +69,15 @@ async def publish_event(
|
||||||
return await publish_publications(publications)
|
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(
|
async def select_and_publish(
|
||||||
command_config: CommandConfig,
|
command_config: CommandConfig,
|
||||||
unpublished_events: Optional[list[MobilizonEvent]] = None,
|
unpublished_events: Optional[list[MobilizonEvent]] = None,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
from typing import List, Sequence
|
from typing import List, Sequence
|
||||||
|
|
||||||
from mobilizon_reshare.dataclasses import _EventPublication
|
from mobilizon_reshare.dataclasses import _EventPublication
|
||||||
|
@ -7,6 +8,8 @@ from mobilizon_reshare.publishers.coordinators.event_publishing.publish import (
|
||||||
EventPublicationReport,
|
EventPublicationReport,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class DryRunPublisherCoordinator(PublisherCoordinator):
|
class DryRunPublisherCoordinator(PublisherCoordinator):
|
||||||
"""
|
"""
|
||||||
|
@ -14,7 +17,7 @@ class DryRunPublisherCoordinator(PublisherCoordinator):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def _publish(self, publications: Sequence[_EventPublication]) -> List[EventPublicationReport]:
|
def _publish(self, publications: Sequence[_EventPublication]) -> List[EventPublicationReport]:
|
||||||
return [
|
reports = [
|
||||||
EventPublicationReport(
|
EventPublicationReport(
|
||||||
status=PublicationStatus.COMPLETED,
|
status=PublicationStatus.COMPLETED,
|
||||||
publication=publication,
|
publication=publication,
|
||||||
|
@ -25,3 +28,9 @@ class DryRunPublisherCoordinator(PublisherCoordinator):
|
||||||
)
|
)
|
||||||
for publication in publications
|
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
|
||||||
|
|
|
@ -5,7 +5,7 @@ import pytest
|
||||||
|
|
||||||
from mobilizon_reshare.dataclasses import EventPublicationStatus
|
from mobilizon_reshare.dataclasses import EventPublicationStatus
|
||||||
from mobilizon_reshare.dataclasses import MobilizonEvent
|
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.notification import NotificationStatus, Notification
|
||||||
from mobilizon_reshare.models.event import Event
|
from mobilizon_reshare.models.event import Event
|
||||||
from mobilizon_reshare.models.publication import PublicationStatus
|
from mobilizon_reshare.models.publication import PublicationStatus
|
||||||
|
@ -104,7 +104,9 @@ async def test_publish_event(
|
||||||
await generate_models(one_unpublished_event_specification)
|
await generate_models(one_unpublished_event_specification)
|
||||||
with caplog.at_level(DEBUG):
|
with caplog.at_level(DEBUG):
|
||||||
# calling mobilizon-reshare publish -E <UUID> -p <platform>
|
# calling mobilizon-reshare publish -E <UUID> -p <platform>
|
||||||
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 is not None
|
||||||
assert report.successful
|
assert report.successful
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue