Exemple #1
0
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
Exemple #2
0
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
Exemple #3
0
def import_json_report(database: Database, imported_report):
    """ Store the report given as json in the database. """
    report_to_store = dict(title=imported_report.get("title",
                                                     "Example report"),
                           report_uuid=imported_report["report_uuid"],
                           subjects={})
    for imported_subject in imported_report.get("subjects", []):
        subject_to_store = default_subject_attributes(database,
                                                      imported_subject["type"])
        subject_to_store["metrics"] = {}  # Remove default metrics
        subject_to_store["name"] = imported_subject["name"]
        report_to_store["subjects"][uuid()] = subject_to_store
        for imported_metric in imported_subject.get("metrics", []):
            metric_to_store = default_metric_attributes(
                database, imported_metric["type"])
            metric_to_store.update(imported_metric)
            metric_to_store["sources"] = {
            }  # Sources in the example report json are lists, we transform them to dicts
            subject_to_store["metrics"][uuid()] = metric_to_store
            for imported_source in imported_metric.get("sources", []):
                source_to_store = metric_to_store["sources"][
                    uuid()] = imported_source
                source_parameters = default_source_parameters(
                    database, imported_metric["type"], imported_source["type"])
                for key, value in source_parameters.items():
                    if key not in source_to_store["parameters"]:
                        source_to_store["parameters"][key] = value
    return insert_new_report(database, "{{user}} imported a new report",
                             (report_to_store, report_to_store["report_uuid"]))
Exemple #4
0
 def test_default_subject_attributes(self):
     """Test that the default subject attributes can be retrieved from the data model."""
     self.database.datamodels.find_one.return_value = dict(
         _id=123, subjects=dict(subject_type=dict(name="name", description="description")))
     self.assertEqual(
         dict(name=None, description="description", type="subject_type", metrics={}),
         default_subject_attributes(self.database, "subject_type"))