alcehmy and event functions updated
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from sqlalchemy import SQLColumnExpression
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
|
||||
from api_validations.validations_request import ListOptions
|
||||
@@ -90,24 +91,51 @@ class FilterAttributes:
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_not_expired_query_arg(cls, *arg, expired=True):
|
||||
def add_new_arg_to_args(cls, args_list, argument, value):
|
||||
new_arg_list = list(
|
||||
args_ for args_ in list(args_list) if isinstance(args_, SQLColumnExpression)
|
||||
)
|
||||
if not any(True for arg in new_arg_list if arg.left.key == argument):
|
||||
new_arg_list.append(value)
|
||||
return tuple(new_arg_list)
|
||||
|
||||
@classmethod
|
||||
def get_not_expired_query_arg(cls, arg):
|
||||
"""Add expiry_starts and expiry_ends to the query."""
|
||||
from api_library.date_time_actions.date_functions import system_arrow
|
||||
|
||||
if expired:
|
||||
arg_add = (
|
||||
*arg[0],
|
||||
system_arrow.get(cls.expiry_ends) >= system_arrow.now(),
|
||||
system_arrow.now() >= system_arrow.get(cls.expiry_starts),
|
||||
)
|
||||
return arg_add
|
||||
return arg[0]
|
||||
arg = cls.add_new_arg_to_args(
|
||||
arg, "expiry_ends", cls.expiry_ends >= str(system_arrow.now())
|
||||
)
|
||||
arg = cls.add_new_arg_to_args(
|
||||
arg, "expiry_starts", cls.expiry_starts <= str(system_arrow.now())
|
||||
)
|
||||
return arg
|
||||
|
||||
@classmethod
|
||||
def filter_by_all(cls, **kwargs):
|
||||
def select_only(
|
||||
cls, *args, select_args: list, order_by=None, limit=None, system=False
|
||||
):
|
||||
if not system:
|
||||
args = cls.add_new_arg_to_args(
|
||||
args, "is_confirmed", cls.is_confirmed == True
|
||||
)
|
||||
args = cls.get_not_expired_query_arg(args)
|
||||
query = cls._query().filter(*args).with_entities(*select_args)
|
||||
if order_by is not None:
|
||||
query = query.order_by(order_by)
|
||||
if limit:
|
||||
query = query.limit(limit)
|
||||
return AlchemyResponse(query=query, first=False)
|
||||
|
||||
@classmethod
|
||||
def filter_by_all(cls, system=False, **kwargs):
|
||||
"""
|
||||
Filters all the records regardless of is_deleted, is_confirmed.
|
||||
"""
|
||||
if "is_confirmed" not in kwargs and not system:
|
||||
kwargs["is_confirmed"] = True
|
||||
kwargs.pop("system", None)
|
||||
query = cls._query().filter_by(**kwargs)
|
||||
if cls.filter_attr:
|
||||
filter_list = cls.get_filter_attributes()
|
||||
@@ -116,18 +144,27 @@ class FilterAttributes:
|
||||
return AlchemyResponse(query=query, first=False)
|
||||
|
||||
@classmethod
|
||||
def filter_by_one(cls, **kwargs):
|
||||
def filter_by_one(cls, system=False, **kwargs):
|
||||
"""
|
||||
Filters one record regardless of is_deleted, is_confirmed.
|
||||
"""
|
||||
if "is_confirmed" not in kwargs and not system:
|
||||
kwargs["is_confirmed"] = True
|
||||
kwargs.pop("system", None)
|
||||
query = cls._query().filter_by(**kwargs)
|
||||
return AlchemyResponse(query=query, first=True)
|
||||
|
||||
@classmethod
|
||||
def filter_all(cls, *args, expired: bool = False):
|
||||
def filter_all(cls, *args, system=False):
|
||||
"""
|
||||
Filters all the records regardless of is_deleted, is_confirmed.
|
||||
"""
|
||||
if not system:
|
||||
args = cls.add_new_arg_to_args(
|
||||
args, "is_confirmed", cls.is_confirmed == True
|
||||
)
|
||||
args = cls.get_not_expired_query_arg(args)
|
||||
|
||||
query = cls._query().filter(*args)
|
||||
if cls.filter_attr:
|
||||
filter_list = cls.get_filter_attributes()
|
||||
@@ -136,12 +173,16 @@ class FilterAttributes:
|
||||
return AlchemyResponse(query=query, first=False)
|
||||
|
||||
@classmethod
|
||||
def filter_one(cls, *args, expired: bool = False):
|
||||
def filter_one(cls, *args, system=False, expired: bool = False):
|
||||
"""
|
||||
Filters one record regardless of is_deleted, is_confirmed.
|
||||
"""
|
||||
arg = cls.get_not_expired_query_arg(args, expired=expired)
|
||||
query = cls._query().filter(*arg)
|
||||
if not system:
|
||||
args = cls.add_new_arg_to_args(
|
||||
args, "is_confirmed", cls.is_confirmed == True
|
||||
)
|
||||
args = cls.get_not_expired_query_arg(args)
|
||||
query = cls._query().filter(*args)
|
||||
return AlchemyResponse(query=query, first=True)
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user