112 lines
3.8 KiB
Python
Raw Normal View History

2021-05-03 17:26:34 +02:00
from dataclasses import dataclass, asdict
from enum import IntEnum
from typing import Optional, Set
from uuid import UUID
2021-05-05 14:29:13 +02:00
import arrow
2021-05-31 01:11:50 +02:00
import tortoise.timezone
from jinja2 import Template
2021-05-03 17:26:34 +02:00
from mobilizon_reshare.models.event import Event
from mobilizon_reshare.models.publication import PublicationStatus, Publication
2021-05-04 22:58:00 +02:00
class EventPublicationStatus(IntEnum):
WAITING = 1
FAILED = 2
COMPLETED = 3
PARTIAL = 4
2021-05-03 17:26:34 +02:00
@dataclass
class MobilizonEvent:
"""Class representing an event retrieved from Mobilizon."""
name: str
description: Optional[str]
begin_datetime: arrow.Arrow
end_datetime: arrow.Arrow
2021-05-03 17:26:34 +02:00
mobilizon_link: str
mobilizon_id: UUID
2021-05-03 17:26:34 +02:00
thumbnail_link: Optional[str] = None
location: Optional[str] = None
publication_time: Optional[dict[str, arrow.Arrow]] = None
status: EventPublicationStatus = EventPublicationStatus.WAITING
2021-05-03 17:26:34 +02:00
2021-05-04 22:58:00 +02:00
def __post_init__(self):
2021-05-31 01:11:50 +02:00
assert self.begin_datetime.tzinfo == self.end_datetime.tzinfo
2021-05-04 22:58:00 +02:00
assert self.begin_datetime < self.end_datetime
if self.publication_time is None:
self.publication_time = {}
2021-05-04 22:58:00 +02:00
if self.publication_time:
assert self.status in [
EventPublicationStatus.COMPLETED,
EventPublicationStatus.PARTIAL,
EventPublicationStatus.FAILED,
2021-05-04 22:58:00 +02:00
]
2021-05-04 12:07:59 +02:00
2021-05-04 11:48:54 +02:00
def _fill_template(self, pattern: Template) -> str:
return pattern.render(**asdict(self))
2021-05-03 17:26:34 +02:00
2021-05-04 11:48:54 +02:00
def format(self, pattern: Template) -> str:
2021-05-03 17:26:34 +02:00
return self._fill_template(pattern)
2021-05-31 01:11:50 +02:00
def to_model(self) -> Event:
return Event(
name=self.name,
description=self.description,
mobilizon_id=self.mobilizon_id,
mobilizon_link=self.mobilizon_link,
thumbnail_link=self.thumbnail_link,
location=self.location,
begin_datetime=self.begin_datetime.astimezone(self.begin_datetime.tzinfo),
end_datetime=self.end_datetime.astimezone(self.end_datetime.tzinfo),
)
@staticmethod
def compute_status(publications: list[Publication]) -> EventPublicationStatus:
if not publications:
return EventPublicationStatus.WAITING
unique_statuses: Set[PublicationStatus] = set(
pub.status for pub in publications
)
if unique_statuses == {
PublicationStatus.COMPLETED,
PublicationStatus.FAILED,
}:
return EventPublicationStatus.PARTIAL
elif len(unique_statuses) == 1:
return EventPublicationStatus[unique_statuses.pop().name]
raise ValueError(f"Illegal combination of PublicationStatus: {unique_statuses}")
2021-05-31 01:11:50 +02:00
@staticmethod
def from_model(event: Event, tz: str = "UTC"):
publication_status = MobilizonEvent.compute_status(list(event.publications))
2021-12-05 16:43:23 +01:00
publication_time = {}
for pub in event.publications:
if publication_status != EventPublicationStatus.WAITING:
assert pub.timestamp is not None
publication_time[pub.publisher.name] = arrow.get(
tortoise.timezone.localtime(value=pub.timestamp, timezone=tz)
).to("local")
2021-05-31 01:11:50 +02:00
return MobilizonEvent(
name=event.name,
description=event.description,
begin_datetime=arrow.get(
tortoise.timezone.localtime(value=event.begin_datetime, timezone=tz)
).to("local"),
2021-05-31 01:11:50 +02:00
end_datetime=arrow.get(
tortoise.timezone.localtime(value=event.end_datetime, timezone=tz)
).to("local"),
2021-05-31 01:11:50 +02:00
mobilizon_link=event.mobilizon_link,
mobilizon_id=event.mobilizon_id,
thumbnail_link=event.thumbnail_link,
location=event.location,
2021-12-05 16:43:23 +01:00
publication_time=publication_time,
status=publication_status,
2021-05-31 01:11:50 +02:00
)