def post_notification_destination_attributes( report_uuid: ReportId, notification_destination_uuid: NotificationDestinationId, database: Database): """Set specified notification destination attributes.""" data = ReportData(latest_datamodel(database), latest_reports(database), report_uuid) notification_destination_name = data.report["notification_destinations"][ notification_destination_uuid]["name"] attributes = dict(bottle.request.json) old_values = [] for key in attributes: old_values.append(data.report["notification_destinations"] [notification_destination_uuid].get(key) or "") data.report["notification_destinations"][ notification_destination_uuid][key] = attributes[key] if set(old_values) == set(attributes.values()): return dict(ok=True) # Nothing to do separator = "' and '" delta_description = ( f"{{user}} changed the '{separator.join(attributes.keys())}' of notification destination " f"'{notification_destination_name}' in report '{data.report_name}' " f"from '{separator.join(old_values)}' to '{separator.join(attributes.values())}'." ) uuids = [data.report_uuid, notification_destination_uuid] return insert_new_report(database, delta_description, (data.report, uuids))
def delete_report(report_uuid: ReportId, database: Database): """Delete a report.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) data.report["deleted"] = "true" delta_description = f"{{user}} deleted the report '{data.report_name}'." return insert_new_report(database, delta_description, (data.report, [report_uuid]))
def delete_report(report_uuid: ReportId, database: Database): """Delete a report.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) data.report["deleted"] = "true" user = sessions.user(database) data.report["delta"] = dict( uuids=[report_uuid], email=user["email"], description=f"{user['user']} deleted the report '{data.report_name}'.") return insert_new_report(database, data.report)
def post_new_subject(report_uuid: ReportId, database: Database): """Create a new subject.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) data.report["subjects"][(subject_uuid := uuid())] = default_subject_attributes(database) user = sessions.user(database) data.report["delta"] = dict( uuids=[report_uuid, subject_uuid], email=user["email"], description=f"{user['user']} created a new subject in report '{data.report_name}'.") result = insert_new_report(database, data.report) result["new_subject_uuid"] = subject_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_report_attribute(report_uuid: ReportId, report_attribute: str, database: Database): """Set a report attribute.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) value = dict(bottle.request.json)[report_attribute] old_value = data.report.get(report_attribute) or "" data.report[report_attribute] = value value_change_description = "" if report_attribute == "layout" else f" from '{old_value}' to '{value}'" delta_description = ( f"{{user}} changed the {report_attribute} of report '{data.report_name}'{value_change_description}." ) return insert_new_report(database, delta_description, (data.report, [report_uuid]))
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_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_new_notification_destination(report_uuid: ReportId, database: Database): """Create a new notification destination.""" data = ReportData(latest_datamodel(database), latest_reports(database), report_uuid) if "notification_destinations" not in data.report: data.report["notification_destinations"] = {} data.report["notification_destinations"][( notification_destination_uuid := uuid())] = dict(webhook="", name="Microsoft Teams webhook", sleep_duration=0) delta_description = f"{{user}} created a new destination for notifications in report '{data.report_name}'." uuids = [report_uuid, notification_destination_uuid] result = insert_new_report(database, delta_description, (data.report, uuids)) result["new_destination_uuid"] = notification_destination_uuid return result
def post_report_attribute(report_uuid: ReportId, report_attribute: str, database: Database): """Set a report attribute.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) value = dict(bottle.request.json)[report_attribute] old_value = data.report.get(report_attribute) or "" data.report[report_attribute] = value value_change_description = "" if report_attribute == "layout" else f" from '{old_value}' to '{value}'" user = sessions.user(database) data.report["delta"] = dict( uuids=[report_uuid], email=user["email"], description= f"{user['user']} changed the {report_attribute} of report '{data.report_name}'" f"{value_change_description}.") return insert_new_report(database, data.report)
def post_report_copy(report_uuid: ReportId, database: Database): """Copy a report.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) report_copy = copy_report(data.report, data.datamodel) delta_description = f"{{user}} copied the report '{data.report_name}'." uuids = [report_uuid, report_copy["report_uuid"]] result = insert_new_report(database, delta_description, (report_copy, uuids)) result["new_report_uuid"] = report_copy["report_uuid"] return result
def post_new_subject(report_uuid: ReportId, database: Database): """Create a new subject.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) data.report["subjects"][(subject_uuid := uuid())] = default_subject_attributes(database) delta_description = f"{{user}} created a new subject in report '{data.report_name}'." uuids = [report_uuid, subject_uuid] result = insert_new_report(database, delta_description, (data.report, uuids)) result["new_subject_uuid"] = subject_uuid return result
def delete_notification_destination( report_uuid: ReportId, notification_destination_uuid: NotificationDestinationId, database: Database): """Delete a destination from a report.""" data = ReportData(latest_datamodel(database), latest_reports(database), report_uuid) destination_name = data.report["notification_destinations"][ notification_destination_uuid]["name"] del data.report["notification_destinations"][notification_destination_uuid] delta_description = f"{{user}} deleted destination {destination_name} from report '{data.report_name}'." uuids = [report_uuid, notification_destination_uuid] return insert_new_report(database, delta_description, (data.report, uuids))
def post_report_copy(report_uuid: ReportId, database: Database): """Copy a report.""" data_model = latest_datamodel(database) reports = latest_reports(database) data = ReportData(data_model, reports, report_uuid) report_copy = copy_report(data.report, data.datamodel) user = sessions.user(database) report_copy["delta"] = dict( uuids=[report_uuid, report_copy["report_uuid"]], email=user["email"], description=f"{user['user']} copied the report '{data.report_name}'.") result = insert_new_report(database, report_copy) result["new_report_uuid"] = report_copy["report_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) 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