Ejemplo n.º 1
0
def _get_dataset_record(dataset_id):
    with db.cursor() as cur:
        query = querybuilder.select_pk('dataset')
        cur.execute(query, dict(id=dataset_id))
        dataset = cur.fetchone()
    if dataset is None:
        raise NotFound()
    return dataset
Ejemplo n.º 2
0
def _get_dataset_record(dataset_id):
    with db.cursor() as cur:
        query = querybuilder.select_pk("dataset")
        cur.execute(query, dict(id=dataset_id))
        dataset = cur.fetchone()
    if dataset is None:
        raise NotFound()
    return dataset
Ejemplo n.º 3
0
    def _dsres_get(self, name, obj_id):
        query = querybuilder.select_pk(name)
        with self.db, self.db.cursor() as cur:
            cur.execute(query, dict(id=obj_id))
            row = cur.fetchone()

        if row is None:
            raise NotFound()

        return self._row_to_obj(row)
Ejemplo n.º 4
0
    def _dsres_get(self, name, obj_id):
        query = querybuilder.select_pk(name)
        with self.db, self.db.cursor() as cur:
            cur.execute(query, dict(id=obj_id))
            row = cur.fetchone()

        if row is None:
            raise NotFound()

        return self._row_to_obj(row)
Ejemplo n.º 5
0
def get_resource_metadata(resource_id):
    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()

    return resource['metadata']
Ejemplo n.º 6
0
def get_resource_metadata(resource_id):
    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()

    return resource["metadata"]
Ejemplo n.º 7
0
def test_querybuilder_select_pk():
    query = querybuilder.select_pk('mytable')
    assert query == 'SELECT * FROM "mytable" WHERE "id"=%(id)s'

    query = querybuilder.select_pk('mytable', table_key='my_id')
    assert query == 'SELECT * FROM "mytable" WHERE "my_id"=%(my_id)s'

    query = querybuilder.select_pk('mytable', fields='one, two')
    assert query == 'SELECT one, two FROM "mytable" WHERE "id"=%(id)s'

    query = querybuilder.select_pk('mytable', fields=['one', 'two'])
    assert query == 'SELECT one, two FROM "mytable" WHERE "id"=%(id)s'

    with pytest.raises(ValueError):
        querybuilder.select_pk('Invalid table name')

    with pytest.raises(ValueError):
        querybuilder.select_pk('mytable', table_key='Invalid field name')
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
0
 def resource_data_get_info(self, objid):
     query = querybuilder.select_pk('resource_data')
     with self.db, self.db.cursor() as cur:
         cur.execute(query, {'id': objid})
         return cur.fetchone()
Ejemplo n.º 11
0
 def resource_data_get_info(self, objid):
     query = querybuilder.select_pk('resource_data')
     with self.db, self.db.cursor() as cur:
         cur.execute(query, {'id': objid})
         return cur.fetchone()
Ejemplo n.º 12
0
def serve_resource(resource_id, transfer_block_size=4096):
    """
    Serve resource data via HTTP, setting ETag and Last-Modified headers
    and honoring ``If-None-Match`` and ``If-modified-since`` headers.

    Currently supported features:

    - Set ``ETag`` header (to the hash of resource body)
    - Set ``Last-Modified`` header (to the last modification date)
    - Honor the ``If-modified-since`` header (if the resource was not
      modified, return 304)

    Planned features:

    - Return response as a stream, to avoid loading everything in memory.
    - Honor the ``If-Match`` / ``If-None-Match`` headers
    - Support ``Range`` requests + 206 partial response
    - Set ``Cache-control`` and ``Expire`` headers (?)
    - Properly support HEAD requests.

    :param resource_id:
        Id of the resource to be served

    :param transfer_block_size:
        Size of the streaming response size. Defaults to 4096 bytes.

    :return:
        A valid return value for a Flask view.
    """

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

    if resource is None:
        raise NotFound()

    mimetype = resource["mimetype"] or "application/octet-stream"
    headers = {
        "Content-type": mimetype,
        "Last-modified": resource["mtime"].strftime(HTTP_DATE_FORMAT),
        "ETag": resource["hash"],
    }

    # ------------------------------------------------------------
    # Check the if-modified-since header

    if "if-modified-since" in request.headers:
        try:
            if_modified_since_date = datetime.strptime(request.headers["if-modified-since"], HTTP_DATE_FORMAT)
        except:
            raise BadRequest("Invalid If-Modified-Since header value")

        if if_modified_since_date >= resource["mtime"]:
            # The resource was not modified -> return ``304 NOT MODIFIED``
            return Response("", status=304, headers=headers)

    # ------------------------------------------------------------
    # Stream the response data

    with db:
        lobject = db.lobject(oid=resource["data_oid"], mode="rb")
        data = lobject.read()
        lobject.close()

    return Response(data, status=200, headers=headers)
Ejemplo n.º 13
0
def serve_resource(resource_id, transfer_block_size=4096):
    """
    Serve resource data via HTTP, setting ETag and Last-Modified headers
    and honoring ``If-None-Match`` and ``If-modified-since`` headers.

    Currently supported features:

    - Set ``ETag`` header (to the hash of resource body)
    - Set ``Last-Modified`` header (to the last modification date)
    - Honor the ``If-modified-since`` header (if the resource was not
      modified, return 304)

    Planned features:

    - Return response as a stream, to avoid loading everything in memory.
    - Honor the ``If-Match`` / ``If-None-Match`` headers
    - Support ``Range`` requests + 206 partial response
    - Set ``Cache-control`` and ``Expire`` headers (?)
    - Properly support HEAD requests.

    :param resource_id:
        Id of the resource to be served

    :param transfer_block_size:
        Size of the streaming response size. Defaults to 4096 bytes.

    :return:
        A valid return value for a Flask view.
    """

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

    if resource is None:
        raise NotFound()

    mimetype = resource['mimetype'] or 'application/octet-stream'
    headers = {
        'Content-type': mimetype,
        'Last-modified': resource['mtime'].strftime(HTTP_DATE_FORMAT),
        'ETag': resource['hash'],
    }

    # ------------------------------------------------------------
    # Check the if-modified-since header

    if 'if-modified-since' in request.headers:
        try:
            if_modified_since_date = datetime.strptime(
                request.headers['if-modified-since'], HTTP_DATE_FORMAT)
        except:
            raise BadRequest("Invalid If-Modified-Since header value")

        if if_modified_since_date >= resource['mtime']:
            # The resource was not modified -> return ``304 NOT MODIFIED``
            return Response('', status=304, headers=headers)

    # ------------------------------------------------------------
    # Stream the response data

    with db:
        lobject = db.lobject(oid=resource['data_oid'], mode='rb')
        data = lobject.read()
        lobject.close()

    return Response(data, status=200, headers=headers)