Skip to content

Commit

Permalink
chore: Refactor Database.py to improve setting existence check
Browse files Browse the repository at this point in the history
  • Loading branch information
TheophileDiot committed Jun 17, 2024
1 parent 89abb65 commit c100e1c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/common/db/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,9 +394,20 @@ def is_setting(self, setting: str, *, multisite: bool = False) -> bool:
"""Check if the setting exists in the database and optionally if it's multisite"""
with self.__db_session() as session:
try:
if multisite:
return session.query(Settings).filter_by(id=setting, context="multisite").first() is not None
return session.query(Settings).filter_by(id=setting).first() is not None
multiple = False
if self.suffix_rx.search(setting):
setting = setting.rsplit("_", 1)[0]
multiple = True

db_setting = session.query(Settings).filter_by(id=setting).first()

if not db_setting:
return False
elif multisite and db_setting.context != "multisite":
return False
elif multiple and db_setting.multiple is None:
return False
return True
except (ProgrammingError, OperationalError):
return False

Expand Down

0 comments on commit c100e1c

Please sign in to comment.