コード例 #1
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_create_action_invalid_schema():
    schema = {'type': 'invalid'}
    with pytest.raises(errors.ValidationError):
        actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                              name="Example Action",
                              description="",
                              schema=schema)
コード例 #2
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
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)
コード例 #3
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_get_missing_action():
    assert len(actions.get_actions()) == 0
    action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                                   name="Example Action",
                                   description="",
                                   schema=SCHEMA)
    with pytest.raises(errors.ActionDoesNotExistError):
        actions.get_action(action.id + 1)
コード例 #4
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_get_actions():
    measurement_action = actions.create_action(
        sampledb.models.ActionType.MEASUREMENT,
        name="Example Action",
        description="",
        schema=SCHEMA)
    sample_action = actions.create_action(
        sampledb.models.ActionType.SAMPLE_CREATION,
        name="Example Action",
        description="",
        schema=SCHEMA)
    assert actions.get_actions() == [
        measurement_action, sample_action
    ] or actions.get_actions() == [sample_action, measurement_action]
    assert actions.get_actions(
        sampledb.models.ActionType.SAMPLE_CREATION) == [sample_action]
    assert actions.get_actions(
        sampledb.models.ActionType.MEASUREMENT) == [measurement_action]
コード例 #5
0
def action(instrument, schema_file_name):
    with open(schema_file_name) as schema_file:
        schema = json.load(schema_file)
    return actions.create_action(
        action_type=actions.ActionType.SAMPLE_CREATION,
        name='Example Action',
        description='Example Action Description',
        schema=schema,
        instrument_id=instrument.id)
コード例 #6
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_update_missing_action():
    action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                                   name="Example Action",
                                   description="",
                                   schema=SCHEMA)
    with pytest.raises(errors.ActionDoesNotExistError):
        actions.update_action(action_id=action.id + 1,
                              name="Test",
                              description="desc",
                              schema=SCHEMA)
コード例 #7
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_create_independent_action():
    assert len(actions.get_actions()) == 0
    action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                                   name="Example Action",
                                   description="",
                                   schema=SCHEMA)
    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)
コード例 #8
0
def action(instrument, schema_file_name):
    with open(schema_file_name) as schema_file:
        schema = json.load(schema_file)
    action = actions.create_action(
        action_type_id=sampledb.models.ActionType.SAMPLE_CREATION,
        name='Example Action',
        description='Example Action Description',
        schema=schema,
        instrument_id=instrument.id)
    assert action.id is not None
    db.session.expunge(action)
    return action
コード例 #9
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_update_action():
    action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                                   name="Example Action",
                                   description="",
                                   schema=SCHEMA)
    actions.update_action(action_id=action.id,
                          name="Test",
                          description="desc",
                          schema=SCHEMA)
    action = actions.get_action(action_id=action.id)
    assert action.name == "Test"
    assert action.description == "desc"
    assert action.schema == SCHEMA
コード例 #10
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
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
コード例 #11
0
def action():
    action = actions.create_action(action_type=ActionType.SAMPLE_CREATION,
                                   name='Example Action',
                                   schema={
                                       'title': 'Example Object',
                                       'type': 'object',
                                       'properties': {
                                           'name': {
                                               'title': 'Sample Name',
                                               'type': 'text'
                                           }
                                       },
                                       'required': ['name']
                                   },
                                   description='',
                                   instrument_id=None)
    return action
コード例 #12
0
ファイル: test_actions.py プロジェクト: FlorianRhiem/sampledb
def test_create_user_action():
    user = users.User(name="Testuser",
                      email="*****@*****.**",
                      type=sampledb.models.UserType.PERSON)
    sampledb.db.session.add(user)
    sampledb.db.session.commit()
    assert len(actions.get_actions()) == 0
    action = actions.create_action(sampledb.models.ActionType.SAMPLE_CREATION,
                                   name="Example Action",
                                   description="",
                                   schema=SCHEMA,
                                   user_id=user.id)
    assert action.name == "Example Action"
    assert action.description == ""
    assert action.schema == SCHEMA
    assert action.user_id == user.id
    assert len(actions.get_actions()) == 1
    assert action == actions.get_action(action_id=action.id)
コード例 #13
0
ファイル: set_up_demo.py プロジェクト: sciapp/sampledb
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")
コード例 #14
0
ファイル: __init__.py プロジェクト: MayerBjoern/sampledb
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)
コード例 #15
0
def set_up_state(user: User):
    action = actions.create_action(
        action_type_id=sampledb.models.ActionType.SAMPLE_CREATION,
        name='Example Action',
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Sample Name',
                    'type': 'text'
                }
            },
            'required': ['name']
        },
        description='',
        instrument_id=None
    )
    actions.create_action(
        action_type_id=sampledb.models.ActionType.SAMPLE_CREATION,
        name='Irrelevant Action',
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Sample Name',
                    'type': 'text'
                }
            },
            'required': ['name']
        },
        description='',
        instrument_id=None
    )
    data = {'name': {'_type': 'text', 'text': 'Object'}}
    object = objects.create_object(user_id=user.id, action_id=action.id, data=data)
    def save_content(file): file.write("This is a test file.".encode('utf-8'))
    files.create_local_file(object.id, user.id, "test.txt", save_content)
    files.create_url_file(object.id, user.id, "https://example.com")

    instrument = sampledb.logic.instruments.create_instrument(
        name="Example Instrument",
        description="Example Instrument Description",
        users_can_view_log_entries=True
    )
    category = sampledb.logic.instrument_log_entries.create_instrument_log_category(
        instrument_id=instrument.id,
        title="Category",
        theme=sampledb.logic.instrument_log_entries.InstrumentLogCategoryTheme.RED
    )
    log_entry = sampledb.logic.instrument_log_entries.create_instrument_log_entry(
        instrument_id=instrument.id,
        user_id=user.id,
        content="Example Log Entry Text",
        category_ids=[category.id]
    )
    sampledb.logic.instrument_log_entries.create_instrument_log_file_attachment(
        instrument_log_entry_id=log_entry.id,
        file_name="example.txt",
        content=b'Example Content'
    )
    sampledb.logic.instrument_log_entries.create_instrument_log_object_attachment(
        instrument_log_entry_id=log_entry.id,
        object_id=object.id
    )
コード例 #16
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)
コード例 #17
0
ファイル: test_rdf.py プロジェクト: NicolasCARPi/sampledb
def test_generate_rdf(flask_server, user, other_user):
    action = actions.create_action(
        action_type=ActionType.SAMPLE_CREATION,
        name='Example Action',
        schema={
            'title': 'Example Object',
            'type': 'object',
            'properties': {
                'name': {
                    'title': 'Sample Name',
                    'type': 'text'
                }
            },
            'required': ['name']
        },
        description='',
        instrument_id=None
    )
    data = {'name': {'_type': 'text', 'text': 'Example Object'}}
    object = objects.create_object(user_id=user.id, action_id=action.id, data=data)

    comments.create_comment(object.id, other_user.id, 'Example Comment')

    objects.update_object(object.id, data, user.id)

    objects.update_object(object.id, data, other_user.id)

    flask_server.app.config['SERVER_NAME'] = urlparse(flask_server.base_url).netloc
    with flask_server.app.app_context():
        rdf_document = rdf.generate_rdf(user.id, object.id)

    etree.register_namespace('dcterms', 'http://purl.org/dc/terms/')
    et = etree.fromstring(rdf_document)

    assert next(et.iter('{*}title')).text == 'Example Object'
    assert next(et.iter('{*}identifier')).text == f'{flask_server.base_url}objects/{object.id}'
    creators = list(et.iter('{*}creator'))
    assert len(creators) == 2
    assert creators[0][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{user.id}'
    assert creators[0][0][0].text == user.name
    assert creators[1][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{other_user.id}'
    assert creators[1][0][0].text == other_user.name
    contributors = list(et.iter('{*}contributor'))
    assert len(contributors) == 0
    versions = list(et.iter('{*}hasVersion'))
    assert len(versions) == 3
    assert versions[0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}/versions/0'
    assert versions[1].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}/versions/1'
    assert versions[2].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}/versions/2'
    is_version_of = list(et.iter('{*}isVersionOf'))
    assert len(is_version_of) == 0

    flask_server.app.config['SERVER_NAME'] = urlparse(flask_server.base_url).netloc
    with flask_server.app.app_context():
        rdf_document = rdf.generate_rdf(user.id, object.id, 1)

    etree.register_namespace('dcterms', 'http://purl.org/dc/terms/')
    et = etree.fromstring(rdf_document)

    assert next(et.iter('{*}title')).text == 'Example Object'
    assert next(et.iter('{*}identifier')).text == f'{flask_server.base_url}objects/{object.id}/versions/1'
    creators = list(et.iter('{*}creator'))
    assert len(creators) == 1
    assert creators[0][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{user.id}'
    assert creators[0][0][0].text == user.name
    contributors = list(et.iter('{*}contributor'))
    assert len(contributors) == 1
    assert contributors[0][0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') == f'{flask_server.base_url}users/{other_user.id}'
    assert contributors[0][0][0].text == other_user.name
    versions = list(et.iter('{*}hasVersion'))
    assert len(versions) == 0
    is_version_of = list(et.iter('{*}isVersionOf'))
    assert len(is_version_of) == 1
    assert is_version_of[0].get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}resource') == f'{flask_server.base_url}objects/{object.id}'