From f3d6244a175ab5b22992b5100b3cc4416118f435 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Sat, 4 May 2024 13:47:07 -0400 Subject: [PATCH] fix(aurora): optimizing the from_sql method --- aurora/models.py | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/aurora/models.py b/aurora/models.py index 694c9fd..dca0422 100644 --- a/aurora/models.py +++ b/aurora/models.py @@ -27,36 +27,17 @@ class Moderation(BaseModel): def __str__(self): return f"{self.moderation_type} {self.target_type} {self.target_id} {self.reason}" - async def from_sql(self, moderation_id: int, guild: Guild): - """""" - database = connect() - cursor = database.cursor() - + @classmethod + def from_sql(cls, moderation_id: int, guild: Guild): query = f"SELECT * FROM moderation_{guild.id} WHERE moderation_id = ?;" - cursor.execute(query, (moderation_id,)) - result = cursor.fetchone() - cursor.close() - database.close() + with connect() as database, database.cursor() as cursor: + cursor.execute(query, (moderation_id,)) + result = cursor.fetchone() - if result: - ( - self.moderation_id, - self.timestamp, - self.moderation_type, - self.target_type, - self.target_id, - self.moderator_id, - self.role_id, - self.duration, - self.end_timestamp, - self.reason, - self.resolved, - self.resolved_by, - self.resolve_reason, - self.expired, - self.changes, - self.metadata, - ) = result[0:16] + if result: + moderation_data = dict(zip(cls.model_fields, result)) + moderation = cls(**moderation_data) + return moderation - return self + return None