Esempio n. 1
0
    def test_joins3_user_group(self):
        from aiida.orm.user import User
        from aiida.orm.querybuilder import QueryBuilder

        # Create another user
        new_email = "[email protected]"
        user = User(email=new_email)
        user.force_save()

        # Create a group that belongs to that user
        from aiida.orm.group import Group
        group = Group(name="node_group")
        group.dbgroup.user = user._dbuser
        group.store()

        # Search for the group of the user
        qb = QueryBuilder()
        qb.append(User, tag='user', filters={'id': {'==': user.id}})
        qb.append(Group, belongs_to='user', filters={'id': {'==': group.id}})
        self.assertEquals(
            qb.count(), 1, "The expected group that belongs to "
            "the selected user was not found.")

        # Search for the user that owns a group
        qb = QueryBuilder()
        qb.append(Group, tag='group', filters={'id': {'==': group.id}})
        qb.append(User, owner_of='group', filters={'id': {'==': user.id}})

        self.assertEquals(
            qb.count(), 1, "The expected user that owns the "
            "selected group was not found.")
Esempio n. 2
0
def get_or_new_user(**kwargs):
    """find an existing user or instantiate a new one (unstored)"""
    from aiida.orm import User
    candidates = User.search_for_users(**kwargs)
    if candidates:
        user = candidates[0]
        created = False
    else:
        user = User(**kwargs)
        created = True
    return user, created
Esempio n. 3
0
    def __clean_db_sqla(self):
        """Clean database for sqlalchemy backend"""
        from aiida.backends.sqlalchemy.tests.testbase import SqlAlchemyTests
        from aiida.backends.sqlalchemy import get_scoped_session
        from aiida.orm import User

        user = User.search_for_users(email=self.email)[0]
        new_user = User(email=user.email)
        new_user.first_name = user.first_name
        new_user.last_name = user.last_name
        new_user.institution = user.institution

        sqla_testcase = SqlAlchemyTests()
        sqla_testcase.test_session = get_scoped_session()
        sqla_testcase.clean_db()

        # that deleted our user, we need to recreate it
        new_user.force_save()
Esempio n. 4
0
    def init_db(self):
        """Initialise the database state for running of tests.

        Adds default user if necessary.
        """
        from aiida.orm import User
        from aiida.cmdline.commands.cmd_user import set_default_user

        if not User.objects.get_default():
            user_dict = get_user_dict(_DEFAULT_PROFILE_INFO)
            try:
                user = User(**user_dict)
                user.store()
            except exceptions.IntegrityError:
                # The user already exists, no problem
                user = User.objects.get(**user_dict)

            set_default_user(self._profile, user)
            User.objects.reset()  # necessary to pick up new default user
Esempio n. 5
0
    def workflow_list(self, *args):
        """
        Return a list of workflows on screen
        """
        from aiida.backends.utils import load_dbenv, is_dbenv_loaded

        if not is_dbenv_loaded():
            load_dbenv()

        from aiida.backends.utils import get_workflow_list, get_automatic_user
        from aiida.orm.workflow import get_workflow_info
        from aiida.orm import User

        import argparse

        parser = argparse.ArgumentParser(prog=self.get_full_command_name(),
                                         description='List AiiDA workflows.')
        parser.add_argument('-s',
                            '--short',
                            action='store_true',
                            help="show shorter output "
                            "(only subworkflows and steps, no calculations)")
        parser.add_argument(
            '-a',
            '--all-states',
            action='store_true',
            help="show all existing "
            "AiiDA workflows, not only running ones",
        )
        parser.add_argument(
            '-d',
            '--depth',
            metavar='M',
            action='store',
            type=int,
            default=16,
            help="add a filter "
            "to show only steps down to a depth of M levels in "
            "subworkflows (0 means only the parent "
            "workflows are shown)")
        parser.add_argument(
            '-p',
            '--past-days',
            metavar='N',
            action='store',
            type=int,
            help="add a "
            "filter to show only workflows created in the past N days")
        parser.add_argument(
            'pks',
            type=int,
            nargs='*',
            help="a list of workflows to show. If empty, "
            "all running workflows are shown. If non-empty, "
            "automatically sets --all and ignores the -p option.")

        tab_size = 2  # how many spaces to use for indentation of subworkflows

        args = list(args)
        parsed_args = parser.parse_args(args)

        workflows = get_workflow_list(parsed_args.pks,
                                      user=User(dbuser=get_automatic_user()),
                                      all_states=parsed_args.all_states,
                                      n_days_ago=parsed_args.past_days)

        for w in workflows:
            if not w.is_subworkflow() or w.pk in parsed_args.pks:
                print "\n".join(
                    get_workflow_info(w,
                                      tab_size=tab_size,
                                      short=parsed_args.short,
                                      depth=parsed_args.depth))
        if not workflows:
            if parsed_args.all_states:
                print "# No workflows found"
            else:
                print "# No running workflows found"