Package: c2c.sqlalchemy.rest
Use itIn<project>/model.py:frompyramid.securityimportAllow,Authenticated,ALL_PERMISSIONSclassObject(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:frompyramid.viewimportview_configfromc2c.sqlalchemy.restimportRESTfrom<project>.modelsimportDBSession,Objectobj=REST(DBSession,Object)@view_config(route_name='obj_read_many',renderer='jsonp')defobj_read_many(request):returnobj.read_many(request)@view_config(route_name='obj_read_one',renderer='jsonp')defobj_read_one(request):returnobj.read_one(request)@view_config(route_name='obj_count',renderer='string')defobj_count(request):returnobj.count(request)@view_config(route_name='obj_create',renderer='jsonp')defobj_create(request):returnobj.create(request)@view_config(route_name='obj_update')defobj_update(request):returnobj.update(request)@view_config(route_name='obj_auto')defobj_auto(request):returnobj.auto(request)@view_config(route_name='obj_delete',renderer='jsonp')defobj_delete(request):returnobj.delete(request)In<project>/__init__.py:frompyramid.renderersimportJSONPfromc2c.sqlalchemy.restimportadd_rest_routesconfig.add_renderer('jsonp',JSONP(param_name='callback'))add_rest_routes(config,'obj','/object')Controlling what attributes to displayOne may restrict the displayed attributes to a subset by passing anattr_listargument to the constructor. By default all attributes are
displayed. For instance:obj=REST(DBSession,Object,attr_list=['id','name'])Additional propertiesIt is possible to add some extra properties by defining in the model an__additional_properties__function with anattr_listargument.
For instance:classObject(Base):def__additional_properties__(self,attr_list=None):properties={}ifattr_listisNoneor'l10n'inattr_list:l10n={}forlinself.l10n:l10n[l.lang.code]=l.valueproperties.update({"l10n":l10n})returnpropertiesUsing RelationshipsIt is possible to retrieve related objects withread_manyandread_oneactions provided that the relationships are defined in the models and that
they are passed to the REST constructor. For instance:classTag(GeoInterface,Base):__tablename__='tag'__table_args__=(UniqueConstraint('name'),{"schema":'tagging'})__acl__=[(Allow,'admin',ALL_PERMISSIONS),(Allow,'editor',('view','edit','new','delete')),(Allow,Everyone,('view')),]id=Column(types.Integer,primary_key=True)name=Column(types.Unicode(200),nullable=False)active=Column(types.Boolean,default=True)l10n=relationship("TagL10n",backref="tag")childrenTags=relationship("Tag",secondary=tag_tag,primaryjoin=id==tag_tag.c.tag_id1,secondaryjoin=id==tag_tag.c.tag_id2,order_by=name,backref="parentTags")tag_children={'childrenTags':{'rest':REST(DBSession,Tag)}}tag=REST(DBSession,Tag,children=tag_children)The name of the property containing the related objects may be specified
using thepropnameparameter (default is the relationship name):tag_children={'childrenTags':{'rest':REST(DBSession,Tag),'propname':'tags'}}tag=REST(DBSession,Tag,children=tag_children)Example result:{"active":false,"tags":[{"active":true,"name":"Artenschutz","id":31},{"active":false,"name":"Pioniervegetation","id":71}],"name":"Naturschutz","id":58}From sourceBuild:python bootstrap.py --distribute -v 1.7.1
./buildout/bin/buildoutProtocolRead many,GETon.../obj:{"objects":[{"id":id,"property":"value",...},...]}Read one,GETon.../obj/{id}:{"id":id,"property":"value",...}Count,GETon.../obj/count:23Create,POSTon.../objwith data:{"property":"value",...}and it will return the id.Update,PUTon.../obj/{id}with data:{"property":"value",...}Auto,POSTon.../obj/autowith data:{"id":id,"property":"value",...}If an object matches the given id, it will be updated, else a new object is
automatically created with the given id value.Delete,DELETEon.../obj/{id}.