def update_scenario(scenario, update_data=True, update_groups=True, **kwargs): """ Update a single scenario as all resources already exist, there is no need to worry about negative IDS """ user_id = kwargs.get('user_id') scen = _get_scenario(scenario.id) _check_network_ownership(scenario.network_id, user_id) if scen.locked == 'Y': raise PermissionError('Scenario is locked. Unlock before editing.') scen.scenario_name = scenario.name scen.scenario_description = scenario.description scen.layout = scenario.get_layout() scen.start_time = str(timestamp_to_ordinal( scenario.start_time)) if scenario.start_time else None scen.end_time = str(timestamp_to_ordinal( scenario.end_time)) if scenario.end_time else None scen.time_step = scenario.time_step if scenario.resourcescenarios == None: scenario.resourcescenarios = [] if scenario.resourcegroupitems == None: scenario.resourcegroupitems = [] if update_data is True: datasets = [rs.value for rs in scenario.resourcescenarios] updated_datasets = data._bulk_insert_data(datasets, user_id, kwargs.get('app_name')) for i, r_scen in enumerate(scenario.resourcescenarios): _update_resourcescenario(scen, r_scen, dataset=updated_datasets[i], user_id=user_id, source=kwargs.get('app_name')) if update_groups is True: #Get all the exiting resource group items for this scenario. #THen process all the items sent to this handler. #Any in the DB that are not passed in here are removed. for group_item in scenario.resourcegroupitems: group_item_i = _add_resourcegroupitem(group_item, scenario.id) if group_item.id is None or group_item.id < 0: scen.resourcegroupitems.append(group_item_i) DBSession.flush() return scen
def update_scenario(scenario,update_data=True,update_groups=True,**kwargs): """ Update a single scenario as all resources already exist, there is no need to worry about negative IDS """ user_id = kwargs.get('user_id') scen = _get_scenario(scenario.id) _check_network_ownership(scenario.network_id, user_id) if scen.locked == 'Y': raise PermissionError('Scenario is locked. Unlock before editing.') scen.scenario_name = scenario.name scen.scenario_description = scenario.description scen.layout = scenario.get_layout() scen.start_time = str(timestamp_to_ordinal(scenario.start_time)) if scenario.start_time else None scen.end_time = str(timestamp_to_ordinal(scenario.end_time)) if scenario.end_time else None scen.time_step = scenario.time_step if scenario.resourcescenarios == None: scenario.resourcescenarios = [] if scenario.resourcegroupitems == None: scenario.resourcegroupitems = [] if update_data is True: datasets = [rs.value for rs in scenario.resourcescenarios] updated_datasets = data._bulk_insert_data(datasets, user_id, kwargs.get('app_name')) for i, r_scen in enumerate(scenario.resourcescenarios): _update_resourcescenario(scen, r_scen, dataset=updated_datasets[i], user_id=user_id, source=kwargs.get('app_name')) if update_groups is True: #Get all the exiting resource group items for this scenario. #THen process all the items sent to this handler. #Any in the DB that are not passed in here are removed. for group_item in scenario.resourcegroupitems: group_item_i = _add_resourcegroupitem(group_item, scenario.id) if group_item.id is None or group_item.id < 0: scen.resourcegroupitems.append(group_item_i) DBSession.flush() return scen
def add_scenario(network_id, scenario, **kwargs): """ Add a scenario to a specified network. """ user_id = int(kwargs.get('user_id')) log.info("Adding scenarios to network") _check_network_ownership(network_id, user_id) existing_scen = DBSession.query(Scenario).filter( Scenario.scenario_name == scenario.name, Scenario.network_id == network_id).first() if existing_scen is not None: raise HydraError("Scenario with name %s already exists in network %s" % (scenario.name, network_id)) scen = Scenario() scen.scenario_name = scenario.name scen.scenario_description = scenario.description scen.layout = scenario.get_layout() scen.network_id = network_id scen.created_by = user_id scen.start_time = str(timestamp_to_ordinal( scenario.start_time)) if scenario.start_time else None scen.end_time = str(timestamp_to_ordinal( scenario.end_time)) if scenario.end_time else None scen.time_step = scenario.time_step #Just in case someone puts in a negative ID for the scenario. if scenario.id < 0: scenario.id = None if scenario.resourcescenarios is not None: #extract the data from each resourcescenario so it can all be #inserted in one go, rather than one at a time all_data = [r.value for r in scenario.resourcescenarios] datasets = data._bulk_insert_data(all_data, user_id=user_id) #record all the resource attribute ids resource_attr_ids = [ r.resource_attr_id for r in scenario.resourcescenarios ] #get all the resource scenarios into a list and bulk insert them for i, ra_id in enumerate(resource_attr_ids): rs_i = ResourceScenario() rs_i.resource_attr_id = ra_id rs_i.dataset_id = datasets[i].dataset_id rs_i.scenario_id = scen.scenario_id rs_i.dataset = datasets[i] scen.resourcescenarios.append(rs_i) if scenario.resourcegroupitems is not None: #Again doing bulk insert. for group_item in scenario.resourcegroupitems: group_item_i = ResourceGroupItem() group_item_i.scenario_id = scen.scenario_id group_item_i.group_id = group_item.group_id group_item_i.ref_key = group_item.ref_key if group_item.ref_key == 'NODE': group_item_i.node_id = group_item.ref_id elif group_item.ref_key == 'LINK': group_item_i.link_id = group_item.ref_id elif group_item.ref_key == 'GROUP': group_item_i.subgroup_id = group_item.ref_id scen.resourcegroupitems.append(group_item_i) DBSession.add(scen) DBSession.flush() return scen
def add_scenario(network_id, scenario,**kwargs): """ Add a scenario to a specified network. """ user_id = int(kwargs.get('user_id')) log.info("Adding scenarios to network") _check_network_ownership(network_id, user_id) existing_scen = DBSession.query(Scenario).filter(Scenario.scenario_name==scenario.name, Scenario.network_id==network_id).first() if existing_scen is not None: raise HydraError("Scenario with name %s already exists in network %s"%(scenario.name, network_id)) scen = Scenario() scen.scenario_name = scenario.name scen.scenario_description = scenario.description scen.layout = scenario.get_layout() scen.network_id = network_id scen.created_by = user_id scen.start_time = str(timestamp_to_ordinal(scenario.start_time)) if scenario.start_time else None scen.end_time = str(timestamp_to_ordinal(scenario.end_time)) if scenario.end_time else None scen.time_step = scenario.time_step #Just in case someone puts in a negative ID for the scenario. if scenario.id < 0: scenario.id = None if scenario.resourcescenarios is not None: #extract the data from each resourcescenario so it can all be #inserted in one go, rather than one at a time all_data = [r.value for r in scenario.resourcescenarios] datasets = data._bulk_insert_data(all_data, user_id=user_id) #record all the resource attribute ids resource_attr_ids = [r.resource_attr_id for r in scenario.resourcescenarios] #get all the resource scenarios into a list and bulk insert them for i, ra_id in enumerate(resource_attr_ids): rs_i = ResourceScenario() rs_i.resource_attr_id = ra_id rs_i.dataset_id = datasets[i].dataset_id rs_i.scenario_id = scen.scenario_id rs_i.dataset = datasets[i] scen.resourcescenarios.append(rs_i) if scenario.resourcegroupitems is not None: #Again doing bulk insert. for group_item in scenario.resourcegroupitems: group_item_i = ResourceGroupItem() group_item_i.scenario_id = scen.scenario_id group_item_i.group_id = group_item.group_id group_item_i.ref_key = group_item.ref_key if group_item.ref_key == 'NODE': group_item_i.node_id = group_item.ref_id elif group_item.ref_key == 'LINK': group_item_i.link_id = group_item.ref_id elif group_item.ref_key == 'GROUP': group_item_i.subgroup_id = group_item.ref_id scen.resourcegroupitems.append(group_item_i) DBSession.add(scen) DBSession.flush() return scen