def post_move_metric(metric_uuid: MetricId, target_subject_uuid: SubjectId, database: Database): """Move the metric to another subject.""" data_model = latest_datamodel(database) reports = latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, target_subject_uuid) user = sessions.user(database) delta_description = f"{user['user']} moved the metric '{source.metric_name}' from subject " \ f"'{source.subject_name}' in report '{source.report_name}' to subject " \ f"'{target.subject_name}' in report '{target.report_name}'." target.subject["metrics"][metric_uuid] = source.metric reports_to_insert = [target.report] if target.report_uuid == source.report_uuid: # Metric is moved within the same report del target.report["subjects"][ source.subject_uuid]["metrics"][metric_uuid] target_uuids = [ target.report_uuid, source.subject_uuid, target_subject_uuid, metric_uuid ] else: # Metric is moved from one report to another, update both reports_to_insert.append(source.report) del source.subject["metrics"][metric_uuid] source.report["delta"] = dict( uuids=[source.report_uuid, source.subject_uuid, metric_uuid], email=user["email"], description=delta_description) target_uuids = [target.report_uuid, target_subject_uuid, metric_uuid] target.report["delta"] = dict(uuids=target_uuids, email=user["email"], description=delta_description) return insert_new_report(database, *reports_to_insert)
def post_move_metric(metric_uuid: MetricId, target_subject_uuid: SubjectId, database: Database): """Move the metric to another subject.""" data_model, reports = latest_datamodel(database), latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, target_subject_uuid) delta_description = ( f"{{user}} moved the metric '{source.metric_name}' from subject '{source.subject_name}' in report " f"'{source.report_name}' to subject '{target.subject_name}' in report '{target.report_name}'." ) target.subject["metrics"][metric_uuid] = source.metric if target.report_uuid == source.report_uuid: # Metric is moved within the same report del target.report["subjects"][ source.subject_uuid]["metrics"][metric_uuid] target_uuids = [ target.report_uuid, source.subject_uuid, target_subject_uuid, metric_uuid ] reports_to_insert = [(target.report, target_uuids)] else: # Metric is moved from one report to another, update both del source.subject["metrics"][metric_uuid] source_uuids = [source.report_uuid, source.subject_uuid, metric_uuid] target_uuids = [target.report_uuid, target_subject_uuid, metric_uuid] reports_to_insert = [(target.report, target_uuids), (source.report, source_uuids)] return insert_new_report(database, delta_description, *reports_to_insert)
def delete_subject(subject_uuid: SubjectId, database: Database): """Delete the subject.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = SubjectData(data_model, reports, subject_uuid) del data.report["subjects"][subject_uuid] user = sessions.user(database) data.report["delta"] = dict( uuids=[data.report_uuid, subject_uuid], email=user["email"], description=f"{user['user']} deleted the subject '{data.subject_name}' from report '{data.report_name}'.") return insert_new_report(database, data.report)
def post_move_subject(subject_uuid: SubjectId, target_report_uuid: ReportId, database: Database): """Move the subject to another report.""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SubjectData(data_model, reports, subject_uuid) target = ReportData(data_model, reports, target_report_uuid) target.report["subjects"][subject_uuid] = source.subject del source.report["subjects"][subject_uuid] user = sessions.user(database) delta_description = f"{user['user']} moved the subject '{source.subject_name}' from report " \ f"'{source.report_name}' to report '{target.report_name}'." source.report["delta"] = dict( uuids=[source.report_uuid, subject_uuid], email=user["email"], description=delta_description) target.report["delta"] = dict( uuids=[target_report_uuid, subject_uuid], email=user["email"], description=delta_description) return insert_new_report(database, source.report, target.report)
def post_metric_new(subject_uuid: SubjectId, database: Database): """Add a new metric.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = SubjectData(data_model, reports, subject_uuid) data.subject["metrics"][(metric_uuid := uuid())] = default_metric_attributes(database) user = sessions.user(database) data.report["delta"] = dict( uuids=[data.report_uuid, data.subject_uuid, metric_uuid], email=user["email"], description= f"{user['user']} added a new metric to subject '{data.subject_name}' in report " f"'{data.report_name}'.") result = insert_new_report(database, data.report) result["new_metric_uuid"] = metric_uuid return result
def post_metric_new(subject_uuid: SubjectId, database: Database): """Add a new metric.""" data = SubjectData(latest_datamodel(database), latest_reports(database), subject_uuid) data.subject["metrics"][(metric_uuid := uuid())] = default_metric_attributes(database) description = f"{{user}} added a new metric to subject '{data.subject_name}' in report '{data.report_name}'." uuids = [data.report_uuid, data.subject_uuid, metric_uuid] result = insert_new_report(database, description, (data.report, uuids)) result["new_metric_uuid"] = metric_uuid return result
def delete_subject(subject_uuid: SubjectId, database: Database): """Delete the subject.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = SubjectData(data_model, reports, subject_uuid) del data.report["subjects"][subject_uuid] delta_description = f"{{user}} deleted the subject '{data.subject_name}' from report '{data.report_name}'." uuids = [data.report_uuid, subject_uuid] return insert_new_report(database, delta_description, (data.report, uuids))
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)
def post_metric_copy(metric_uuid: MetricId, subject_uuid: SubjectId, database: Database): """Add a copy of the metric to the subject (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, subject_uuid) target.subject["metrics"][(metric_copy_uuid := uuid())] = copy_metric(source.metric, source.datamodel) user = sessions.user(database) target.report["delta"] = dict( uuids=[target.report_uuid, target.subject_uuid, metric_copy_uuid], email=user["email"], description= f"{user['user']} copied the metric '{source.metric_name}' of subject " f"'{source.subject_name}' from report '{source.report_name}' to subject '{target.subject_name}' " f"in report '{target.report_name}'.") result = insert_new_report(database, target.report) result["new_metric_uuid"] = metric_copy_uuid return result
def post_metric_copy(metric_uuid: MetricId, subject_uuid: SubjectId, database: Database): """Add a copy of the metric to the subject (new in v3).""" data_model, reports = latest_datamodel(database), latest_reports(database) source = MetricData(data_model, reports, metric_uuid) target = SubjectData(data_model, reports, subject_uuid) target.subject["metrics"][(metric_copy_uuid := uuid())] = copy_metric(source.metric, source.datamodel) description = ( f"{{user}} copied the metric '{source.metric_name}' of subject '{source.subject_name}' from report " f"'{source.report_name}' to subject '{target.subject_name}' in report '{target.report_name}'." ) uuids = [target.report_uuid, target.subject_uuid, metric_copy_uuid] result = insert_new_report(database, description, (target.report, uuids)) result["new_metric_uuid"] = metric_copy_uuid return result
def post_subject_copy(subject_uuid: SubjectId, report_uuid: ReportId, database: Database): """Add a copy of the subject to the report (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SubjectData(data_model, reports, subject_uuid) target = ReportData(data_model, reports, report_uuid) target.report["subjects"][(subject_copy_uuid := uuid())] = copy_subject(source.subject, source.datamodel) user = sessions.user(database) target.report["delta"] = dict( uuids=[target.report_uuid, subject_copy_uuid], email=user["email"], description=f"{user['user']} copied the subject '{source.subject_name}' from report " \ f"'{source.report_name}' to report '{target.report_name}'.") result = insert_new_report(database, target.report) result["new_subject_uuid"] = subject_copy_uuid return result
def post_move_subject(subject_uuid: SubjectId, target_report_uuid: ReportId, database: Database): """Move the subject to another report.""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SubjectData(data_model, reports, subject_uuid) target = ReportData(data_model, reports, target_report_uuid) target.report["subjects"][subject_uuid] = source.subject del source.report["subjects"][subject_uuid] delta_description = ( f"{{user}} moved the subject '{source.subject_name}' from report " f"'{source.report_name}' to report '{target.report_name}'.") source_uuids = [source.report_uuid, subject_uuid] target_uuids = [target_report_uuid, subject_uuid] return insert_new_report(database, delta_description, (source.report, source_uuids), (target.report, target_uuids))
def post_subject_copy(subject_uuid: SubjectId, report_uuid: ReportId, database: Database): """Add a copy of the subject to the report (new in v3).""" data_model = latest_datamodel(database) reports = latest_reports(database) source = SubjectData(data_model, reports, subject_uuid) target = ReportData(data_model, reports, report_uuid) target.report["subjects"][(subject_copy_uuid := uuid())] = copy_subject(source.subject, source.datamodel) delta_description = ( f"{{user}} copied the subject '{source.subject_name}' from report " f"'{source.report_name}' to report '{target.report_name}'.") uuids = [target.report_uuid, subject_copy_uuid] result = insert_new_report(database, delta_description, (target.report, uuids)) result["new_subject_uuid"] = subject_copy_uuid return result