def setup_app(command, conf, vars): """Place any commands to setup majic_web_service here :param conf: configuration for the setup :param vars: variables :param command: commands """ # Don't reload the app if it was loaded under the testing environment load_environment(conf.global_conf, conf.local_conf) filename = os.path.split(conf.filename)[-1] if filename == 'test.ini': log.debug("Creating database") # Recreate the tables Base.metadata.drop_all(bind=Session.bind) Base.metadata.create_all(bind=Session.bind) with session_scope(Session) as session: stat_created = ModelRunStatus(constants.MODEL_RUN_STATUS_CREATED) stat_submitted = ModelRunStatus(constants.MODEL_RUN_STATUS_SUBMITTED) stat_pending = ModelRunStatus(constants.MODEL_RUN_STATUS_PENDING) stat_running = ModelRunStatus(constants.MODEL_RUN_STATUS_RUNNING) stat_completed = ModelRunStatus(constants.MODEL_RUN_STATUS_COMPLETED) stat_published = ModelRunStatus(constants.MODEL_RUN_STATUS_PUBLISHED) stat_public = ModelRunStatus(constants.MODEL_RUN_STATUS_PUBLIC) stat_failed = ModelRunStatus(constants.MODEL_RUN_STATUS_FAILED) stat_submit_failed = ModelRunStatus(constants.MODEL_RUN_STATUS_SUBMIT_FAILED) stat_unknown = ModelRunStatus(constants.MODEL_RUN_STATUS_UNKNOWN) map(session.add, [stat_created, stat_submitted, stat_pending, stat_running, stat_completed, stat_published, stat_public, stat_failed, stat_submit_failed, stat_unknown])
def clean_database(self): """ Cleans the dynamic data from the database """ with session_scope() as session: # delete all runs except the scientific configurations session \ .query(ModelRun) \ .delete() session \ .query(User) \ .delete()
def add_model_run( self, workbench_username, last_status_change, status, model_name="model name", description="a description", majic_username=None): """ Add a model run to the db :param workbench_username: username for the workbench :param last_status_change: last staus change :param status: current status (as string) :param model_name: name of the model :param description: description of the model :param majic_username: username in majic, defaults to the workbench_username if not set :return:id of model run created """ with session_scope() as session: try: if majic_username is not None: user = session.query(User).filter(User.username == majic_username).one() else: user = session.query(User).filter(User.username == workbench_username).one() except NoResultFound: user = User(workbench_username=workbench_username) if majic_username is None: user.username = workbench_username else: user.username = majic_username session.add(user) if status is not None: found_status = session.query(ModelRunStatus).filter(ModelRunStatus.name == status).one() else: found_status = None model_run = ModelRun() model_run.name = model_name model_run.status = found_status model_run.last_status_change = last_status_change model_run.description = description model_run.user = user session.add(model_run) session.flush() return model_run.id