Skip to content

Commit

Permalink
added scn to webapp link
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Jun 16, 2024
1 parent b8ad208 commit 2d844a4
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 6 deletions.
2 changes: 2 additions & 0 deletions config_dist/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,5 @@ file-storage-config:
mkdir: true
exist-ok: true
parents: true
web:
base-url: https://exmple.org
1 change: 1 addition & 0 deletions shvatka/api/config/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ def from_base(
context_path=context_path,
app=base.app,
enable_logging=enable_logging,
web=base.web,
)
6 changes: 6 additions & 0 deletions shvatka/common/config/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Config:
db: DBConfig
redis: RedisConfig
file_storage_config: FileStorageConfig
web: WebConfig

@property
def app_dir(self) -> Path:
Expand All @@ -39,3 +40,8 @@ class FileStorageConfig:
mkdir: bool
parents: bool
exist_ok: bool


@dataclass
class WebConfig:
base_url: str
7 changes: 6 additions & 1 deletion shvatka/common/config/parser/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclass_factory import Factory

from shvatka.common.config.models.main import Config, FileStorageConfig, AppConfig
from shvatka.common.config.models.main import Config, FileStorageConfig, AppConfig, WebConfig
from shvatka.common.config.models.paths import Paths
from shvatka.infrastructure.db.config.parser.db import load_db_config, load_redis_config

Expand All @@ -12,6 +12,7 @@ def load_config(config_dct: dict, paths: Paths, dcf: Factory) -> Config:
redis=load_redis_config(config_dct["redis"]),
file_storage_config=load_file_storage_config(config_dct["file-storage-config"], dcf),
app=load_app_config(config_dct["app"], dcf),
web=load_web_config(config_dct["web"], dcf),
)


Expand All @@ -21,3 +22,7 @@ def load_app_config(config_dct: dict, dcf: Factory) -> AppConfig:

def load_file_storage_config(config_dct: dict, dcf: Factory) -> FileStorageConfig:
return dcf.load(config_dct, FileStorageConfig)


def load_web_config(config_dct: dict, dcf: Factory) -> WebConfig:
return dcf.load(config_dct, WebConfig)
7 changes: 7 additions & 0 deletions shvatka/common/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dishka import Provider, Scope, provide
from telegraph.aio import Telegraph

from shvatka.common.url_factory import UrlFactory
from shvatka.core.models.schems import schemas
from shvatka.tgbot.config.models.bot import BotConfig

Expand All @@ -26,3 +27,9 @@ def create_dataclass_factory(self) -> dataclass_factory.Factory:
default_schema=Schema(name_style=NameStyle.kebab),
)
return dcf


class UrlProvider(Provider):
scope = Scope.APP

url_factory = provide(UrlFactory)
9 changes: 9 additions & 0 deletions shvatka/common/url_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from shvatka.common.config.models.main import WebConfig


class UrlFactory:
def __init__(self, config: WebConfig):
self.config = config

def get_game_id_web_url(self, game_id: int) -> str:
return f"{self.config.base_url}/games/{game_id}"
5 changes: 5 additions & 0 deletions shvatka/infrastructure/di/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dishka import Provider, provide, Scope

from shvatka.common import FileStorageConfig, Paths, Config
from shvatka.common.config.models.main import WebConfig
from shvatka.common.config.parser.paths import common_get_paths
from shvatka.infrastructure.db.config.models.db import RedisConfig, DBConfig
from shvatka.infrastructure.db.config.models.storage import StorageConfig
Expand Down Expand Up @@ -44,6 +45,10 @@ def get_bot_storage_config(self, config: TgBotConfig) -> StorageConfig:
def get_tg_client_config(self, config: TgBotConfig) -> TgClientConfig:
return config.tg_client

@provide
def get_web_app_config(self, config: TgBotConfig) -> WebConfig:
return config.web


class DbConfigProvider(Provider):
scope = Scope.APP
Expand Down
1 change: 1 addition & 0 deletions shvatka/tgbot/config/models/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ def from_base(
tg_client=tg_client,
file_storage_config=base.file_storage_config,
app=base.app,
web=base.web,
)
6 changes: 3 additions & 3 deletions shvatka/tgbot/dialogs/game_manage/getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from dishka import AsyncContainer
from telegraph import Telegraph

from shvatka.common.url_factory import UrlFactory
from shvatka.core.models import dto
from shvatka.core.services import game
from shvatka.core.services.game import get_authors_games, get_completed_games
Expand All @@ -30,14 +31,13 @@ async def get_completed_game(dao: HolderDao, dialog_manager: DialogManager, **_)
dialog_manager.dialog_data.get("game_id", None) or dialog_manager.start_data["game_id"]
)
dishka: AsyncContainer = dialog_manager.middleware_data["dishka_container"]
bot = await dishka.get(Bot)
username = (await bot.get_me()).username
url_factory = await dishka.get(UrlFactory)
return {
"game": await game.get_game(
id_=game_id,
dao=dao.game,
),
"webapp_url": f"https://t.me/{username}/games?startapp={game_id}",
"webapp_url": url_factory.get_game_id_web_url(game_id),
}


Expand Down
3 changes: 2 additions & 1 deletion shvatka/tgbot/main_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dishka.integrations.aiogram import setup_dishka
from redis.asyncio import Redis

from shvatka.common.factory import TelegraphProvider, DCFProvider
from shvatka.common.factory import TelegraphProvider, DCFProvider, UrlProvider
from shvatka.core.interfaces.clients.file_storage import FileStorage
from shvatka.core.utils.key_checker_lock import KeyCheckerFactory
from shvatka.core.views.game import GameLogWriter, GameView, GameViewPreparer, OrgNotifier
Expand Down Expand Up @@ -59,6 +59,7 @@ def get_bot_specific_providers() -> list[Provider]:
GameToolsProvider(),
UserGetterProvider(),
LockProvider(),
UrlProvider(),
]


Expand Down
3 changes: 2 additions & 1 deletion tests/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from shvatka.api.dependencies import AuthProvider, ApiConfigProvider, PlayerProvider, TeamProvider
from shvatka.common import Paths
from shvatka.common.factory import DCFProvider, TelegraphProvider
from shvatka.common.factory import DCFProvider, TelegraphProvider, UrlProvider
from shvatka.core.interfaces.clients.file_storage import FileStorage, FileGateway
from shvatka.core.interfaces.scheduler import Scheduler
from shvatka.core.utils.key_checker_lock import KeyCheckerFactory
Expand Down Expand Up @@ -69,6 +69,7 @@ async def dishka():
MockMessageManagerProvider(),
LockProvider(),
DCFProvider(),
UrlProvider(),
TelegraphProvider(),
GamePlayProvider(),
GameToolsProvider(),
Expand Down

0 comments on commit 2d844a4

Please sign in to comment.