Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
refactor: better name convention
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 7, 2025
1 parent e211af1 commit ca9ec33
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 31 deletions.
4 changes: 2 additions & 2 deletions schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
],
"default": null,
"description": "Ghi chú chủ sở hữu (phù hợp khi dùng notify ai đó)",
"description": "Ghi chú chủ sở hữu (phù hợp khi dùng nhắc ai đó với lựa chọn notifications)",
"examples": [
"@kevinnitro",
"dad"
Expand Down Expand Up @@ -169,7 +169,7 @@
},
"requestTimeout": {
"default": 10,
"description": "Thời gian (s) để gửi request đến server API và gửi message notify",
"description": "Thời gian (s) để gửi request đến server API và gửi notify message",
"title": "Requesttimeout",
"type": "integer"
},
Expand Down
2 changes: 1 addition & 1 deletion src/check_phat_nguoi/config/dto/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConfigDTO(BaseModel):
frozen=True,
)
request_timeout: int = Field(
description="Thời gian (s) để gửi request đến server API và gửi message notify",
description="Thời gian (s) để gửi request đến server API và gửi notify message",
default=10,
frozen=True,
)
Expand Down
4 changes: 2 additions & 2 deletions src/check_phat_nguoi/config/dto/notifications/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .base_engine import BaseNotificationEngineDTO
from .base_notify import BaseNotificationDTO
from .base_notification import BaseNotificationDTO
from .telegram_engine import TelegramNotificationEngineDTO
from .telegram_notify import TelegramNotificationDTO
from .telegram_notification import TelegramNotificationDTO

__all__ = [
"BaseNotificationEngineDTO",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
from re import match as re_match

from pydantic import ConfigDict, Field, field_validator
from pydantic.alias_generators import to_camel
from pydantic import Field, field_validator

from .base_engine import BaseNotificationEngineDTO


class TelegramNotificationEngineDTO(BaseNotificationEngineDTO):
model_config = ConfigDict(alias_generator=to_camel)

bot_token: str = Field(
description="Bot token Telegram",
examples=["2780473231:weiruAShGUUx4oLOMoUhd0GiREXSZcCq-uB"],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pydantic import Field

from .base_notify import BaseNotificationDTO
from .base_notification import BaseNotificationDTO
from .telegram_engine import TelegramNotificationEngineDTO


Expand Down
2 changes: 1 addition & 1 deletion src/check_phat_nguoi/config/dto/plate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class PlateInfoDTO(BaseModel):
examples=["60A64685", "98-A-56604", "30-F88251", "59XB-00000"],
)
owner: str | None = Field(
description="Ghi chú chủ sở hữu (phù hợp khi dùng notify ai đó)",
description="Ghi chú chủ sở hữu (phù hợp khi dùng nhắc ai đó với lựa chọn notifications)",
title="Ghi chú chủ sở hữu",
examples=["@kevinnitro", "dad"],
default=None,
Expand Down
7 changes: 4 additions & 3 deletions src/check_phat_nguoi/notify/engines/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from check_phat_nguoi.config import TelegramNotificationEngineDTO
from check_phat_nguoi.constants import SEND_MESSAGE_API_URL_TELEGRAM as API_URL

from ..markdown_msg import MessagesModel
from ..markdown_message import MessagesModel
from .base import BaseNotificationEngine

logger = getLogger(__name__)
Expand Down Expand Up @@ -53,9 +53,10 @@ async def _send_message(message: str, plate: str) -> None:
except Exception as e:
logger.error(e)

# TODO: the violation name conventno is not match with param message :v
tasks = (
_send_message(vio_msg, message.plate)
_send_message(violation, message.plate)
for message in messages
for vio_msg in message.vio_msgs
for violation in message.violations
)
await asyncio.gather(*tasks)
4 changes: 4 additions & 0 deletions src/check_phat_nguoi/notify/markdown_message/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .markdown_message import MarkdownMessage
from .models import *

__all__ = ["MarkdownMessage"]
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .models import MessagesModel


class MdMsg:
class MarkdownMessage:
def __init__(self, plate_info: PlateInfoModel) -> None:
self.plate_info: PlateInfoModel = plate_info

Expand Down Expand Up @@ -56,8 +56,8 @@ def _format_message(self) -> tuple[str, ...]:
]
)

def generate_msg(self) -> MessagesModel:
def generate_message(self) -> MessagesModel:
return MessagesModel(
plate=self.plate_info.plate,
vio_msgs=self._format_message(),
violations=self._format_message(),
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

class MessagesModel(BaseModel):
plate: str = Field(description="Biển số")
vio_msgs: tuple[str, ...] = Field(
violations: tuple[str, ...] = Field(
description="List chứa các string chứa các thông tin cụ thể về lỗi vi phạm sau"
)
4 changes: 0 additions & 4 deletions src/check_phat_nguoi/notify/markdown_msg/__init__.py

This file was deleted.

24 changes: 15 additions & 9 deletions src/check_phat_nguoi/notify/send_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,34 @@
from check_phat_nguoi.context import plates_context

from .engines.telegram import TelegramNotificationEngine
from .markdown_msg import MdMsg, MessagesModel
from .markdown_message import MarkdownMessage, MessagesModel


class SendNotifications:
def __init__(self) -> None:
self._message_dict: tuple[MessagesModel, ...] = tuple(
MdMsg(plate_info).generate_msg() for plate_info in plates_context.plates
)
self._md_messages: tuple[MessagesModel, ...]
self._tele: TelegramNotificationEngine

async def _send_msgs(self, notification: BaseNotificationDTO) -> None:
async def _send_messages(self, notification: BaseNotificationDTO) -> None:
if isinstance(notification, TelegramNotificationDTO):
await self._tele.send(notification.telegram, self._message_dict)
await self._tele.send(notification.telegram, self._md_messages)

async def send(self) -> None:
enabled_notifications: filter[BaseNotificationDTO] = filter(
lambda notify: notify.enabled, config.notifications
enabled_notifications: tuple[TelegramNotificationDTO, ...] = tuple(
notification
for notification in config.notifications
if notification.enabled
)
if len(enabled_notifications) == 0:
return
self._md_messages = tuple(
MarkdownMessage(plate_info).generate_message()
for plate_info in plates_context.plates
)
async with TelegramNotificationEngine() as self._tele:
await gather(
*(
self._send_msgs(notification)
self._send_messages(notification)
for notification in enabled_notifications
)
)

0 comments on commit ca9ec33

Please sign in to comment.