def create_thai_units_and_targets(project): with open(Path('forecast_app/tests/projects/thai-project.json')) as fp: project_dict = json.load(fp) _validate_and_create_units(project, project_dict) # !is_validate to bypass Impetus non-uniform bins: [0, 1), [1, 10), [10, 20), ..., [1990, 2000): _validate_and_create_targets(project, project_dict)
def make_cdc_units_and_targets(project): """ Creates CDC standard Targets for project. """ with open(Path('forecast_app/tests/projects/cdc-project.json')) as fp: project_dict = json.load(fp) _validate_and_create_units(project, project_dict) _validate_and_create_targets(project, project_dict)
def execute_project_config_diff(project, changes): """ Executes the passed Changes list by making the corresponding database changes to project. :param project: the Project that's being modified :param changes: list of Changes as returned by project_config_diff() """ objects_to_save = [] for change in order_project_config_diff(changes): if change.change_type == ChangeType.OBJ_ADDED: if change.object_type == ObjectType.UNIT: _validate_and_create_units(project, {'units': [change.object_dict]}) elif change.object_type == ObjectType.TARGET: _validate_and_create_targets(project, {'targets': [change.object_dict]}) elif change.object_type == ObjectType.TIMEZERO: _validate_and_create_timezeros( project, {'timezeros': [change.object_dict]}) elif change.change_type == ChangeType.OBJ_REMOVED: the_obj = object_for_change( project, change, objects_to_save) # Project/Unit/Target/TimeZero. raises the_obj.delete() elif (change.change_type == ChangeType.FIELD_EDITED) or (change.change_type == ChangeType.FIELD_ADDED) \ or (change.change_type == ChangeType.FIELD_REMOVED): the_obj = object_for_change( project, change, objects_to_save) # Project/Unit/Target/TimeZero. raises # NB: here we convert '' to None to avoid errors like when setting a timezero's data_version_date to '', # say when users incorrectly pass '' instead of null in a project config JSON file attr_value = None if (change.change_type == ChangeType.FIELD_REMOVED) \ or (change.object_dict[change.field_name] == '') \ else change.object_dict[change.field_name] setattr(the_obj, change.field_name, attr_value) # NB: do not save here b/c multiple FIELD_* changes might be required together to be valid, e.g., when # changing Target.is_step_ahead to False, one must remove Target.step_ahead_increment (i.e., set it to None) objects_to_save.append(the_obj) for object_to_save in objects_to_save: try: object_to_save.save() except Exception as ex: message = f"execute_project_config_diff(): error trying to save: ex={ex}, object_to_save={object_to_save}" logger.error(message) raise RuntimeError(message)