Skip to content

Commit

Permalink
domains: add find by id function
Browse files Browse the repository at this point in the history
  • Loading branch information
carlinmack committed Nov 7, 2024
1 parent e865a3c commit d37027a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions invenio_accounts/datastore.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand Down

0 comments on commit d37027a

Please sign in to comment.