Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def __init__(self, _db=None, _instance=None):
- """A class that performs queries using SQLAlchemy based on specified models.
- Args:
- _db: The SQLAlchemy database session.
- """
- self._db = _db
- self._instance = _instance
- def get_mode_fields(self, fields: str, test_mode_switch: str = "off") -> str | None:
- """Return fields using the specified model.
- Args:
- model: The SQLAlchemy model class to use for the query.
- test_mode_switch (str, optional): Value on/off, Defaults to "off" .
- Returns:
- Query: string attr.
- """
- try:
- test_mode = util.strtobool(test_mode_switch)
- return self.SECONDARY_FIELDS.get(fields) if test_mode else self.MAIN_FIELDS.get(fields)
- except (ValueError, AttributeError):
- return self.MAIN_FIELDS.get(fields)
- def get_inner_instance(self, test_mode_switch: str = "off"):
- """
- Return instance using the specified model.
- Args:
- model: The SQLAlchemy model class to use for the query.
- Returns:
- Query: The SQLAlchemy query object.
- """
- if self._instance:
- try:
- test_mode = util.strtobool(test_mode_switch)
- return (
- self.SECONDARY_MODELS.get(self._instance.__tablename__)
- if test_mode
- else self.MAIN_MODELS.get(self._instance.__tablename__)
- )
- except (ValueError, AttributeError):
- return self.MAIN_MODELS.get(self._instance.__tablename__)
- return None
- @classmethod
- def get_instance(cls, _instance, test_mode_switch: str = "off"):
- """
- Return instance using the specified model.
- Args:
- model: The SQLAlchemy model class to use for the query.
- Returns:
- Query: The SQLAlchemy query object.
- """
- if _instance:
- try:
- test_mode = util.strtobool(test_mode_switch)
- return (
- cls.SECONDARY_MODELS.get(_instance.__tablename__)
- if test_mode
- else cls.MAIN_MODELS.get(_instance.__tablename__)
- )
- except (ValueError, AttributeError):
- return cls.MAIN_MODELS.get(_instance.__tablename__)
- return None
- def query(self, test_mode_switch: str = "off"):
- """
- Executes a query using the specified model.
- Args:
- model: The SQLAlchemy model class to use for the query.
- Returns:
- Query: The SQLAlchemy query object.
- """
- if not self._instance:
- try:
- test_mode = util.strtobool(test_mode_switch)
- return (
- self._db.query(self.SECONDARY_MODELS.get(self._instance.__tablename__))
- if test_mode
- else self._db.query(self.MAIN_MODELS.get(self._instance.__tablename__))
- )
- except (ValueError, AttributeError):
- return self._db.query(self.MAIN_MODELS.get(self._instance.__tablename__))
- return None
- @staticmethod
- def is_test_mode(test_mode_switch: str = "off") -> bool | int:
- """Check test_mode_switch
- Args:
- test_mode_switch (str, optional): Defaults to "off".
- Returns:
- bool: True/False
- """
- try:
- test_mode = util.strtobool(test_mode_switch)
- return test_mode
- except (ValueError, AttributeError):
- return False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement