From d37027ab55c45aabeb24a023e4da1f5fe0ebfdf6 Mon Sep 17 00:00:00 2001 From: Carlin MacKenzie Date: Thu, 7 Nov 2024 10:02:36 +0100 Subject: [PATCH] domains: add find by id function --- invenio_accounts/datastore.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/invenio_accounts/datastore.py b/invenio_accounts/datastore.py index be4b799d..b5a9fc2e 100644 --- a/invenio_accounts/datastore.py +++ b/invenio_accounts/datastore.py @@ -104,13 +104,22 @@ def create_role(self, **kwargs): return role def find_role_by_id(self, role_id): - """Fetches roles searching by id.""" + """Fetches roles searching by ID.""" return self.role_model.query.filter_by(id=role_id).one_or_none() - def find_domain(self, domain): - """Find a domain.""" + def find_domain(self, domain_or_id): + """Find a domain by value or ID.""" + if isinstance(domain_or_id, str): + if domain_or_id.isdigit(): + clause = Domain.id == int(domain_or_id) + else: + clause = Domain.domain == domain_or_id + elif isinstance(domain_or_id, int): + clause = Domain.id == domain_or_id + else: + raise ValueError("Expected string or int, received:", type(domain_or_id)) return ( - Domain.query.filter_by(domain=domain) + self.db.session.query(Domain).filter(clause) .options(joinedload(Domain.category_name)) .one_or_none() )