Ejemplo n.º 1
0
def test_create_existing_instrument(capsys):
    name = 'Example Instrument'
    description = 'Example Instrument Description'
    instruments.create_instrument(name, description)
    assert len(instruments.get_instruments()) == 1

    with pytest.raises(SystemExit) as exc_info:
        scripts.main([scripts.__file__, 'create_instrument', name, description])
    assert exc_info.value != 0
    assert 'Error: an instrument with this name already exists' in capsys.readouterr()[1]
    assert len(instruments.get_instruments()) == 1
Ejemplo n.º 2
0
def instrument(flask_server):
    with flask_server.app.app_context():
        instrument = instruments.create_instrument(name="Example Instrument",
                                                   description="")
        # force attribute refresh
        assert instrument.id is not None
    return instrument
Ejemplo n.º 3
0
def test_update_instrument():
    instrument = instruments.create_instrument(name="Example Instrument", description="")
    instruments.update_instrument(instrument_id=instrument.id, name="Test", description="desc")
    instrument = instruments.get_instrument(instrument_id=instrument.id)
    assert instrument.name == "Test"
    assert instrument.description == "desc"
    assert len(instrument.responsible_users) == 0
Ejemplo n.º 4
0
def test_update_missing_instrument():
    instrument = instruments.create_instrument(name="Example Instrument",
                                               description="")
    with pytest.raises(errors.InstrumentDoesNotExistError):
        instruments.update_instrument(instrument_id=instrument.id + 1,
                                      name="Test",
                                      description="desc")
Ejemplo n.º 5
0
def test_instrument_responsible_users():
    user = User(name="Testuser",
                email="*****@*****.**",
                type=UserType.PERSON)
    sampledb.db.session.add(user)
    sampledb.db.session.commit()
    instrument = instruments.create_instrument(name="Example Instrument",
                                               description="")
    assert len(instrument.responsible_users) == 0
    instruments.add_instrument_responsible_user(instrument_id=instrument.id,
                                                user_id=user.id)
    assert len(instrument.responsible_users) == 1
    with pytest.raises(errors.UserAlreadyResponsibleForInstrumentError):
        instruments.add_instrument_responsible_user(
            instrument_id=instrument.id, user_id=user.id)
    instruments.remove_instrument_responsible_user(instrument_id=instrument.id,
                                                   user_id=user.id)
    assert len(instrument.responsible_users) == 0
    with pytest.raises(errors.UserNotResponsibleForInstrumentError):
        instruments.remove_instrument_responsible_user(
            instrument_id=instrument.id, user_id=user.id)
    with pytest.raises(errors.InstrumentDoesNotExistError):
        instruments.add_instrument_responsible_user(
            instrument_id=instrument.id + 1, user_id=user.id)
    with pytest.raises(errors.UserDoesNotExistError):
        instruments.add_instrument_responsible_user(
            instrument_id=instrument.id, user_id=user.id + 1)
    with pytest.raises(errors.InstrumentDoesNotExistError):
        instruments.remove_instrument_responsible_user(
            instrument_id=instrument.id + 1, user_id=user.id)
    with pytest.raises(errors.UserDoesNotExistError):
        instruments.remove_instrument_responsible_user(
            instrument_id=instrument.id, user_id=user.id + 1)
Ejemplo n.º 6
0
def test_set_instrument_responsible_users():
    user1 = User(name="Testuser",
                 email="*****@*****.**",
                 type=UserType.PERSON)
    user2 = User(name="Testuser",
                 email="*****@*****.**",
                 type=UserType.PERSON)
    sampledb.db.session.add(user1)
    sampledb.db.session.add(user2)
    sampledb.db.session.commit()
    instrument = instruments.create_instrument(name="Example Instrument",
                                               description="")
    assert len(instrument.responsible_users) == 0
    instruments.set_instrument_responsible_users(instrument.id, [user1.id])
    assert len(instrument.responsible_users) == 1
    assert user1 in instrument.responsible_users
    instruments.set_instrument_responsible_users(instrument.id, [user2.id])
    assert len(instrument.responsible_users) == 1
    assert user2 in instrument.responsible_users
    instruments.set_instrument_responsible_users(instrument.id,
                                                 [user1.id, user2.id])
    assert len(instrument.responsible_users) == 2
    assert user1 in instrument.responsible_users
    assert user2 in instrument.responsible_users
    instruments.set_instrument_responsible_users(instrument.id, [])
    assert len(instrument.responsible_users) == 0
Ejemplo n.º 7
0
def test_create_instrument():
    assert len(instruments.get_instruments()) == 0
    instrument = instruments.create_instrument(name="Example Instrument",
                                               description="")
    assert len(instruments.get_instruments()) == 1
    assert instrument == instruments.get_instrument(
        instrument_id=instrument.id)
    assert len(instrument.responsible_users) == 0
Ejemplo n.º 8
0
def test_create_missing_instrument_action():
    instrument = instruments.create_instrument(
        "Example Instrument", "Example Instrument Description")
    assert len(actions.get_actions()) == 0
    with pytest.raises(errors.InstrumentDoesNotExistError):
        actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                              name="Example Action",
                              description="",
                              schema=SCHEMA,
                              instrument_id=instrument.id + 1)
Ejemplo n.º 9
0
def test_update_instrument_invalid_instrument_id(capsys):
    name = 'Example Instrument'
    description = 'Example Instrument Description'
    instrument = instruments.create_instrument(name, description)
    assert len(instruments.get_instruments()) == 1

    with pytest.raises(SystemExit) as exc_info:
        scripts.main([scripts.__file__, 'update_instrument', name, name, ""])
    assert exc_info.value != 0
    assert "Error: instrument_id must be an integer" in capsys.readouterr()[1]

    assert len(instruments.get_instruments()) == 1
    instrument = instruments.get_instruments()[0]
    assert instrument.name == name
    assert instrument.description == description
Ejemplo n.º 10
0
def test_create_instrument_action():
    instrument = instruments.create_instrument(
        "Example Instrument", "Example Instrument Description")
    assert len(actions.get_actions()) == 0
    action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                                   name="Example Action",
                                   description="",
                                   schema=SCHEMA,
                                   instrument_id=instrument.id)
    assert action.name == "Example Action"
    assert action.description == ""
    assert action.schema == SCHEMA
    assert len(actions.get_actions()) == 1
    assert action == actions.get_action(action_id=action.id)
    assert action.instrument == instrument
Ejemplo n.º 11
0
def test_update_instrument(capsys):
    name = 'Example Instrument'
    description = 'Example Instrument Description'
    instrument = instruments.create_instrument(name, description)
    assert len(instruments.get_instruments()) == 1

    scripts.main(
        [scripts.__file__, 'update_instrument',
         str(instrument.id), name, ''])
    assert "Success" in capsys.readouterr()[0]

    assert len(instruments.get_instruments()) == 1
    instrument = instruments.get_instruments()[0]
    assert instrument.name == name
    assert instrument.description == ''
    assert len(instrument.responsible_users) == 0
Ejemplo n.º 12
0
def test_update_instrument_missing_arguments(capsys):
    name = 'Example Instrument'
    description = 'Example Instrument Description'
    instrument = instruments.create_instrument(name, description)
    assert len(instruments.get_instruments()) == 1

    with pytest.raises(SystemExit) as exc_info:
        scripts.main(
            [scripts.__file__, 'update_instrument',
             str(instrument.id)])
    assert exc_info.value != 0
    assert "Usage" in capsys.readouterr()[0]

    assert len(instruments.get_instruments()) == 1
    instrument = instruments.get_instruments()[0]
    assert instrument.name == name
    assert instrument.description == description
Ejemplo n.º 13
0
def instruments():
    return [
        create_instrument(name, 'Example Instrument Description')
        for name in ['Instrument 1', 'Instrument 2']
    ]
Ejemplo n.º 14
0
def setup_data(app):
    # TODO: replace using user management logic
    admin = User(name="Administrator",
                 email="*****@*****.**",
                 type=UserType.PERSON)
    admin.is_admin = True
    instrument_responsible_user = User(name="Instrument Responsible User",
                                       email="*****@*****.**",
                                       type=UserType.PERSON)
    basic_user = User(name="Basic User",
                      email="*****@*****.**",
                      type=UserType.PERSON)
    for user in (admin, instrument_responsible_user, basic_user):
        sampledb.db.session.add(user)
    sampledb.db.session.commit()

    api_user = sampledb.logic.users.create_user(name="API User",
                                                email="*****@*****.**",
                                                type=UserType.OTHER)
    sampledb.logic.authentication.add_other_authentication(
        api_user.id, 'api', 'password')

    group_id = groups.create_group(
        "Example Group", "This is an example group for testing purposes.",
        instrument_responsible_user.id).id

    project_id = projects.create_project("Example Project",
                                         "This is an example project",
                                         instrument_responsible_user.id).id
    project_id2 = projects.create_project("Example Project 2",
                                          "This is another example project",
                                          instrument_responsible_user.id).id
    projects.create_subproject_relationship(parent_project_id=project_id,
                                            child_project_id=project_id2,
                                            child_can_add_users_to_parent=True)

    # Setup autologin for testing
    @app.route('/users/me/autologin')
    @app.route('/users/<int:user_id>/autologin')
    def autologin(user_id=instrument_responsible_user.id):
        user = User.query.get(user_id)
        assert user is not None
        flask_login.login_user(user)
        return flask.redirect(
            flask.url_for('frontend.new_action', previous_action_id=4))

    sampledb.login_manager.login_view = 'autologin'

    instrument = create_instrument(
        name="OMBE I", description="This is an example instrument.")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('sampledb/schemas/ombe_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.SAMPLE_CREATION,
                                      "Sample Creation",
                                      "This is an example action", schema,
                                      instrument.id)
    independent_action = create_action(ActionType.SAMPLE_CREATION,
                                       "Alternative Process",
                                       "This is an example action", schema)
    with open('sampledb/schemas/ombe_measurement_batch.sampledb.json',
              'r') as schema_file:
        batch_schema = json.load(schema_file)
    create_action(ActionType.SAMPLE_CREATION, "Sample Creation (Batch)",
                  "This is an example action", batch_schema, instrument.id)
    sampledb.db.session.commit()

    with open('example_data/ombe-1.sampledb.json', 'r') as data_file:
        data = json.load(data_file)
    instrument_object = Objects.create_object(
        data=data,
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=instrument_action.id,
        connection=sampledb.db.engine)
    create_object(object_id=instrument_object.object_id,
                  user_id=instrument_responsible_user.id)
    data['multilayer'][0]['repetitions']['magnitude_in_base_units'] = 20000
    data['multilayer'][1]['films'][0]['thickness'][
        'magnitude_in_base_units'] = 1
    independent_object = Objects.create_object(
        data=data,
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=independent_action.id,
        connection=sampledb.db.engine)
    create_object(object_id=independent_object.object_id,
                  user_id=instrument_responsible_user.id)
    comments.create_comment(
        instrument_object.id, instrument_responsible_user.id,
        'This comment is very long. ' * 20 + '\n' +
        'This comment has three paragraphs. ' * 20 + '\n' + '\n' +
        'This comment has three paragraphs. ' * 20)
    comments.create_comment(instrument_object.id,
                            instrument_responsible_user.id,
                            'This is another, shorter comment')
    files.create_local_file(
        instrument_object.id, instrument_responsible_user.id, 'example.txt',
        lambda stream: stream.write("Dies ist ein Test".encode('utf-8')))
    files.create_local_file(
        instrument_object.id, instrument_responsible_user.id, 'demo.png',
        lambda stream: stream.write(
            open('sampledb/static/img/ghs01.png', 'rb').read()))
    files.update_file_information(instrument_object.id, 1,
                                  instrument_responsible_user.id,
                                  'Example File',
                                  'This is a file description.')
    files.create_url_file(instrument_object.id, instrument_responsible_user.id,
                          'http://iffsamples.fz-juelich.de/')

    with open('server_schemas/ombe_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    sampledb.logic.actions.update_action(instrument_action.id,
                                         "Updated Sample Creation", "", schema)

    object_permissions.set_group_object_permissions(
        independent_object.object_id, group_id,
        object_permissions.Permissions.READ)

    instrument = create_instrument(name="XRR",
                                   description="X-Ray Reflectometry")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('sampledb/schemas/xrr_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "XRR Measurement", "", schema,
                                      instrument.id)
    with open('sampledb/schemas/searchable_quantity.json', 'r') as schema_file:
        schema = json.load(schema_file)
    action = create_action(ActionType.SAMPLE_CREATION, "Searchable Object", "",
                           schema, None)
    independent_object = Objects.create_object(
        data={
            "name": {
                "_type": "text",
                "text": "TEST-1"
            },
            "tags": {
                "_type": "tags",
                "tags": ["tag1", "tag2"]
            },
            "mass": {
                "_type": "quantity",
                "dimensionality": "[mass]",
                "magnitude_in_base_units": 0.00001,
                "units": "mg"
            }
        },
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=action.id,
        connection=sampledb.db.engine)
    create_object(object_id=independent_object.object_id,
                  user_id=instrument_responsible_user.id)
    object_permissions.set_group_object_permissions(
        independent_object.object_id, group_id,
        object_permissions.Permissions.READ)
    object_permissions.set_user_object_permissions(
        independent_object.object_id, api_user.id,
        object_permissions.Permissions.WRITE)
    independent_object = Objects.create_object(
        data={
            "name": {
                "_type": "text",
                "text": "TEST-2"
            },
            "tags": {
                "_type": "tags",
                "tags": ["tag2", "tag3"]
            },
            "mass": {
                "_type": "quantity",
                "dimensionality": "[mass]",
                "magnitude_in_base_units": 0.000005,
                "units": "mg"
            }
        },
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=action.id,
        connection=sampledb.db.engine)
    create_object(object_id=independent_object.object_id,
                  user_id=instrument_responsible_user.id)
    object_permissions.set_group_object_permissions(
        independent_object.object_id, group_id,
        object_permissions.Permissions.READ)
    sampledb.db.session.commit()

    instrument = create_instrument(
        name="MPMS SQUID", description="MPMS SQUID Magnetometer JCNS-2")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('server_schemas/squid_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "Perform Measurement", "", schema,
                                      instrument.id)
    sampledb.db.session.commit()

    instrument = create_instrument(
        name="Powder Diffractometer",
        description="Huber Imaging Plate Guinier Camera G670 at JCNS-2")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('server_schemas/powder_diffractometer_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "Perform Measurement", "", schema,
                                      instrument.id)
    sampledb.db.session.commit()

    instrument = create_instrument(
        name="GALAXI", description="Gallium Anode Low-Angle X-ray Instrument")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('server_schemas/galaxi_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "Perform Measurement", "", schema,
                                      instrument.id)
    sampledb.db.session.commit()

    with open('server_schemas/other_sample.sampledb.json', 'r') as schema_file:
        schema = json.load(schema_file)
    create_action(ActionType.SAMPLE_CREATION, "Other Sample", "", schema, None)
    sampledb.db.session.commit()

    sample_action = sampledb.logic.actions.create_action(
        action_type=ActionType.SAMPLE_CREATION,
        name="sample_action",
        description="",
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Object Name',
                    'type': 'text'
                },
                'sample': {
                    'title': 'Sample',
                    'type': 'sample'
                }
            },
            'required': ['name']
        })
    measurement_action = sampledb.logic.actions.create_action(
        action_type=ActionType.MEASUREMENT,
        name="measurement_action",
        description="",
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Object Name',
                    'type': 'text'
                },
                'sample': {
                    'title': 'Sample',
                    'type': 'sample'
                },
                'comment': {
                    'title': 'Comment',
                    'type': 'text',
                    'multiline': True
                }
            },
            'required': ['name']
        })
    data = {'name': {'_type': 'text', 'text': 'Object 1'}}
    object = sampledb.logic.objects.create_object(sample_action.id, data,
                                                  user.id)
    sampledb.logic.object_permissions.set_object_public(object.id, True)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Object 2'
        },
        'sample': {
            '_type': 'sample',
            'object_id': object.id
        }
    }
    sample = sampledb.logic.objects.create_object(sample_action.id, data,
                                                  user.id)
    sampledb.logic.object_permissions.set_object_public(sample.id, True)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Object 1'
        },
        'sample': {
            '_type': 'sample',
            'object_id': sample.id
        }
    }
    sampledb.logic.objects.update_object(object.id, data, user.id)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Measurement'
        },
        'sample': {
            '_type': 'sample',
            'object_id': object.id
        },
        'comment': {
            '_type':
            'text',
            'text':
            'This is a test.\nThis is a second line.\n\nThis line follows an empty line.'
        }
    }
    measurement = sampledb.logic.objects.create_object(measurement_action.id,
                                                       data, user.id)
    sampledb.logic.object_permissions.set_object_public(measurement.id, True)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Measurement 2'
        },
        'sample': {
            '_type': 'sample',
            'object_id': sample.id
        },
        'comment': {
            '_type':
            'text',
            'text':
            'This is a test.\nThis is a second line.\n\nThis line follows an empty line.'
        }
    }
    measurement = sampledb.logic.objects.create_object(
        measurement_action.id, data, instrument_responsible_user.id)
    sampledb.logic.object_permissions.set_object_public(measurement.id, True)

    juelich = sampledb.logic.locations.create_location(
        "FZJ", "Forschungszentrum Jülich", None,
        instrument_responsible_user.id)
    building_04_8 = sampledb.logic.locations.create_location(
        "Building 04.8", "Building 04.8 at Forschungszentrum Jülich",
        juelich.id, instrument_responsible_user.id)
    room_139 = sampledb.logic.locations.create_location(
        "Room 139b", "Building 04.8, Room 139", building_04_8.id,
        instrument_responsible_user.id)
    room_141 = sampledb.logic.locations.create_location(
        "Room 141", "Building 04.8, Room 141", building_04_8.id,
        instrument_responsible_user.id)
    sampledb.logic.locations.assign_location_to_object(
        measurement.id, room_141.id, None, instrument_responsible_user.id,
        "Temporarily stored on table\n\nSome other text")
    sampledb.logic.locations.assign_location_to_object(
        measurement.id, room_141.id, instrument_responsible_user.id,
        basic_user.id, "Stored in shelf K")
    sampledb.logic.notifications.create_other_notification(
        instrument_responsible_user.id, "This is a demo.")
    sampledb.logic.object_permissions.set_user_object_permissions(
        independent_object.id, instrument_responsible_user.id,
        sampledb.models.Permissions.GRANT)
    sampledb.logic.notifications.create_notification_for_having_received_an_objects_permissions_request(
        instrument_responsible_user.id, independent_object.id, admin.id)
Ejemplo n.º 15
0
def setup_data(app):
    # TODO: replace using user management logic
    admin = User(name="Administrator",
                 email="*****@*****.**",
                 type=UserType.PERSON)
    admin.is_admin = True
    instrument_responsible_user = User(name="Instrument Responsible User",
                                       email="*****@*****.**",
                                       type=UserType.PERSON)
    basic_user = User(name="Basic User",
                      email="*****@*****.**",
                      type=UserType.PERSON)
    for user in (admin, instrument_responsible_user, basic_user):
        sampledb.db.session.add(user)
    sampledb.db.session.commit()

    api_user = sampledb.logic.users.create_user(name="API User",
                                                email="*****@*****.**",
                                                type=UserType.OTHER)
    sampledb.logic.authentication.add_other_authentication(
        api_user.id, 'api', 'password')

    group_id = groups.create_group(
        "Example Group", "This is an example group for testing purposes.",
        instrument_responsible_user.id).id

    project_id = projects.create_project("Example Project",
                                         "This is an example project",
                                         instrument_responsible_user.id).id
    project_id2 = projects.create_project("Example Project 2",
                                          "This is another example project",
                                          instrument_responsible_user.id).id
    projects.create_subproject_relationship(parent_project_id=project_id,
                                            child_project_id=project_id2,
                                            child_can_add_users_to_parent=True)

    # Setup autologin for testing
    @app.route('/users/me/autologin')
    @app.route('/users/<int:user_id>/autologin')
    def autologin(user_id=instrument_responsible_user.id):
        user = User.query.get(user_id)
        assert user is not None
        flask_login.login_user(user)
        # Remove the message asking the user to sign in
        flask.session.pop('_flashes', None)
        flask.flash(
            'You have been signed in automatically as part of the SampleDB Demo.',
            'info')
        return flask.redirect(
            os.environ.get('SAMPLEDB_DEMO_REDIRECT_URI',
                           flask.url_for('frontend.index')))

    sampledb.login_manager.login_view = 'autologin'

    markdown_notes = """
This example shows how Markdown can be used for instrument Notes.

## Header

*italics* **bold**


| A | B | C |
|--:|:-:|---|
| Example | 100˚C | 5µm |
| Data | 110˚C | 6µm |
        """
    instrument = create_instrument(
        name="OMBE I",
        description="This is an example instrument.",
        users_can_create_log_entries=True,
        notes=markdown_notes,
        notes_as_html=sampledb.frontend.utils.markdown_to_safe_html(
            markdown_notes))
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    log_category_error = sampledb.logic.instrument_log_entries.create_instrument_log_category(
        instrument_id=instrument.id,
        title='Error',
        theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.
        RED)
    log_category_warning = sampledb.logic.instrument_log_entries.create_instrument_log_category(
        instrument_id=instrument.id,
        title='Warning',
        theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.
        YELLOW)
    log_category_success = sampledb.logic.instrument_log_entries.create_instrument_log_category(
        instrument_id=instrument.id,
        title='Success',
        theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.
        GREEN)
    log_category_other = sampledb.logic.instrument_log_entries.create_instrument_log_category(
        instrument_id=instrument.id,
        title='Other',
        theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.
        BLUE)
    log_category_normal = sampledb.logic.instrument_log_entries.create_instrument_log_category(
        instrument_id=instrument.id,
        title='Normal',
        theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.
        GRAY)
    for category in sampledb.logic.instrument_log_entries.get_instrument_log_categories(
            instrument.id):
        sampledb.logic.instrument_log_entries.create_instrument_log_entry(
            instrument.id, basic_user.id,
            "This is an example instrument log entry", [category.id])
    sampledb.logic.instrument_log_entries.create_instrument_log_entry(
        instrument.id, basic_user.id,
        "This is an example instrument log entry")
    log_entry = sampledb.logic.instrument_log_entries.create_instrument_log_entry(
        instrument.id, basic_user.id,
        "This is an example instrument log entry", [
            log_category_error.id, log_category_warning.id,
            log_category_normal.id, log_category_success.id
        ])
    sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
        instrument_log_entry_id=log_entry.id,
        file_name="ghs01.png",
        content=open('sampledb/static/img/ghs01.png', 'rb').read())
    sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
        instrument_log_entry_id=log_entry.id,
        file_name="ghs02.png",
        content=open('sampledb/static/img/ghs02.png', 'rb').read())
    sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
        instrument_log_entry_id=log_entry.id,
        file_name="test.txt",
        content="This is a test".encode('utf-8'))

    with open('sampledb/schemas/ombe_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.SAMPLE_CREATION,
                                      "Sample Creation",
                                      "This is an example action", schema,
                                      instrument.id)
    independent_action = create_action(ActionType.SAMPLE_CREATION,
                                       "Alternative Process",
                                       "This is an example action", schema)
    with open('sampledb/schemas/ombe_measurement_batch.sampledb.json',
              'r') as schema_file:
        batch_schema = json.load(schema_file)
    create_action(ActionType.SAMPLE_CREATION, "Sample Creation (Batch)",
                  "This is an example action", batch_schema, instrument.id)
    sampledb.db.session.commit()

    with open('example_data/ombe-1.sampledb.json', 'r') as data_file:
        data = json.load(data_file)
    instrument_object = Objects.create_object(
        data=data,
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=instrument_action.id,
        connection=sampledb.db.engine)
    create_object(object_id=instrument_object.object_id,
                  user_id=instrument_responsible_user.id)
    data['multilayer'][0]['repetitions']['magnitude_in_base_units'] = 20000
    data['multilayer'][1]['films'][0]['thickness'][
        'magnitude_in_base_units'] = 1
    independent_object = Objects.create_object(
        data=data,
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=independent_action.id,
        connection=sampledb.db.engine)
    create_object(object_id=independent_object.object_id,
                  user_id=instrument_responsible_user.id)
    comments.create_comment(
        instrument_object.id, instrument_responsible_user.id,
        'This comment is very long. ' * 20 + '\n' +
        'This comment has three paragraphs. ' * 20 + '\n' + '\n' +
        'This comment has three paragraphs. ' * 20)
    comments.create_comment(instrument_object.id,
                            instrument_responsible_user.id,
                            'This is another, shorter comment')
    files.create_local_file(
        instrument_object.id, instrument_responsible_user.id, 'example.txt',
        lambda stream: stream.write("Dies ist ein Test".encode('utf-8')))
    files.create_local_file(
        instrument_object.id, instrument_responsible_user.id, 'demo.png',
        lambda stream: stream.write(
            open('sampledb/static/img/ghs01.png', 'rb').read()))
    files.update_file_information(instrument_object.id, 1,
                                  instrument_responsible_user.id,
                                  'Example File',
                                  'This is a file description.')
    files.create_url_file(instrument_object.id, instrument_responsible_user.id,
                          'http://iffsamples.fz-juelich.de/')

    with open('server_schemas/ombe_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    sampledb.logic.actions.update_action(instrument_action.id,
                                         "Updated Sample Creation", "", schema)

    object_permissions.set_group_object_permissions(
        independent_object.object_id, group_id,
        object_permissions.Permissions.READ)

    instrument = create_instrument(name="XRR",
                                   description="X-Ray Reflectometry")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('sampledb/schemas/xrr_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "XRR Measurement", "", schema,
                                      instrument.id)
    with open('sampledb/schemas/searchable_quantity.json', 'r') as schema_file:
        schema = json.load(schema_file)
    action = create_action(ActionType.SAMPLE_CREATION, "Searchable Object", "",
                           schema, None)
    independent_object = Objects.create_object(
        data={
            "name": {
                "_type": "text",
                "text": "TEST-1"
            },
            "tags": {
                "_type": "tags",
                "tags": ["tag1", "tag2"]
            },
            "mass": {
                "_type": "quantity",
                "dimensionality": "[mass]",
                "magnitude_in_base_units": 0.00001,
                "units": "mg"
            }
        },
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=action.id,
        connection=sampledb.db.engine)
    create_object(object_id=independent_object.object_id,
                  user_id=instrument_responsible_user.id)
    object_permissions.set_group_object_permissions(
        independent_object.object_id, group_id,
        object_permissions.Permissions.READ)
    object_permissions.set_user_object_permissions(
        independent_object.object_id, api_user.id,
        object_permissions.Permissions.WRITE)
    independent_object = Objects.create_object(
        data={
            "name": {
                "_type": "text",
                "text": "TEST-2"
            },
            "tags": {
                "_type": "tags",
                "tags": ["tag2", "tag3"]
            },
            "mass": {
                "_type": "quantity",
                "dimensionality": "[mass]",
                "magnitude_in_base_units": 0.000005,
                "units": "mg"
            }
        },
        schema=schema,
        user_id=instrument_responsible_user.id,
        action_id=action.id,
        connection=sampledb.db.engine)
    create_object(object_id=independent_object.object_id,
                  user_id=instrument_responsible_user.id)
    object_permissions.set_group_object_permissions(
        independent_object.object_id, group_id,
        object_permissions.Permissions.READ)
    sampledb.db.session.commit()

    instrument = create_instrument(
        name="MPMS SQUID", description="MPMS SQUID Magnetometer JCNS-2")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('server_schemas/squid_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "Perform Measurement", "", schema,
                                      instrument.id)
    sampledb.db.session.commit()

    instrument = create_instrument(
        name="Powder Diffractometer",
        description="Huber Imaging Plate Guinier Camera G670 at JCNS-2")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('server_schemas/powder_diffractometer_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "Perform Measurement", "", schema,
                                      instrument.id)
    sampledb.db.session.commit()

    instrument = create_instrument(
        name="GALAXI", description="Gallium Anode Low-Angle X-ray Instrument")
    add_instrument_responsible_user(instrument.id,
                                    instrument_responsible_user.id)
    with open('server_schemas/galaxi_measurement.sampledb.json',
              'r') as schema_file:
        schema = json.load(schema_file)
    instrument_action = create_action(ActionType.MEASUREMENT,
                                      "Perform Measurement", "", schema,
                                      instrument.id)
    sampledb.db.session.commit()

    with open('server_schemas/other_sample.sampledb.json', 'r') as schema_file:
        schema = json.load(schema_file)
    create_action(ActionType.SAMPLE_CREATION, "Other Sample", "", schema, None)
    sampledb.db.session.commit()

    sample_action = sampledb.logic.actions.create_action(
        action_type_id=ActionType.SAMPLE_CREATION,
        name="sample_action",
        description="",
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Object Name',
                    'type': 'text'
                },
                'sample': {
                    'title': 'Sample',
                    'type': 'sample'
                }
            },
            'required': ['name']
        })
    measurement_action = sampledb.logic.actions.create_action(
        action_type_id=ActionType.MEASUREMENT,
        name="measurement_action",
        description="",
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Object Name',
                    'type': 'text'
                },
                'sample': {
                    'title': 'Sample',
                    'type': 'sample'
                },
                'comment': {
                    'title': 'Comment',
                    'type': 'text',
                    'multiline': True
                }
            },
            'required': ['name']
        })
    data = {'name': {'_type': 'text', 'text': 'Object 1'}}
    object = sampledb.logic.objects.create_object(sample_action.id, data,
                                                  user.id)
    sampledb.logic.object_permissions.set_object_public(object.id, True)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Object 2'
        },
        'sample': {
            '_type': 'sample',
            'object_id': object.id
        }
    }
    sample = sampledb.logic.objects.create_object(sample_action.id, data,
                                                  user.id)
    sampledb.logic.object_permissions.set_object_public(sample.id, True)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Object 1'
        },
        'sample': {
            '_type': 'sample',
            'object_id': sample.id
        }
    }
    sampledb.logic.objects.update_object(object.id, data, user.id)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Measurement'
        },
        'sample': {
            '_type': 'sample',
            'object_id': object.id
        },
        'comment': {
            '_type':
            'text',
            'text':
            'This is a test.\nThis is a second line.\n\nThis line follows an empty line.'
        }
    }
    measurement = sampledb.logic.objects.create_object(measurement_action.id,
                                                       data, user.id)
    sampledb.logic.object_permissions.set_object_public(measurement.id, True)
    data = {
        'name': {
            '_type': 'text',
            'text': 'Measurement 2'
        },
        'sample': {
            '_type': 'sample',
            'object_id': sample.id
        },
        'comment': {
            '_type':
            'text',
            'text':
            'This is a test.\nThis is a second line.\n\nThis line follows an empty line.'
        }
    }
    measurement = sampledb.logic.objects.create_object(
        measurement_action.id, data, instrument_responsible_user.id)
    sampledb.logic.object_permissions.set_object_public(measurement.id, True)

    juelich = sampledb.logic.locations.create_location(
        "FZJ", "Forschungszentrum Jülich", None,
        instrument_responsible_user.id)
    building_04_8 = sampledb.logic.locations.create_location(
        "Building 04.8", "Building 04.8 at Forschungszentrum Jülich",
        juelich.id, instrument_responsible_user.id)
    room_139 = sampledb.logic.locations.create_location(
        "Room 139b", "Building 04.8, Room 139", building_04_8.id,
        instrument_responsible_user.id)
    room_141 = sampledb.logic.locations.create_location(
        "Room 141", "Building 04.8, Room 141", building_04_8.id,
        instrument_responsible_user.id)
    sampledb.logic.locations.assign_location_to_object(
        measurement.id, room_141.id, None, instrument_responsible_user.id,
        "Temporarily stored on table\n\nSome other text")
    sampledb.logic.locations.assign_location_to_object(
        measurement.id, room_141.id, instrument_responsible_user.id,
        basic_user.id, "Stored in shelf K")
    sampledb.logic.notifications.create_other_notification(
        instrument_responsible_user.id, "This is a demo.")
    sampledb.logic.object_permissions.set_user_object_permissions(
        independent_object.id, instrument_responsible_user.id,
        sampledb.models.Permissions.GRANT)
    sampledb.logic.notifications.create_notification_for_having_received_an_objects_permissions_request(
        instrument_responsible_user.id, independent_object.id, admin.id)

    for i in range(1, 8):
        sampledb.logic.instrument_log_entries.create_instrument_log_object_attachment(
            instrument_log_entry_id=log_entry.id, object_id=i)
def instrument():
    instrument = instruments.create_instrument(
        'Example Instrument', 'Example Instrument Description')
    assert instrument.id is not None
    db.session.expunge(instrument)
    return instrument
Ejemplo n.º 17
0
def instrument():
    return instruments.create_instrument('Example Instrument',
                                         'Example Instrument Description')
Ejemplo n.º 18
0
def main(arguments):
    if len(arguments) != 0:
        print(__doc__)
        exit(1)
    app = create_app()
    if not app.config.get("SERVER_NAME"):
        app.config["SERVER_NAME"] = "localhost:8000"
    with app.app_context():
        if sampledb.logic.actions.get_actions(
        ) or sampledb.logic.instruments.get_instruments() or len(
                sampledb.logic.users.get_users()) > 1:
            print("Error: database must be empty for demo", file=sys.stderr)
            exit(1)

        data_directory = os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'demo_data'))
        schema_directory = os.path.join(data_directory, 'schemas')
        objects_directory = os.path.join(data_directory, 'objects')

        if sampledb.logic.users.get_users():
            # admin might have been created from environment variables
            admin = sampledb.logic.users.get_users()[0]
        else:
            admin = sampledb.logic.users.create_user(name="Administrator",
                                                     email="*****@*****.**",
                                                     type=UserType.PERSON)
            sampledb.logic.users.set_user_administrator(admin.id, True)

        instrument_responsible_user = sampledb.logic.users.create_user(
            name="Instrument Scientist",
            email="*****@*****.**",
            type=UserType.PERSON)
        basic_user = sampledb.logic.users.create_user(
            name="Basic User", email="*****@*****.**", type=UserType.PERSON)
        api_user = sampledb.logic.users.create_user(name="API User",
                                                    email="*****@*****.**",
                                                    type=UserType.OTHER)
        sampledb.logic.authentication.add_other_authentication(
            api_user.id, 'api', 'password')

        group_id = groups.create_group(
            "Example Group", "This is an example group for testing purposes.",
            instrument_responsible_user.id).id

        project_id = projects.create_project("Example Project",
                                             "This is an example project",
                                             instrument_responsible_user.id).id
        project_id2 = projects.create_project(
            "Example Project 2", "This is another example project",
            instrument_responsible_user.id).id
        projects.create_subproject_relationship(
            parent_project_id=project_id,
            child_project_id=project_id2,
            child_can_add_users_to_parent=True)

        markdown_notes = """# Header
    This example shows how Markdown can be used for instrument Notes.

    ## Subheader

    *italics* **bold**


    | A | B | C |
    |--:|:-:|---|
    | Example | 100˚C | 5µm |
    | Data | 110˚C | 6µm |
            """
        instrument = create_instrument(
            name="OMBE I",
            description="This is an example instrument.",
            users_can_create_log_entries=True,
            notes=markdown_notes,
            notes_is_markdown=True)
        add_instrument_responsible_user(instrument.id,
                                        instrument_responsible_user.id)
        log_category_error = sampledb.logic.instrument_log_entries.create_instrument_log_category(
            instrument_id=instrument.id,
            title='Error',
            theme=sampledb.logic.instrument_log_entries.
            InstrumentLogCategoryTheme.RED)
        log_category_warning = sampledb.logic.instrument_log_entries.create_instrument_log_category(
            instrument_id=instrument.id,
            title='Warning',
            theme=sampledb.logic.instrument_log_entries.
            InstrumentLogCategoryTheme.YELLOW)
        log_category_success = sampledb.logic.instrument_log_entries.create_instrument_log_category(
            instrument_id=instrument.id,
            title='Success',
            theme=sampledb.logic.instrument_log_entries.
            InstrumentLogCategoryTheme.GREEN)
        log_category_other = sampledb.logic.instrument_log_entries.create_instrument_log_category(
            instrument_id=instrument.id,
            title='Other',
            theme=sampledb.logic.instrument_log_entries.
            InstrumentLogCategoryTheme.BLUE)
        log_category_normal = sampledb.logic.instrument_log_entries.create_instrument_log_category(
            instrument_id=instrument.id,
            title='Normal',
            theme=sampledb.logic.instrument_log_entries.
            InstrumentLogCategoryTheme.GRAY)
        for category in sampledb.logic.instrument_log_entries.get_instrument_log_categories(
                instrument.id):
            sampledb.logic.instrument_log_entries.create_instrument_log_entry(
                instrument.id, basic_user.id,
                "This is an example instrument log entry", [category.id])
        sampledb.logic.instrument_log_entries.create_instrument_log_entry(
            instrument.id, basic_user.id,
            "This is an example instrument log entry")
        sampledb.logic.instrument_log_entries.create_instrument_log_entry(
            instrument.id, basic_user.id,
            "This is an example instrument log entry", [log_category_other.id])
        log_entry = sampledb.logic.instrument_log_entries.create_instrument_log_entry(
            instrument.id, basic_user.id,
            "This is an example instrument log entry", [
                log_category_error.id, log_category_warning.id,
                log_category_normal.id, log_category_success.id
            ])
        sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
            instrument_log_entry_id=log_entry.id,
            file_name="ghs01.png",
            content=open(
                os.path.join(os.path.dirname(sampledb.__file__),
                             'static/img/ghs01.png'), 'rb').read())
        sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
            instrument_log_entry_id=log_entry.id,
            file_name="ghs02.png",
            content=open(
                os.path.join(os.path.dirname(sampledb.__file__),
                             'static/img/ghs02.png'), 'rb').read())
        sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
            instrument_log_entry_id=log_entry.id,
            file_name="test.txt",
            content="This is a test".encode('utf-8'))

        with open(os.path.join(schema_directory,
                               'ombe_measurement.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        instrument_action = create_action(ActionType.SAMPLE_CREATION,
                                          "Sample Creation",
                                          "This is an example action", schema,
                                          instrument.id)
        sampledb.logic.action_permissions.set_action_public(
            instrument_action.id)
        independent_action = create_action(ActionType.SAMPLE_CREATION,
                                           "Alternative Process",
                                           "This is an example action", schema)
        sampledb.logic.action_permissions.set_action_public(
            independent_action.id)
        with open(os.path.join(schema_directory,
                               'ombe_measurement_batch.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            batch_schema = json.load(schema_file)
        action = create_action(ActionType.SAMPLE_CREATION,
                               "Sample Creation (Batch)",
                               "This is an example action", batch_schema,
                               instrument.id)
        sampledb.logic.action_permissions.set_action_public(action.id)
        sampledb.db.session.commit()

        with open(os.path.join(objects_directory, 'ombe-1.sampledb.json'),
                  'r',
                  encoding='utf-8') as data_file:
            data = json.load(data_file)
        instrument_object = Objects.create_object(
            data=data,
            schema=schema,
            user_id=instrument_responsible_user.id,
            action_id=instrument_action.id,
            connection=sampledb.db.engine)
        create_object(object_id=instrument_object.object_id,
                      user_id=instrument_responsible_user.id)
        independent_object = Objects.create_object(
            data=data,
            schema=schema,
            user_id=instrument_responsible_user.id,
            action_id=independent_action.id,
            connection=sampledb.db.engine)
        create_object(object_id=independent_object.object_id,
                      user_id=instrument_responsible_user.id)
        comments.create_comment(
            instrument_object.id, instrument_responsible_user.id,
            'This comment is very long. ' * 20 + '\n' +
            'This comment has three paragraphs. ' * 20 + '\n' + '\n' +
            'This comment has three paragraphs. ' * 20)
        comments.create_comment(instrument_object.id,
                                instrument_responsible_user.id,
                                'This is another, shorter comment')
        files.create_local_file(
            instrument_object.id, instrument_responsible_user.id,
            'example.txt',
            lambda stream: stream.write("Dies ist ein Test".encode('utf-8')))
        files.create_local_file(
            instrument_object.id, instrument_responsible_user.id, 'demo.png',
            lambda stream: stream.write(
                open(
                    os.path.join(os.path.dirname(sampledb.__file__),
                                 'static/img/ghs01.png'), 'rb').read()))
        files.update_file_information(instrument_object.id, 1,
                                      instrument_responsible_user.id,
                                      'Example File',
                                      'This is a file description.')
        files.create_url_file(instrument_object.id,
                              instrument_responsible_user.id,
                              'http://iffsamples.fz-juelich.de/')

        projects.link_project_and_object(project_id,
                                         instrument_object.object_id,
                                         instrument_responsible_user.id)

        with open(os.path.join(schema_directory,
                               'ombe_measurement_updated.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        sampledb.logic.actions.update_action(instrument_action.id,
                                             "Updated Sample Creation", "",
                                             schema)

        object_permissions.set_group_object_permissions(
            independent_object.object_id, group_id,
            object_permissions.Permissions.READ)

        instrument = create_instrument(name="XRR",
                                       description="X-Ray Reflectometry")
        add_instrument_responsible_user(instrument.id,
                                        instrument_responsible_user.id)
        with open(os.path.join(schema_directory,
                               'xrr_measurement.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        instrument_action = create_action(ActionType.MEASUREMENT,
                                          "XRR Measurement", "", schema,
                                          instrument.id)
        sampledb.logic.action_permissions.set_action_public(
            instrument_action.id)
        with open(os.path.join(schema_directory, 'searchable_quantity.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        action = create_action(ActionType.SAMPLE_CREATION, "Searchable Object",
                               "", schema, None)
        sampledb.logic.action_permissions.set_action_public(action.id)
        independent_object = Objects.create_object(
            data={
                "name": {
                    "_type": "text",
                    "text": "TEST-1"
                },
                "tags": {
                    "_type": "tags",
                    "tags": ["tag1", "tag2"]
                },
                "mass": {
                    "_type": "quantity",
                    "dimensionality": "[mass]",
                    "magnitude_in_base_units": 0.00001,
                    "units": "mg"
                }
            },
            schema=schema,
            user_id=instrument_responsible_user.id,
            action_id=action.id,
            connection=sampledb.db.engine)
        create_object(object_id=independent_object.object_id,
                      user_id=instrument_responsible_user.id)
        object_permissions.set_group_object_permissions(
            independent_object.object_id, group_id,
            object_permissions.Permissions.READ)
        object_permissions.set_user_object_permissions(
            independent_object.object_id, api_user.id,
            object_permissions.Permissions.WRITE)
        independent_object = Objects.create_object(
            data={
                "name": {
                    "_type": "text",
                    "text": "TEST-2"
                },
                "tags": {
                    "_type": "tags",
                    "tags": ["tag2", "tag3"]
                },
                "mass": {
                    "_type": "quantity",
                    "dimensionality": "[mass]",
                    "magnitude_in_base_units": 0.000005,
                    "units": "mg"
                }
            },
            schema=schema,
            user_id=instrument_responsible_user.id,
            action_id=action.id,
            connection=sampledb.db.engine)
        create_object(object_id=independent_object.object_id,
                      user_id=instrument_responsible_user.id)
        object_permissions.set_group_object_permissions(
            independent_object.object_id, group_id,
            object_permissions.Permissions.READ)
        sampledb.db.session.commit()

        instrument = create_instrument(
            name="MPMS SQUID", description="MPMS SQUID Magnetometer JCNS-2")
        add_instrument_responsible_user(instrument.id,
                                        instrument_responsible_user.id)
        with open(os.path.join(schema_directory,
                               'squid_measurement.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        instrument_action = create_action(ActionType.MEASUREMENT,
                                          "Perform Measurement", "", schema,
                                          instrument.id)
        sampledb.logic.action_permissions.set_action_public(
            instrument_action.id)
        sampledb.db.session.commit()

        instrument = create_instrument(
            name="Powder Diffractometer",
            description="Huber Imaging Plate Guinier Camera G670 at JCNS-2")
        add_instrument_responsible_user(instrument.id,
                                        instrument_responsible_user.id)
        with open(os.path.join(
                schema_directory,
                'powder_diffractometer_measurement.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        instrument_action = create_action(ActionType.MEASUREMENT,
                                          "Perform Measurement", "", schema,
                                          instrument.id)
        sampledb.logic.action_permissions.set_action_public(
            instrument_action.id)
        sampledb.db.session.commit()

        instrument = create_instrument(
            name="GALAXI",
            description="Gallium Anode Low-Angle X-ray Instrument")
        add_instrument_responsible_user(instrument.id,
                                        instrument_responsible_user.id)
        with open(os.path.join(schema_directory,
                               'galaxi_measurement.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        instrument_action = create_action(ActionType.MEASUREMENT,
                                          "Perform Measurement", "", schema,
                                          instrument.id)
        sampledb.logic.action_permissions.set_action_public(
            instrument_action.id)
        sampledb.db.session.commit()

        with open(os.path.join(schema_directory, 'other_sample.sampledb.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        action = create_action(ActionType.SAMPLE_CREATION, "Other Sample", "",
                               schema, None)
        sampledb.logic.action_permissions.set_action_public(action.id)
        sampledb.db.session.commit()

        sample_action = sampledb.logic.actions.create_action(
            action_type_id=ActionType.SAMPLE_CREATION,
            name="sample_action",
            description="",
            schema={
                'title': 'Example Object',
                'type': 'object',
                'properties': {
                    'name': {
                        'title': 'Object Name',
                        'type': 'text'
                    },
                    'sample': {
                        'title': 'Sample',
                        'type': 'sample'
                    }
                },
                'required': ['name']
            })
        sampledb.logic.action_permissions.set_action_public(sample_action.id)
        measurement_action = sampledb.logic.actions.create_action(
            action_type_id=ActionType.MEASUREMENT,
            name="measurement_action",
            description="",
            schema={
                'title': 'Example Object',
                'type': 'object',
                'properties': {
                    'name': {
                        'title': 'Object Name',
                        'type': 'text'
                    },
                    'sample': {
                        'title': 'Sample',
                        'type': 'sample'
                    },
                    'comment': {
                        'title': 'Comment',
                        'type': 'text',
                        'markdown': True
                    }
                },
                'required': ['name']
            })
        sampledb.logic.action_permissions.set_action_public(
            measurement_action.id)
        data = {'name': {'_type': 'text', 'text': 'Object 1'}}
        object = sampledb.logic.objects.create_object(sample_action.id, data,
                                                      basic_user.id)
        sampledb.logic.object_permissions.set_object_public(object.id, True)
        data = {
            'name': {
                '_type': 'text',
                'text': 'Object 2'
            },
            'sample': {
                '_type': 'sample',
                'object_id': object.id
            }
        }
        sample = sampledb.logic.objects.create_object(sample_action.id, data,
                                                      basic_user.id)
        sampledb.logic.object_permissions.set_object_public(sample.id, True)
        data = {
            'name': {
                '_type': 'text',
                'text': 'Object 1'
            },
            'sample': {
                '_type': 'sample',
                'object_id': sample.id
            }
        }
        sampledb.logic.objects.update_object(object.id, data, basic_user.id)
        data = {
            'name': {
                '_type': 'text',
                'text': 'Measurement'
            },
            'sample': {
                '_type': 'sample',
                'object_id': object.id
            },
            'comment': {
                '_type': 'text',
                'text':
                'This is a test.\nThis **is** a *second* line.\n\nThis line follows an empty line.',
                'is_markdown': True
            }
        }
        measurement = sampledb.logic.objects.create_object(
            measurement_action.id, data, basic_user.id)
        sampledb.logic.object_permissions.set_object_public(
            measurement.id, True)
        data = {
            'name': {
                '_type': 'text',
                'text': 'Measurement 2'
            },
            'sample': {
                '_type': 'sample',
                'object_id': sample.id
            },
            'comment': {
                '_type':
                'text',
                'text':
                'This is a test.\nThis is a second line.\n\nThis line follows an empty line.'
            }
        }
        measurement = sampledb.logic.objects.create_object(
            measurement_action.id, data, instrument_responsible_user.id)
        sampledb.logic.object_permissions.set_object_public(
            measurement.id, True)

        with open(os.path.join(schema_directory, 'plotly.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        plotly_action = create_action(ActionType.SAMPLE_CREATION,
                                      "Plotly Example Action", "", schema,
                                      None)
        sampledb.logic.action_permissions.set_action_public(plotly_action.id)

        with open(os.path.join(objects_directory,
                               'plotly-example-data1.sampledb.json'),
                  'r',
                  encoding='utf-8') as data_file:
            example_data = json.load(data_file)

        data = {
            "name": {
                "_type": "text",
                "text": "Plotly Example Data #1"
            },
            "plot1": {
                "_type": "plotly_chart",
                "plotly": example_data
            }
        }
        plotly_object = sampledb.logic.objects.create_object(
            plotly_action.id, data, basic_user.id)
        sampledb.logic.object_permissions.set_object_public(
            plotly_object.id, True)

        with open(os.path.join(schema_directory, 'plotly_array.json'),
                  'r',
                  encoding='utf-8') as schema_file:
            schema = json.load(schema_file)
        plotly_array_action = create_action(ActionType.SAMPLE_CREATION,
                                            "Plotly Array Example Action", "",
                                            schema, None)
        sampledb.logic.action_permissions.set_action_public(
            plotly_array_action.id)

        with open(os.path.join(objects_directory,
                               'plotly-example-data2.sampledb.json'),
                  'r',
                  encoding='utf-8') as data_file:
            example_data2 = json.load(data_file)
        with open(os.path.join(objects_directory,
                               'plotly-example-data3.sampledb.json'),
                  'r',
                  encoding='utf-8') as data_file:
            example_data3 = json.load(data_file)

        data = {
            "name": {
                "_type": "text",
                "text": "Plotly Array Example"
            },
            "plotlist": [{
                "_type": "plotly_chart",
                "plotly": example_data
            }, {
                "_type": "plotly_chart",
                "plotly": example_data2
            }, {
                "_type": "plotly_chart",
                "plotly": example_data3
            }]
        }
        plotly_object = sampledb.logic.objects.create_object(
            plotly_array_action.id, data, basic_user.id)
        sampledb.logic.object_permissions.set_object_public(
            plotly_object.id, True)
        sampledb.db.session.commit()

        campus = sampledb.logic.locations.create_location(
            "Campus", "Max Mustermann Campus", None,
            instrument_responsible_user.id)
        building_a = sampledb.logic.locations.create_location(
            "Building A", "Building A on Max Mustermann Campus", campus.id,
            instrument_responsible_user.id)
        room_42a = sampledb.logic.locations.create_location(
            "Room 42a", "Building A, Room 42a", building_a.id,
            instrument_responsible_user.id)
        room_42b = sampledb.logic.locations.create_location(
            "Room 42b", "Building A, Room 42b", building_a.id,
            instrument_responsible_user.id)
        sampledb.logic.locations.assign_location_to_object(
            measurement.id, room_42a.id, None, instrument_responsible_user.id,
            "Temporarily stored on table\n\nSome other text")
        sampledb.logic.locations.assign_location_to_object(
            measurement.id, room_42b.id, instrument_responsible_user.id,
            basic_user.id, "Stored in shelf K")
        sampledb.logic.notifications.create_other_notification(
            instrument_responsible_user.id, "This is a demo.")
        sampledb.logic.object_permissions.set_user_object_permissions(
            independent_object.id, instrument_responsible_user.id,
            sampledb.models.Permissions.GRANT)
        sampledb.logic.notifications.create_notification_for_having_received_an_objects_permissions_request(
            instrument_responsible_user.id, independent_object.id, admin.id)

        for object in sampledb.logic.objects.get_objects():
            sampledb.logic.instrument_log_entries.create_instrument_log_object_attachment(
                instrument_log_entry_id=log_entry.id, object_id=object.id)
    print("Success: set up demo data")