wag-managment-api-service-v.../databases/sql_models/response_model.py

44 lines
1.2 KiB
Python

class AlchemyResponse:
"""
alchemy_object = [AlchemyObject].filter_non_deleted() -> AlchemyResponse
alchemy_object.get(1) -> Get the first object in the list
alchemy_object.data -> Get the list of objects
alchemy_object.count -> Get the count of objects
"""
def __init__(self, query, first: bool = False):
self.first = first
self.__query = query
def get(self, index: int):
count = self.count
if count and not index > count:
return self.data[index - 1]
return None
@property
def data(self):
if self.first:
try:
return self.__query.first()
except Exception as e:
err = e
self.__query.session.expunge_all()
self.__query.session.rollback()
return None
try:
return self.__query.all()
except Exception as e:
err = e
self.__query.session.expunge_all()
self.__query.session.rollback()
return []
@property
def count(self):
return self.__query.count()
@property
def query(self):
return self.__query