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

32 lines
818 B
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:
return self.__query.first()
return self.__query.all()
@property
def count(self):
return self.__query.count()
@property
def query(self):
return self.__query