Exemple #1
0
 def post(self, object_id: int):
     request_json = flask.request.get_json(force=True)
     if not isinstance(request_json, dict):
         return {"message": "JSON object body required"}, 400
     for key in request_json:
         if key not in {
                 'object_id', 'version_id', 'action_id', 'schema', 'data'
         }:
             return {"message": "invalid key '{}'".format(key)}, 400
     object = get_object(object_id=object_id)
     action = get_action(action_id=object.action_id)
     if 'object_id' in request_json:
         if request_json['object_id'] != object.object_id:
             return {
                 "message": "object_id must be {}".format(object.object_id)
             }, 400
     if 'version_id' in request_json:
         if request_json['version_id'] != object.version_id + 1:
             return {
                 "message":
                 "version_id must be {}".format(object.version_id + 1)
             }, 400
     if 'action_id' in request_json:
         if request_json['action_id'] != object.action_id:
             return {
                 "message": "action_id must be {}".format(object.action_id)
             }, 400
     if 'schema' in request_json:
         if request_json['schema'] != object.schema and request_json[
                 'schema'] != action.schema:
             return {
                 "message":
                 "schema must be either:\n{}\nor:\n{}".format(
                     json.dumps(object.schema, indent=4),
                     json.dumps(action.schema, indent=4))
             }, 400
         schema = request_json['schema']
     else:
         schema = object.schema
     if 'data' not in request_json:
         return {"message": "data must be set"}, 400
     data = request_json['data']
     try:
         update_object(object_id=object.object_id,
                       data=data,
                       user_id=flask.g.user.id,
                       schema=schema)
     except errors.ValidationError as e:
         messages = e.message.splitlines()
         return {
             "message":
             "validation failed:\n - {}".format('\n - '.join(messages))
         }, 400
     except Exception:
         return {"message": "failed to update object"}, 400
     object_version_url = flask.url_for('api.object_version',
                                        object_id=object.object_id,
                                        version_id=object.version_id + 1)
     return flask.redirect(object_version_url, code=201)
Exemple #2
0
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}'