tinoilcotechino 6cea51bcab
Publications (#28)
* mobilizon_bots: query: Add create_publisher.

* Move PublicationStatus to models.publication.

* Move NotificationStatus to models.notification.

* storage: query: Add events_with_status.

* storage: query: Add get_unpublished_events.

* storage: query: Add create_unpublished events.

This function takes care of looking into the database
to store only the events whose  is not
already present.

* event: event: Support multiple publications.

This patch changes the public interface of `MobilizonEvent` to
support multiple publications. This mainly entails two changes:

  - When instancing a `MobilizonEvent`  from an `Event` model
    the `publication_status` will be computed by looking at
    the statuses of all the related publications.
  - Now the `publication_time` is a `dict[str, Arrow]`. This
    enables tracking multiple social platforms publication time.

* Update main.

* Minor fixes

* Better definition of MobilizonEvent.publication_status.

Co-authored-by: Giacomo Leidi <goodoldpaul@autistici.org>
2021-07-05 23:07:12 +02:00

53 lines
1.6 KiB
Python

import asyncio
import atexit
import logging
from pathlib import Path
from tortoise import Tortoise
from mobilizon_bots.config.publishers import publisher_names
from mobilizon_bots.storage.query import create_publisher
logger = logging.getLogger(__name__)
class MobilizonBotsDB:
def __init__(self, path: Path):
self.path = path
# TODO: Check if DB is openable/"queriable"
self.is_init = self.path.exists() and (not self.path.is_dir())
if not self.is_init:
self.path.parent.mkdir(parents=True, exist_ok=True)
async def setup(self):
await Tortoise.init(
db_url=f"sqlite:///{self.path}",
modules={
"models": [
"mobilizon_bots.models.event",
"mobilizon_bots.models.notification",
"mobilizon_bots.models.publication",
"mobilizon_bots.models.publisher",
]
},
# always store UTC time in database
use_tz=True,
)
if not self.is_init:
await Tortoise.generate_schemas()
for name in publisher_names:
logging.info(f"Creating {name} publisher")
# TODO: Deal with account_ref
await create_publisher(name)
self.is_init = True
logger.info(f"Succesfully initialized database at {self.path}")
@atexit.register
def gracefully_tear_down():
logger.info("Shutting down DB")
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncio.run(Tortoise.close_connections())