diff --git a/db/create.py b/db/create.py index 879fe51..1cb7968 100644 --- a/db/create.py +++ b/db/create.py @@ -1,5 +1,46 @@ +""" +Create database file and tables if not exists. +:: + + +--------------+ + |services | +---------------+ + +--------------+ |notifications | + |service_id |<--------+ +---------------+ + |token | | |notification_id| + |name | +-------|service_id | + |description | |timestamp | + |representation| |content_json | + |by_request | +---------------+ + +--------------+ + +* services table + * **service_id:** service id + *(integer primary key)*. + * **token:** service token for access to api + *(text, not null)*. + * **name:** service name + *(text, not null)*. + * **description:** service description + *(text with default value)*. + * **representation:** how to display notification + *(integer with 0 as default)*. + * **by_request:** send notifications automatically or not + *(integer with 0 as default)*. +* notifications table + * **notification_id:** notification id + *(integer primary key)*. + * **service_id:** service id + *(integer, not null, references to* + *service_id in service table)*. + * **timestamp:** UTC timestamp, example: **"2022-02-14T13:45:15Z"** + *(text, not null)*. + + * **content_json:** content in json_format + *(text, not null)*. +""" import sqlite3 + CREATE_SERVICES_TABLE = '''CREATE TABLE IF NOT EXISTS services ( service_id INTEGER PRIMARY KEY, token TEXT NOT NULL, @@ -20,6 +61,12 @@ ON UPDATE CASCADE ON DELETE CASCADE def create_tables_if_not_exists(dbfile: str) -> None: + """ + Create database file and tables if not exists. + *See module docstring*. + + :param dbfile: path to database file. + """ connection = sqlite3.connect(dbfile) cursor = connection.cursor() cursor.execute('PRAGMA foreign_keys=0') diff --git a/start.py b/start.py index 7e88d3c..996010f 100644 --- a/start.py +++ b/start.py @@ -3,10 +3,11 @@ import logging from aiohttp import web from config import config +from telegram import telegram_bot, telegram_dispatcher import db -from web import app - +from web import create_app +app = create_app(telegram_bot, telegram_dispatcher, config) logging.basicConfig(level=logging.INFO) diff --git a/web/__init__.py b/web/__init__.py index 0e428a9..4e14062 100644 --- a/web/__init__.py +++ b/web/__init__.py @@ -1,34 +1,44 @@ import logging +from aiogram import Bot, Dispatcher from aiogram.dispatcher.webhook import configure_app -from aiohttp import web +from aiohttp.web import Application -from config import config -from telegram import telegram_bot, telegram_dispatcher +from config import Config from .routes import routes, TELEGRAM_WEBHOOK_ROUTE -TELEGRAM_WEBHOOK_URL = f'{config.base_url}{TELEGRAM_WEBHOOK_ROUTE}' +def create_app(bot: Bot, + dispatcher: Dispatcher, + config: Config) -> Application: + """ + Create aiohttp application. + + :param bot: telegram bot instance. + :param dispatcher: telegram dispatcher instance. + :param config: config object. + :return: aiohttp Appliction. + """ + + telegram_webhook_url = f'{config.base_url}{TELEGRAM_WEBHOOK_ROUTE}' + + async def on_startup(_): + await bot.set_webhook(telegram_webhook_url) + + async def on_shutdown(_): + logging.warning('Shutting down') + await bot.delete_webhook() + await dispatcher.storage.close() + await dispatcher.storage.wait_closed() + session = await bot.get_session() + await session.close() + logging.warning('Bye!..') + + app = Application() + app.add_routes(routes) + app.on_startup.append(on_startup) + app.on_shutdown.append(on_shutdown) + configure_app(dispatcher, app, TELEGRAM_WEBHOOK_ROUTE) + return app -async def on_startup(_): - await telegram_bot.set_webhook(TELEGRAM_WEBHOOK_URL) - - -async def on_shutdown(_): - logging.warning('Shutting down') - await telegram_bot.delete_webhook() - await telegram_dispatcher.storage.close() - await telegram_dispatcher.storage.wait_closed() - session = await telegram_bot.get_session() - await session.close() - logging.warning('Bye!..') - - -app = web.Application() -app.add_routes(routes) -app.on_startup.append(on_startup) -app.on_shutdown.append(on_shutdown) -configure_app(telegram_dispatcher, app, TELEGRAM_WEBHOOK_ROUTE) - - -__all__ = ('app',) +__all__ = ('create_app',)