-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #117 from Studio-Yandex-Practicum/feature/admin_co…
…mmands перенос команд в админку. ( как вариант).
- Loading branch information
Showing
10 changed files
with
178 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,107 @@ | ||
import re | ||
from functools import wraps | ||
from typing import Any, Generic, Protocol, TypeVar | ||
|
||
from apscheduler.schedulers.asyncio import AsyncIOScheduler | ||
from dependency_injector.wiring import Provide, inject | ||
from mmpy_bot import Message, Plugin, listen_to | ||
|
||
from src.bot.services.admin import AdminService | ||
from src.bot.services.matching import MatchingService | ||
from src.bot.services.notify_service import NotifyService | ||
from src.core.db.models import Admin | ||
from src.depends import Container | ||
|
||
ReturningT = TypeVar("ReturningT", covariant=True) | ||
|
||
class BotAdmin(Plugin): | ||
@listen_to("Admin", re.IGNORECASE) | ||
|
||
class Handler(Protocol, Generic[ReturningT]): | ||
async def __call__( | ||
self_: Any, # type: ignore ## for pyright | ||
self: Plugin, | ||
message: Message, | ||
*args: Any, | ||
) -> ReturningT: | ||
... | ||
|
||
|
||
def is_admin(fn: Handler[ReturningT]) -> Handler[ReturningT | None]: | ||
@wraps(fn) | ||
@inject | ||
async def admin(self, message: Message, admin_service: AdminService = Provide[Container.admin_service]) -> None: | ||
async def wrapper( | ||
self: Plugin, | ||
message: Message, | ||
*args: Any, | ||
admin_service: AdminService = Provide[Container.admin_service], | ||
) -> ReturningT | None: | ||
admin_instance = Admin(username=message.sender_name, user_id=message.user_id) | ||
if await admin_service.check_if_admin(message.user_id, admin_instance): | ||
self.driver.reply_to(message, "Привет, админ!") | ||
return await fn(self, message, *args) | ||
else: | ||
self.driver.reply_to(message, "Недостаточно прав!") | ||
return None | ||
|
||
return wrapper | ||
|
||
|
||
class BotAdmin(Plugin): | ||
@is_admin | ||
@listen_to("notify_all_users", re.IGNORECASE) | ||
@inject | ||
async def test_notify_all_users( | ||
self, message: Message, notify_service: NotifyService = Provide[Container.week_routine_service,] | ||
) -> None: | ||
"""Тестирование опроса по пятницам""" | ||
await notify_service.notify_all_users(plugin=self) | ||
|
||
@is_admin | ||
@listen_to("monday_message", re.IGNORECASE) | ||
@inject | ||
async def test_monday_message( | ||
self, message: Message, notify_service: NotifyService = Provide[Container.week_routine_service,] | ||
) -> None: | ||
"""Тестирование напоминания о встречах""" | ||
await notify_service.meeting_notifications(plugin=self) | ||
|
||
@is_admin | ||
@listen_to("wednesday_message", re.IGNORECASE) | ||
@inject | ||
async def test_wednesday_message( | ||
self, message: str, notify_service: NotifyService = Provide[Container.week_routine_service,] | ||
) -> None: | ||
"""Тестирование опроса по средам""" | ||
await notify_service.match_review_notifications(plugin=self) | ||
|
||
@is_admin | ||
@listen_to("match", re.IGNORECASE) | ||
@inject | ||
async def test( | ||
self, message: Message, matching_service: MatchingService = Provide[Container.matching_service] | ||
) -> None: | ||
"""Тестирование создания пар""" | ||
try: | ||
reply_text = await matching_service.run_matching() | ||
self.driver.reply_to(message, reply_text) | ||
except Exception as error: | ||
self.driver.reply_to(message, str(error)) | ||
|
||
@is_admin | ||
@listen_to("close", re.IGNORECASE) | ||
@inject | ||
async def test_closing_meetings( | ||
self, message: Message, matching_service: MatchingService = Provide[Container.matching_service] | ||
) -> None: | ||
"""Тестирование закрытия встреч""" | ||
try: | ||
await matching_service.run_closing_meetings() | ||
self.driver.reply_to(message, "Встречи закрыты") | ||
except Exception as error: | ||
self.driver.reply_to(message, str(error)) | ||
|
||
@is_admin | ||
@listen_to("stop_jobs", re.IGNORECASE) | ||
@inject | ||
def cancel_jobs(self, message: Message, scheduler: AsyncIOScheduler = Provide[Container.scheduler,]) -> None: | ||
"""Остановка всех jobs""" | ||
scheduler.shutdown() | ||
self.driver.reply_to(message, "All jobs cancelled.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,5 @@ | ||
import re | ||
|
||
from dependency_injector.wiring import Provide, inject | ||
from mmpy_bot import Message, Plugin, listen_to | ||
|
||
from src.bot.services.matching import MatchingService | ||
from src.depends import Container | ||
from mmpy_bot import Plugin | ||
|
||
|
||
class Matching(Plugin): | ||
@listen_to("/match", re.IGNORECASE) | ||
@inject | ||
async def test( | ||
self, message: Message, matching_service: MatchingService = Provide[Container.matching_service] | ||
) -> None: | ||
try: | ||
await matching_service.run_matching() | ||
self.driver.reply_to(message, "Создание пар завершено") | ||
except Exception as error: | ||
self.driver.reply_to(message, str(error)) | ||
|
||
@listen_to("/close", re.IGNORECASE) | ||
@inject | ||
async def test_closing_meetings( | ||
self, message: Message, matching_service: MatchingService = Provide[Container.matching_service] | ||
) -> None: | ||
try: | ||
await matching_service.run_closing_meetings() | ||
self.driver.reply_to(message, "Встречи закрыты") | ||
except Exception as error: | ||
self.driver.reply_to(message, str(error)) | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.