From 6f9ad3c0c68a8458c61fcd4e67cceea05e046617 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 a7a554db..51ad6534 100644 --- a/invenio_accounts/datastore.py +++ b/invenio_accounts/datastore.py @@ -106,14 +106,23 @@ 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 db.session.query(self.role_model).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 ( db.session.query(Domain) - .filter_by(domain=domain) + .filter_by(clause) .options(joinedload(Domain.category_name)) .one_or_none() )