Esempio n. 1
0
def put_resource_data(resource_id):
    content_type = 'application/octet-stream'
    if request.headers.get('Content-type'):
        content_type, _ = parse_header(request.headers['Content-type'])

    with db.cursor() as cur:
        cur.execute(
            """
        SELECT id, data_oid FROM "resource" WHERE id = %(id)s;
        """, dict(id=resource_id))
        resource = cur.fetchone()

    if resource is None:
        raise NotFound()

    # todo: better use a streaming response here..?
    with db, db.cursor() as cur:
        lobj = db.lobject(oid=resource['data_oid'], mode='wb')
        lobj.seek(0)
        lobj.truncate()
        lobj.write(request.data)
        lobj.close()

        resource_hash = 'sha1:' + hashlib.sha1(request.data).hexdigest()

        data = dict(id=resource_id,
                    mimetype=content_type,
                    mtime=datetime.datetime.utcnow(),
                    hash=resource_hash)

        query = querybuilder.update('resource', data)
        cur.execute(query, data)

    return '', 200
Esempio n. 2
0
    def resource_data_update(self, objid, stream=None, metadata=None,
                             mimetype=None):

        # Get the original object, to check for its existence
        # and to get the oid of the lobject holding the data.
        original = self.resource_data_get_info(objid)

        data = {
            'id': objid,
            'mtime': datetime.now(),
        }

        if metadata is not None:
            data['metadata'] = json.dumps(metadata)
        if mimetype is not None:
            data['mimetype'] = mimetype

        with self.db, self.db.cursor() as cur:
            if stream is not None:
                # Update the lobject with data from the stream
                resource_hash = hashlib.sha1()
                lobj = self.db.lobject(oid=original['data_oid'], mode='wb')
                for chunk in file_read_chunks(stream):
                    lobj.write(chunk)
                    resource_hash.update(chunk)
                lobj.close()
                data['hash'] = 'sha1:{0}'.format(resource_hash.hexdigest())

            query = querybuilder.update('resource_data', data)
            cur.execute(query, data)
Esempio n. 3
0
    def resource_data_update(self,
                             objid,
                             stream=None,
                             metadata=None,
                             mimetype=None):

        # Get the original object, to check for its existence
        # and to get the oid of the lobject holding the data.
        original = self.resource_data_get_info(objid)

        data = {
            'id': objid,
            'mtime': datetime.now(),
        }

        if metadata is not None:
            data['metadata'] = json.dumps(metadata)
        if mimetype is not None:
            data['mimetype'] = mimetype

        with self.db, self.db.cursor() as cur:
            if stream is not None:
                # Update the lobject with data from the stream
                resource_hash = hashlib.sha1()
                lobj = self.db.lobject(oid=original['data_oid'], mode='wb')
                for chunk in file_read_chunks(stream):
                    lobj.write(chunk)
                    resource_hash.update(chunk)
                lobj.close()
                data['hash'] = 'sha1:{0}'.format(resource_hash.hexdigest())

            query = querybuilder.update('resource_data', data)
            cur.execute(query, data)
Esempio n. 4
0
def put_resource_data(resource_id):
    content_type = "application/octet-stream"
    if request.headers.get("Content-type"):
        content_type, _ = parse_header(request.headers["Content-type"])

    with db.cursor() as cur:
        cur.execute(
            """
        SELECT id, data_oid FROM "resource" WHERE id = %(id)s;
        """,
            dict(id=resource_id),
        )
        resource = cur.fetchone()

    if resource is None:
        raise NotFound()

    # todo: better use a streaming response here..?
    with db, db.cursor() as cur:
        lobj = db.lobject(oid=resource["data_oid"], mode="wb")
        lobj.seek(0)
        lobj.truncate()
        lobj.write(request.data)
        lobj.close()

        resource_hash = "sha1:" + hashlib.sha1(request.data).hexdigest()

        data = dict(id=resource_id, mimetype=content_type, mtime=datetime.datetime.utcnow(), hash=resource_hash)

        query = querybuilder.update("resource", data)
        cur.execute(query, data)

    return "", 200
Esempio n. 5
0
def test_querybuilder_update():
    data = {'foo': 'FOO', 'bar': 'BAR'}
    query = querybuilder.update('mytable', sorted(data.keys()))
    assert query == ('UPDATE "mytable" SET "bar"=%(bar)s, "foo"=%(foo)s '
                     'WHERE "id"=%(id)s')

    data = {'foo': 'FOO', 'bar': 'BAR', 'id': 123}
    query = querybuilder.update('mytable', sorted(data.keys()))
    assert query == ('UPDATE "mytable" SET "bar"=%(bar)s, "foo"=%(foo)s '
                     'WHERE "id"=%(id)s')

    data = {'foo': 'FOO', 'bar': 'BAR', 'myid': 123}
    query = querybuilder.update('mytable',
                                sorted(data.keys()),
                                table_key='myid')
    assert query == ('UPDATE "mytable" SET "bar"=%(bar)s, "foo"=%(foo)s '
                     'WHERE "myid"=%(myid)s')
Esempio n. 6
0
 def _dsres_update(self, name, obj_id, obj):
     data = {
         'id': obj_id,
         'configuration': json.dumps(obj),
         'mtime': datetime.now(),
     }
     query = querybuilder.update(name, data)
     with self.db, self.db.cursor() as cur:
         cur.execute(query, data)
Esempio n. 7
0
 def _dsres_update(self, name, obj_id, obj):
     data = {
         'id': obj_id,
         'configuration': json.dumps(obj),
         'mtime': datetime.now(),
     }
     query = querybuilder.update(name, data)
     with self.db, self.db.cursor() as cur:
         cur.execute(query, data)
Esempio n. 8
0
def _update_dataset_record(dataset_id, fields):
    fields['id'] = dataset_id
    _configuration = None
    if 'configuration' in fields:
        _configuration = fields['configuration']
        fields['configuration'] = json.dumps(fields['configuration'])
    fields['mtime'] = datetime.datetime.utcnow()
    query = querybuilder.update('dataset', fields)

    with db, db.cursor() as cur:
        cur.execute(query, fields)

    current_app.plugins.call_hook('dataset_update', dataset_id, _configuration)
Esempio n. 9
0
def _update_dataset_record(dataset_id, fields):
    fields["id"] = dataset_id
    _configuration = None
    if "configuration" in fields:
        _configuration = fields["configuration"]
        fields["configuration"] = json.dumps(fields["configuration"])
    fields["mtime"] = datetime.datetime.utcnow()
    query = querybuilder.update("dataset", fields)

    with db, db.cursor() as cur:
        cur.execute(query, fields)

    current_app.plugins.call_hook("dataset_update", dataset_id, _configuration)
Esempio n. 10
0
def put_resource_metadata(resource_id):
    new_metadata = _get_json_from_request()

    with db.cursor() as cur:
        query = querybuilder.select_pk('resource', fields='id, metadata')
        cur.execute(query, dict(id=resource_id))
        resource = cur.fetchone()

    if resource is None:
        raise NotFound('This resource does not exist')

    with db, db.cursor() as cur:
        data = dict(id=resource_id, metadata=json.dumps(new_metadata))
        query = querybuilder.update('resource', data)
        cur.execute(query, data)

    return '', 200
Esempio n. 11
0
def put_resource_metadata(resource_id):
    new_metadata = _get_json_from_request()

    with db.cursor() as cur:
        query = querybuilder.select_pk("resource", fields="id, metadata")
        cur.execute(query, dict(id=resource_id))
        resource = cur.fetchone()

    if resource is None:
        raise NotFound("This resource does not exist")

    with db, db.cursor() as cur:
        data = dict(id=resource_id, metadata=json.dumps(new_metadata))
        query = querybuilder.update("resource", data)
        cur.execute(query, data)

    return "", 200
Esempio n. 12
0
def test_querybuilder_sanity_checks():
    with pytest.raises(ValueError):
        querybuilder.insert('invalid table name', {'foo': 'bar'})

    with pytest.raises(ValueError):
        querybuilder.insert('mytable', {'invalid field name': 'bar'})

    with pytest.raises(ValueError):
        querybuilder.insert('mytable', {'foo': 'bar'}, table_key='invalid key')

    # -------------------- Update --------------------

    with pytest.raises(ValueError):
        querybuilder.update('invalid table name', {'foo': 'bar'})

    with pytest.raises(ValueError):
        querybuilder.update('mytable', {'invalid field name': 'bar'})

    with pytest.raises(ValueError):
        querybuilder.update('mytable', {'foo': 'bar'}, table_key='invalid key')