Files
atst/atst/domain/__init__.py
dandds eaeeed0b05 Added BaseDomainClass with a get method.
The BaseDomainClass.get can accept any number of keyword arguments and
will add a filter to the query for each kwarg. This will allow the
caller to scope the query as needed with kwargs.
2019-04-16 14:18:53 -04:00

26 lines
743 B
Python

from sqlalchemy.orm.exc import NoResultFound
from atst.database import db
from atst.domain.exceptions import NotFoundError
class BaseDomainClass(object):
model = None
resource_name = None
@classmethod
def get(cls, resource_id, **kwargs):
base_query = db.session.query(cls.model).filter(cls.model.id == resource_id)
if getattr(cls.model, "deleted", False):
base_query = base_query.filter(cls.model.deleted == False)
for col, val in kwargs.items():
base_query = base_query.filter(getattr(cls.model, col) == val)
try:
resource = base_query.one()
return resource
except NoResultFound:
raise NotFoundError(cls.resource_name)