Skip to main content

Add a REST interface to simple SQLAlchemy object

Project description

Use it

In <project>/model.py:

from pyramid.security import Allow, Authenticated, ALL_PERMISSIONS
class Object(Base):
    __tablename__ = 'object'
    __table_args__ = {'autoload':True}
    __acl__ = [
        (Allow, 'admin', ALL_PERMISSIONS),
        (Allow, 'editor', ('view', 'edit', 'new', 'delete')),
        (Allow, Authenticated, ('view')),
    ]

In <project>/views/rest.py:

from pyramid.view import view_config
from c2c.sqlalchemy.rest import REST
from <project>.models import DBSession, Object
obj = REST(DBSession, Object)

@view_config(route_name='obj_read_many', renderer='jsonp')
def obj_read_many(request):
    return obj.read_many(request)

@view_config(route_name='obj_read_one', renderer='jsonp')
def obj_read_one(request):
    return obj.read_one(request)

@view_config(route_name='obj_count', renderer='string')
def obj_count(request):
    return obj.count(request)

@view_config(route_name='obj_create', renderer='jsonp')
def obj_create(request):
    return obj.create(request)

@view_config(route_name='obj_update')
def obj_update(request):
    return obj.update(request)

@view_config(route_name='obj_delete', renderer='jsonp')
def obj_delete(request):
    return obj.delete(request)

In <project>/__init__.py:

from pyramid.renderers import JSONP
from c2c.sqlalchemy.rest import add_rest_routes
config.add_renderer('jsonp', JSONP(param_name='callback'))
add_rest_routes(config, 'obj', '/object')

From source

Build:

python bootstrap.py --distribute -v 1.7.1
./buildout/bin/buildout

Protocol

Read many, GET on .../obj:

{
    "objects": [{
        "id": id,
        "property": "value",
        ...
    },
    ...
    ]
}

Read one, GET on .../obj/{id}:

{
    "id": id,
    "property": "value",
    ...
}

Count, GET on .../obj/count:

23

Create, POST on .../obj with data:

{
    "property": "value",
    ...
}

and it will return the id.

Update, PUT on .../obj/{id} with data:

{
    "property": "value",
    ...
}

Delete, DELETE on .../obj/{id}.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

c2c.sqlalchemy.rest-0.2.2.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file c2c.sqlalchemy.rest-0.2.2.tar.gz.

File metadata

File hashes

Hashes for c2c.sqlalchemy.rest-0.2.2.tar.gz
Algorithm Hash digest
SHA256 305ddb0887d2cdafd2a11d5bc3089abee30509c8db08836918295021e8eee328
MD5 530cf2cbb7cba03143382ba802023ae9
BLAKE2b-256 07560c6ecef4d9c5ccada1da6372a9c8ffd5f642323e9941a4e92d486fdea225

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page