-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
👌 IMPROVE: Move default user caching to StorageBackend
#5460
Changes from 3 commits
d0b5e06
29c3d0c
5383204
8fb4dfd
bd11421
d9e6355
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,7 @@ | |||||||||
BackendQueryBuilder, | ||||||||||
BackendUserCollection, | ||||||||||
) | ||||||||||
from aiida.orm.users import User | ||||||||||
from aiida.repository.backend.abstract import AbstractRepositoryBackend | ||||||||||
|
||||||||||
__all__ = ('StorageBackend',) | ||||||||||
|
@@ -84,6 +85,7 @@ def __init__(self, profile: 'Profile') -> None: | |||||||||
""" | ||||||||||
from aiida.orm.autogroup import AutogroupManager | ||||||||||
self._profile = profile | ||||||||||
self._default_user: Optional['User'] = None | ||||||||||
self._autogroup = AutogroupManager(self) | ||||||||||
|
||||||||||
@abc.abstractmethod | ||||||||||
|
@@ -161,6 +163,21 @@ def nodes(self) -> 'BackendNodeCollection': | |||||||||
def users(self) -> 'BackendUserCollection': | ||||||||||
"""Return the collection of users""" | ||||||||||
|
||||||||||
@property | ||||||||||
def default_user(self) -> Optional['User']: | ||||||||||
"""Return the default user for the profile, if it has been created. | ||||||||||
|
||||||||||
This is cached, since it is a frequently used operation, for creating other entities. | ||||||||||
""" | ||||||||||
from aiida.orm import QueryBuilder, User | ||||||||||
|
||||||||||
if self._default_user is None and self.profile.default_user_email: | ||||||||||
query = QueryBuilder(self).append(User, filters={'email': self.profile.default_user_email}) | ||||||||||
results = query.all(flat=True) | ||||||||||
if results: | ||||||||||
self._default_user = results[0] | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was line 178 being tested? Shouldn't
Suggested change
|
||||||||||
return self._default_user | ||||||||||
|
||||||||||
@abc.abstractmethod | ||||||||||
def query(self) -> 'BackendQueryBuilder': | ||||||||||
"""Return an instance of a query builder implementation for this backend""" | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ def _entity_base_cls() -> Type['User']: | |
|
||
def __init__(self, entity_class: Type['User'], backend: Optional['StorageBackend'] = None) -> None: | ||
super().__init__(entity_class=entity_class, backend=backend) | ||
self._default_user: Optional[User] = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The overridden constructor is now pointless, might as well get rid of it entirely |
||
|
||
def get_or_create(self, email: str, **kwargs) -> Tuple[bool, 'User']: | ||
"""Get the existing user with a given email address or create an unstored one | ||
|
@@ -48,23 +47,7 @@ def get_or_create(self, email: str, **kwargs) -> Tuple[bool, 'User']: | |
|
||
def get_default(self) -> Optional['User']: | ||
"""Get the current default user""" | ||
if self._default_user is None: | ||
email = self.backend.profile.default_user_email | ||
if not email: | ||
self._default_user = None | ||
|
||
try: | ||
self._default_user = self.get(email=email) | ||
except (exceptions.MultipleObjectsError, exceptions.NotExistent): | ||
self._default_user = None | ||
|
||
return self._default_user | ||
|
||
def reset(self) -> None: | ||
""" | ||
Reset internal caches (default user). | ||
""" | ||
self._default_user = None | ||
return self.backend.default_user | ||
|
||
|
||
class User(entities.Entity['BackendUser']): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the change from
user or
touser if user else
necessary? Does that change anything? I think theor
notation is clearer and is also used in the line before.