Skip to content

Commit

Permalink
domains: extend find_domain to accept ID
Browse files Browse the repository at this point in the history
  • Loading branch information
carlinmack committed Nov 7, 2024
1 parent b29a85d commit 72e7cf1
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 @@ -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(clause)
.options(joinedload(Domain.category_name))
.one_or_none()
)
Expand Down

0 comments on commit 72e7cf1

Please sign in to comment.