Skip to content

Commit

Permalink
Changes to remove __dict__ usage
Browse files Browse the repository at this point in the history
  • Loading branch information
DeveloperAnonymous committed Feb 16, 2024
1 parent 6757ad6 commit 84e6a22
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
1 change: 1 addition & 0 deletions base_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
PREFIX = "!"

# Database Configs
DB_PROTOCOL = "mongodb"
DB_HOST = "localhost"
DB_NAME = "database_name"
DB_USER = "Cool Name"
Expand Down
2 changes: 1 addition & 1 deletion bot/botcommands/bot_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def get_spam_configs(self, ctx: commands.Context):
"""Affiche la configuration du spam."""
spam_config = self.configs_service.find_or_create_spam_configs()

await ctx.send(spam_config.__dict__)
await ctx.send(spam_config.__getstate__())

@commands.command(name="editspamconfig", aliases=["esc"])
@commands.guild_only()
Expand Down
2 changes: 1 addition & 1 deletion bot/botcommands/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async def create_ticket(self, ctx: Context):
"""
await ctx.channel.send(configs.TICKET_VIEW_MESSAGE, view=TicketOpeningInteraction())

@commands.command()
@commands.command(aliases=["stalk"])
@has_at_least_role(configs.ADMIN_ROLE)
async def search(self, ctx: Context, user: discord.User):
"""
Expand Down
10 changes: 9 additions & 1 deletion bot/db/models/bot_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class GlobalConfig:
"""

__slots__ = ("_id",)
_id: str

def __init__(self, _id: str) -> None:
"""
Expand All @@ -28,6 +27,15 @@ def config_id(self):
"""The id of the config."""
return self._id

def __getstate__(self):
state = {}
for cls in self.__class__.__mro__:
if hasattr(cls, "__slots__"):
for slot in cls.__slots__:
if hasattr(self, slot):
state[slot] = getattr(self, slot)
return state


class SpamConfigs(GlobalConfig):
"""
Expand Down
13 changes: 12 additions & 1 deletion bot/db/models/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Entity:
The time the entity was last updated.
"""

__slots__ = ("_id", "created_at", "updated_at")

def __init__(self, _id: int, created_at: datetime = None, updated_at: datetime = None):
self._id = _id
self.created_at = created_at
Expand All @@ -43,7 +45,7 @@ def save(self, upsert=True):
else:
self.updated_at = datetime.utcnow()

return self.service.update_one({"_id": self._id}, self.__dict__, upsert=upsert)
return self.service.update_one({"_id": self._id}, self.__getstate__(), upsert=upsert)

def delete(self):
"""Delete the entity from the database."""
Expand All @@ -57,6 +59,15 @@ def _load(self):
self.created_at = entity.get("created_at")
self.updated_at = entity.get("updated_at")

def __getstate__(self):
state = {}
for cls in self.__class__.__mro__:
if hasattr(cls, "__slots__"):
for slot in cls.__slots__:
if hasattr(self, slot):
state[slot] = getattr(self, slot)
return state

@property
@abstractmethod
def service(self) -> BaseService:
Expand Down
16 changes: 9 additions & 7 deletions bot/db/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ class AdeptMember(Entity):
The program of the member.
"""

name: str
email: str
is_student: bool
is_teacher: bool
is_it_student: bool
student_id: int
program: str
__slots__ = (
"name",
"email",
"is_student",
"is_teacher",
"is_it_student",
"student_id",
"program",
)

def __init__(
self,
Expand Down
4 changes: 2 additions & 2 deletions bot/db/services/base_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def conn(self) -> Database:
"""Return a connection to the database."""
if self.__conn is None:
self.__conn = MongoClient(
"mongodb+srv://"
f"{configs.DB_PROTOCOL}://"
+ f"{configs.DB_USER}:{configs.DB_PASSWORD}"
+ f"@{configs.DB_HOST}/"
+ "?retryWrites=true&w=majority&ssl=true",
+ "?retryWrites=true&w=majority",
).get_database(configs.DB_NAME)

return self.__conn
Expand Down
4 changes: 2 additions & 2 deletions bot/db/services/configs_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def find_or_create_spam_configs(self) -> SpamConfigs:

spam_config = SpamConfigs()
if data is None:
self.insert_one(spam_config.__dict__)
self.insert_one(spam_config.__getstate__())

return spam_config

Expand All @@ -34,7 +34,7 @@ def update_configs(self, config: GlobalConfig):
`config` : GlobalConfig
The config to update.
"""
return self.update_one({"_id": config.config_id}, config.__dict__, upsert=True)
return self.update_one({"_id": config.config_id}, config.__getstate__(), upsert=True)

@property
def collection_name(self):
Expand Down

0 comments on commit 84e6a22

Please sign in to comment.