docs: def assign_value(rs, data_type, val,
                   unit_id, name, metadata={}, data_hash=None, user_id=None, source=None):
      """
          Insert or update a piece of data in a scenario.
          If the dataset is being shared by other resource scenarios, a new dataset is inserted.
          If the dataset is ONLY being used by the resource scenario in question, the dataset
          is updated to avoid unnecessary duplication.
      """
  
      log.debug("Assigning value %s to rs %s in scenario %s",
                name, rs.resource_attr_id, rs.scenario_id)
  
      if rs.scenario.locked == 'Y':
          raise PermissionError("Cannot assign value. Scenario %s is locked"
                               %(rs.scenario_id))
  
      #Check if this RS is the only RS in the DB connected to this dataset.
      #If no results is found, the RS isn't in the DB yet, so the condition is false.
      update_dataset = False # Default behaviour is to create a new dataset.
  
      if rs.dataset is not None:
  
          #Has this dataset changed?
          if rs.dataset.hash == data_hash:
              log.debug("Dataset has not changed. Returning.")
              return
  
          connected_rs = db.DBSession.query(ResourceScenario).filter(ResourceScenario.dataset_id==rs.dataset.id).all()
          #If there's no RS found, then the incoming rs is new, so the dataset can be altered
          #without fear of affecting something else.
          if len(connected_rs) == 0:
          #If it's 1, the RS exists in the DB, but it's the only one using this dataset or
          #The RS isn't in the DB yet and the datset is being used by 1 other RS.
              update_dataset = True
  
          if len(connected_rs) == 1 :
              if connected_rs[0].scenario_id == rs.scenario_id and connected_rs[0].resource_attr_id==rs.resource_attr_id:
                  update_dataset = True
          else:
              update_dataset=False
  
      if update_dataset is True:
          log.info("Updating dataset '%s'", name)
          dataset = data.update_dataset(rs.dataset.id, name, data_type, val, unit_id, metadata, flush=False, **dict(user_id=user_id))
          rs.dataset = dataset
          rs.dataset_id = dataset.id
          log.info("Set RS dataset id to %s"%dataset.id)
      else:
          log.info("Creating new dataset %s in scenario %s", name, rs.scenario_id)
          dataset = data.add_dataset(data_type,
                                  val,
                                  unit_id,
                                  metadata=metadata,
                                  name=name,
                                  **dict(user_id=user_id))
          rs.dataset = dataset
          rs.source = source
  
      db.DBSession.flush()