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() )