向简单的sqlalchemy对象添加rest接口
c2c.sqlalchemy.rest的Python项目详细描述
使用它
在<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')),]
在<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)
在<project>/__init__.py:
frompyramid.renderersimportJSONPfromc2c.sqlalchemy.restimportadd_rest_routesconfig.add_renderer('jsonp',JSONP(param_name='callback'))add_rest_routes(config,'obj','/object')
控制要显示的属性
可以通过传递 attr_list构造函数的参数。默认情况下,所有属性都是 显示。例如:
obj=REST(DBSession,Object,attr_list=['id','name'])
其他属性
通过在模型中定义 __additional_properties__函数有一个attr_list参数。 例如:
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})returnproperties
使用关系
可以使用read_many和^{tt8}检索相关对象$ 如果在模型中定义了关系并且 它们被传递给rest构造函数。例如:
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)
可以指定包含相关对象的属性的名称 使用propname参数(默认为关系名称):
tag_children={'childrenTags':{'rest':REST(DBSession,Tag),'propname':'tags'}}tag=REST(DBSession,Tag,children=tag_children)
示例结果:
{"active":false,"tags":[{"active":true,"name":"Artenschutz","id":31},{"active":false,"name":"Pioniervegetation","id":71}],"name":"Naturschutz","id":58}
来源
生成:
python bootstrap.py --distribute -v 1.7.1 ./buildout/bin/buildout
协议
- 多读,GETon.../obj:
{"objects":[{"id":id,"property":"value",...},...]}
- 读一篇,GET关于.../obj/{id}:
{"id":id,"property":"value",...}
- 计数,GET在.../obj/count:
23
- 创建,POST在.../obj上,数据:
{"property":"value",...}
它将返回ID。
- 更新,PUTon.../obj/{id}with data:
{"property":"value",...}
- 自动,POST打开.../obj/auto,数据:
{"id":id,"property":"value",...}
如果一个对象与给定的id匹配,它将被更新,否则一个新的对象是 用给定的id值自动创建。
- 删除,DELETE于.../obj/{id}。