def _generate_experiments_allowed():
    experimentA, experimentB = _generate_two_experiments()
    exp_allowedA = ExperimentAllowed.ExperimentAllowed(experimentA, 100, 5,
                                                       True)
    exp_allowedB = ExperimentAllowed.ExperimentAllowed(experimentB, 100, 5,
                                                       True)
    return exp_allowedA, exp_allowedB
Exemple #2
0
    def list_experiments(self, user_login):
        session = self.Session()
        try:
            user = self._get_user(session, user_login)
            permissions = self._gather_permissions(session, user,
                                                   'experiment_allowed')

            grouped_experiments = {}
            for permission in permissions:
                p_permanent_id = self._get_parameter_from_permission(
                    session, permission, 'experiment_permanent_id')
                p_category_id = self._get_parameter_from_permission(
                    session, permission, 'experiment_category_id')
                p_time_allowed = self._get_float_parameter_from_permission(
                    session, permission, 'time_allowed')
                p_priority = self._get_int_parameter_from_permission(
                    session, permission, 'priority',
                    ExperimentAllowed.DEFAULT_PRIORITY)
                p_initialization_in_accounting = self._get_bool_parameter_from_permission(
                    session, permission, 'initialization_in_accounting',
                    ExperimentAllowed.DEFAULT_INITIALIZATION_IN_ACCOUNTING)

                experiment = session.query(model.DbExperiment).filter_by(
                    name=p_permanent_id).filter(model.DbExperimentCategory.name
                                                == p_category_id).one()
                experiment_allowed = ExperimentAllowed.ExperimentAllowed(
                    experiment.to_business(), p_time_allowed, p_priority,
                    p_initialization_in_accounting)

                experiment_unique_id = p_permanent_id + "@" + p_category_id
                if experiment_unique_id in grouped_experiments:
                    grouped_experiments[experiment_unique_id].append(
                        experiment_allowed)
                else:
                    grouped_experiments[experiment_unique_id] = [
                        experiment_allowed
                    ]

            # If any experiment is duplicated, only the less restrictive one is given
            experiments = []
            for experiment_unique_id in grouped_experiments:
                less_restrictive_experiment_allowed = grouped_experiments[
                    experiment_unique_id][0]
                for experiment_allowed in grouped_experiments[
                        experiment_unique_id]:
                    if experiment_allowed.time_allowed > less_restrictive_experiment_allowed.time_allowed:
                        less_restrictive_experiment_allowed = experiment_allowed
                experiments.append(less_restrictive_experiment_allowed)

            experiments.sort(lambda x, y: cmp(x.experiment.category.name, y.
                                              experiment.category.name))
            return tuple(experiments)
        finally:
            session.close()
def generate_experiment_allowed(time_allowed, exp_name, exp_cat_name):
    exp = generate_experiment(exp_name, exp_cat_name)
    return ExperimentAllowed.ExperimentAllowed(exp, time_allowed, 5, True,
                                               '%s::user' % exp_name, 1,
                                               'user')