Advertisement
Mochinov

Untitled

Jun 11th, 2024
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1.  
  2. def __init__(self, _db=None, _instance=None):
  3. """A class that performs queries using SQLAlchemy based on specified models.
  4.  
  5. Args:
  6. _db: The SQLAlchemy database session.
  7. """
  8. self._db = _db
  9. self._instance = _instance
  10.  
  11. def get_mode_fields(self, fields: str, test_mode_switch: str = "off") -> str | None:
  12. """Return fields using the specified model.
  13.  
  14. Args:
  15. model: The SQLAlchemy model class to use for the query.
  16. test_mode_switch (str, optional): Value on/off, Defaults to "off" .
  17.  
  18. Returns:
  19. Query: string attr.
  20. """
  21. try:
  22. test_mode = util.strtobool(test_mode_switch)
  23. return self.SECONDARY_FIELDS.get(fields) if test_mode else self.MAIN_FIELDS.get(fields)
  24.  
  25. except (ValueError, AttributeError):
  26. return self.MAIN_FIELDS.get(fields)
  27.  
  28. def get_inner_instance(self, test_mode_switch: str = "off"):
  29. """
  30. Return instance using the specified model.
  31.  
  32. Args:
  33. model: The SQLAlchemy model class to use for the query.
  34.  
  35. Returns:
  36. Query: The SQLAlchemy query object.
  37. """
  38. if self._instance:
  39. try:
  40. test_mode = util.strtobool(test_mode_switch)
  41. return (
  42. self.SECONDARY_MODELS.get(self._instance.__tablename__)
  43. if test_mode
  44. else self.MAIN_MODELS.get(self._instance.__tablename__)
  45. )
  46.  
  47. except (ValueError, AttributeError):
  48. return self.MAIN_MODELS.get(self._instance.__tablename__)
  49.  
  50. return None
  51.  
  52. @classmethod
  53. def get_instance(cls, _instance, test_mode_switch: str = "off"):
  54. """
  55. Return instance using the specified model.
  56.  
  57. Args:
  58. model: The SQLAlchemy model class to use for the query.
  59.  
  60. Returns:
  61. Query: The SQLAlchemy query object.
  62. """
  63. if _instance:
  64. try:
  65. test_mode = util.strtobool(test_mode_switch)
  66. return (
  67. cls.SECONDARY_MODELS.get(_instance.__tablename__)
  68. if test_mode
  69. else cls.MAIN_MODELS.get(_instance.__tablename__)
  70. )
  71.  
  72. except (ValueError, AttributeError):
  73. return cls.MAIN_MODELS.get(_instance.__tablename__)
  74.  
  75. return None
  76.  
  77. def query(self, test_mode_switch: str = "off"):
  78. """
  79. Executes a query using the specified model.
  80.  
  81. Args:
  82. model: The SQLAlchemy model class to use for the query.
  83.  
  84. Returns:
  85. Query: The SQLAlchemy query object.
  86. """
  87. if not self._instance:
  88. try:
  89. test_mode = util.strtobool(test_mode_switch)
  90. return (
  91. self._db.query(self.SECONDARY_MODELS.get(self._instance.__tablename__))
  92. if test_mode
  93. else self._db.query(self.MAIN_MODELS.get(self._instance.__tablename__))
  94. )
  95.  
  96. except (ValueError, AttributeError):
  97. return self._db.query(self.MAIN_MODELS.get(self._instance.__tablename__))
  98.  
  99. return None
  100.  
  101. @staticmethod
  102. def is_test_mode(test_mode_switch: str = "off") -> bool | int:
  103. """Check test_mode_switch
  104.  
  105. Args:
  106. test_mode_switch (str, optional): Defaults to "off".
  107.  
  108. Returns:
  109. bool: True/False
  110. """
  111. try:
  112. test_mode = util.strtobool(test_mode_switch)
  113. return test_mode
  114.  
  115. except (ValueError, AttributeError):
  116. return False
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement