From 4a3d2746fbf5731518e7e3882337ca0c76c5fe2d Mon Sep 17 00:00:00 2001 From: Ettore Dreucci Date: Wed, 14 May 2025 23:27:50 +0200 Subject: [PATCH] More refactoring --- bot.py | 6 +++--- gates.py | 2 +- users.py | 27 +++++++++++++++++++++------ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/bot.py b/bot.py index b363762..31ebfb5 100644 --- a/bot.py +++ b/bot.py @@ -1,5 +1,6 @@ from telegram import Update from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes +from datetime import datetime from config import BotConfig from gates import Gates from users import Users, Credential, Role @@ -10,11 +11,10 @@ gates = Gates() users = Users() async def post_init(application: Application) -> None: - bot_commands = bot_config.commands - await application.bot.set_my_commands(bot_commands) await application.bot.set_my_name(bot_config.name) await application.bot.set_my_description(bot_config.description) await application.bot.set_my_short_description(bot_config.short_description) + await application.bot.set_my_commands(bot_config.commands) async def updateuser(update: Update, context: ContextTypes.DEFAULT_TYPE): user_id = str(update.effective_user.id) @@ -94,7 +94,7 @@ async def approve(update: Update, context: ContextTypes.DEFAULT_TYPE): user_id = context.args[0] gate = context.args[1] expires_at = context.args[2] - users.grant_access(user_id, gate, expires_at, grantor_id=approver_id) + users.grant_access(user_id, gate, datetime.fromisoformat(expires_at.replace("Z", "+00:00")), grantor_id=approver_id) await update.message.reply_text(f"Access to {gate} granted to user {user_id}.") try: await context.bot.send_message(chat_id=user_id, text=f"Access granted to gate {gate} up to {expires_at}") diff --git a/gates.py b/gates.py index 9cd1592..c1fe752 100644 --- a/gates.py +++ b/gates.py @@ -30,7 +30,7 @@ class Gates: self._json_path: str = json_path self._gates: dict[str, _Gate] = self._load_gates() - def _load_gates(self): + def _load_gates(self) -> dict[str, _Gate]: try: with open(self._json_path, "r") as file: gates_data = json.load(file) diff --git a/users.py b/users.py index 7e7e187..57f43ee 100644 --- a/users.py +++ b/users.py @@ -9,7 +9,7 @@ class Role(Enum): GUEST = "guest" class _Grant: - def __init__(self, grantor: str, expires_at: datetime, granted_at: datetime = datetime.now(), last_used_at: datetime = None, status: int | Status = Status.ENABLED): + def __init__(self, grantor: str, expires_at: datetime, granted_at: datetime = datetime.now(), last_used_at: datetime = None, status: Status = Status.ENABLED): self._grantor: str = grantor self._granted_at: datetime = granted_at self._expires_at = expires_at @@ -50,14 +50,14 @@ class _Grant: return self._status class _User: - def __init__(self, id: str, username: str, fullname: str, role: str | Role = Role.GUEST, credentials: dict = None, grants: dict = None, status: int | Status = Status.ENABLED): + def __init__(self, id: str, username: str, fullname: str, role: Role = Role.GUEST, credentials: Credential = Credential("", ""), grants: dict = None, status: Status = Status.ENABLED): self._id: str = id self._username: str = username self._fullname: str = fullname self._role: Role = role if isinstance(role, Role) else Role(role) - self._credentials: Credential = Credential(**credentials) - self._grants: dict[str, _Grant] = {gate: _Grant(**grant) for gate, grant in grants.items()} if grants else {} - self._status: Status = status if isinstance(status, Status) else Status(status) + self._credentials: Credential = credentials + self._grants: dict[str, _Grant] = {gate:_Grant(grant.get("grantor", ""), datetime.fromisoformat(grant.get("expires_at").replace("Z", "+00:00"))) for gate, grant in grants.items()} if grants else {} + self._status: Status = status def __dict__(self): return { @@ -133,7 +133,22 @@ class Users: def _load_users(self) -> dict[str, _User]: try: with open(self._json_path, "r") as f: - return {uid: _User(**info) for uid, info in json.load(f)} + users = {} + for uid, info in json.load(f).items(): + try: + user = _User( + id=uid, + username=info.get("username", ""), + fullname=info.get("fullname", ""), + role=info.get("role", Role.GUEST), + credentials=Credential(info.get("credentials", {}).get("username", ""), info.get("credentials", {}).get("password", "")), + grants={gate: info for gate, info in info.get("grants", {}).items()}, + status=info.get("status", Status.ENABLED) + ) + users[uid] = user + except: + continue + return users except Exception: return {}