Skip to content

Commit

Permalink
ORM: Add the User.is_default property
Browse files Browse the repository at this point in the history
This is a useful shortcut to determine whether a `User` instance is the
current default user. The previous way of determing this was to retrieve
the default user from the collection `User.collection.get_default()` and
manually compare it with the `User` instance.
  • Loading branch information
sphuber committed Nov 12, 2023
1 parent 1d8ea2a commit a43c4cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
10 changes: 9 additions & 1 deletion aiida/orm/users.py
Expand Up @@ -11,7 +11,6 @@
from typing import TYPE_CHECKING, Optional, Tuple, Type

from aiida.common import exceptions
from aiida.common.lang import classproperty
from aiida.manage import get_manager

from . import entities
Expand Down Expand Up @@ -84,6 +83,15 @@ def normalize_email(email: str) -> str:
email = '@'.join([email_name, domain_part.lower()])
return email

@property
def is_default(self) -> bool:
"""Return whether the user is the default user.
:returns: Boolean, ``True`` if the user is the default, ``False`` otherwise.
"""
default_user = self.collection.get_default()
return default_user is not None and self.pk == default_user.pk

@property
def email(self) -> str:
return self._backend_entity.email
Expand Down
3 changes: 1 addition & 2 deletions aiida/transports/cli.py
Expand Up @@ -17,7 +17,6 @@
from aiida.cmdline.utils import echo
from aiida.cmdline.utils.decorators import with_dbenv
from aiida.common.exceptions import NotExistent
from aiida.manage import get_manager

TRANSPORT_PARAMS = []

Expand All @@ -40,7 +39,7 @@ def configure_computer_main(computer, user, **kwargs):
user = user or orm.User.collection.get_default()

echo.echo_report(f'Configuring computer {computer.label} for user {user.email}.')
if user.email != get_manager().get_profile().default_user_email:
if not user.is_default:
echo.echo_report('Configuring different user, defaults may not be appropriate.')

computer.configure(user=user, **kwargs)
Expand Down
18 changes: 18 additions & 0 deletions tests/orm/test_users.py
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida-core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
"""Tests for :mod:`aiida.orm.users`."""
from aiida.orm.users import User


def test_user_is_default(default_user):
"""Test the :meth:`aiida.orm.users.User.is_default` property."""
assert default_user.is_default
user = User('other@localhost').store()
assert not user.is_default

0 comments on commit a43c4cd

Please sign in to comment.