2021-05-03 17:26:34 +02:00
|
|
|
from dataclasses import dataclass, asdict
|
2021-07-15 18:13:11 +02:00
|
|
|
from enum import IntEnum
|
2022-02-09 00:54:56 +01:00
|
|
|
from typing import Optional
|
2021-08-29 14:51:52 +02:00
|
|
|
from uuid import UUID
|
2021-05-05 14:29:13 +02:00
|
|
|
|
2021-05-30 21:47:36 +02:00
|
|
|
import arrow
|
2021-07-05 23:07:12 +02:00
|
|
|
from jinja2 import Template
|
2021-05-03 17:26:34 +02:00
|
|
|
|
2022-04-11 18:13:35 +02:00
|
|
|
from mobilizon_reshare.config.config import get_settings
|
|
|
|
|
2021-05-04 22:58:00 +02:00
|
|
|
|
2021-07-15 18:13:11 +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
|
2021-05-30 21:47:36 +02:00
|
|
|
description: Optional[str]
|
|
|
|
begin_datetime: arrow.Arrow
|
|
|
|
end_datetime: arrow.Arrow
|
2021-05-03 17:26:34 +02:00
|
|
|
mobilizon_link: str
|
2021-08-29 14:51:52 +02:00
|
|
|
mobilizon_id: UUID
|
2022-02-09 00:54:56 +01:00
|
|
|
last_update_time: arrow.Arrow
|
2021-05-03 17:26:34 +02:00
|
|
|
thumbnail_link: Optional[str] = None
|
|
|
|
location: Optional[str] = None
|
2021-07-05 23:07:12 +02:00
|
|
|
publication_time: Optional[dict[str, arrow.Arrow]] = None
|
2021-07-15 18:13:11 +02:00
|
|
|
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
|
2021-11-11 15:18:04 +01:00
|
|
|
if self.publication_time is None:
|
|
|
|
self.publication_time = {}
|
2021-05-04 22:58:00 +02:00
|
|
|
if self.publication_time:
|
2021-07-15 18:13:11 +02:00
|
|
|
assert self.status in [
|
|
|
|
EventPublicationStatus.COMPLETED,
|
|
|
|
EventPublicationStatus.PARTIAL,
|
2021-08-14 18:23:55 +02:00
|
|
|
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:
|
2022-04-11 18:13:35 +02:00
|
|
|
config = get_settings()
|
|
|
|
return pattern.render(locale=config["locale"], **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)
|