def post_source_attribute(source_uuid: SourceId, source_attribute: str, database: Database): """Set a source attribute.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = SourceData(data_model, reports, source_uuid) value = dict(bottle.request.json)[source_attribute] old_value: Any if source_attribute == "position": old_value, value = move_item(data, value, "source") else: old_value = data.source.get(source_attribute) or "" data.source[source_attribute] = value if old_value == value: return dict(ok=True) # Nothing to do delta_description = ( f"{{user}} changed the {source_attribute} of source '{data.source_name}' of metric '{data.metric_name}' of " f"subject '{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{value}'." ) uuids = [ data.report_uuid, data.subject_uuid, data.metric_uuid, source_uuid ] if source_attribute == "type": data.source["parameters"] = default_source_parameters( database, data.metric["type"], value) return insert_new_report(database, delta_description, (data.report, uuids))
def post_metric_attribute(metric_uuid: MetricId, metric_attribute: str, database: Database): """Set the metric attribute.""" new_value = dict(bottle.request.json)[metric_attribute] data = MetricData(latest_datamodel(database), latest_reports(database), metric_uuid) if metric_attribute == "comment" and new_value: new_value = sanitize_html(new_value) old_value: Any if metric_attribute == "position": old_value, new_value = move_item(data, new_value, "metric") else: old_value = data.metric.get(metric_attribute) or "" if old_value == new_value: return dict(ok=True) # Nothing to do data.metric[metric_attribute] = new_value if metric_attribute == "type": data.metric.update(default_metric_attributes(database, new_value)) description = ( f"{{user}} changed the {metric_attribute} of metric '{data.metric_name}' of subject " f"'{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{new_value}'." ) uuids = [data.report_uuid, data.subject_uuid, metric_uuid] insert_new_report(database, description, (data.report, uuids)) if metric_attribute in ATTRIBUTES_IMPACTING_STATUS and ( latest := latest_measurement(database, metric_uuid)): return insert_new_measurement(database, data.datamodel, data.metric, latest.copy(), latest)
def post_metric_attribute(metric_uuid: MetricId, metric_attribute: str, database: Database): """Set the metric attribute.""" value = dict(bottle.request.json)[metric_attribute] data_model = latest_datamodel(database) reports = latest_reports(database) data = MetricData(data_model, reports, metric_uuid) if metric_attribute == "comment" and value: value = sanitize_html(value) old_value: Any if metric_attribute == "position": old_value, value = move_item(data, value, "metric") else: old_value = data.metric.get(metric_attribute) or "" if old_value == value: return dict(ok=True) # Nothing to do data.metric[metric_attribute] = value if metric_attribute == "type": data.metric.update(default_metric_attributes(database, value)) user = sessions.user(database) data.report["delta"] = dict( uuids=[data.report_uuid, data.subject_uuid, metric_uuid], email=user["email"], description= f"{user['user']} changed the {metric_attribute} of metric '{data.metric_name}' " f"of subject '{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{value}'." ) insert_new_report(database, data.report) attributes_impacting_status = ("accept_debt", "debt_target", "debt_end_date", "direction", "near_target", "target") if metric_attribute in attributes_impacting_status and ( latest := latest_measurement(database, metric_uuid)): return insert_new_measurement(database, data.datamodel, data.metric, latest)
def post_subject_attribute(subject_uuid: SubjectId, subject_attribute: str, database: Database): """Set the subject attribute.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = SubjectData(data_model, reports, subject_uuid) value = dict(bottle.request.json)[subject_attribute] old_value = data.subject.get(subject_attribute) or "" if subject_attribute == "position": old_value, value = move_item(data, value, "subject") else: data.subject[subject_attribute] = value if old_value == value: return dict(ok=True) # Nothing to do user = sessions.user(database) data.report["delta"] = dict( uuids=[data.report_uuid, subject_uuid], email=user["email"], description=f"{user['user']} changed the {subject_attribute} of subject " f"'{data.subject_name}' in report '{data.report_name}' from '{old_value}' to '{value}'.") return insert_new_report(database, data.report)