金字塔的extdirect实现
pyramid_extdirect的Python项目详细描述
金字塔ExtDirect自述文件
简介:
这个pyramid插件为ExtDirect Senchaapi提供了一个路由器 包含在ExtJS中。
extdirect允许直接通过javascript运行服务器端回调,而不需要 额外的ajax模板。典型的extdirect使用场景如下:
MyApp.SomeClass.fooMethod(foo, bar, function(response) { // do cool things with response });
或者更好,如果在gridstore中使用extdirect:
var usersStore = new Ext.data.Store({ fields: ['id', 'name', 'title'], proxy: { type: 'direct', directFn: MyApp.Users.loadAll, reader: { type: 'json', rootProperty: 'items' } } // ... });
这里MyApp是应用程序命名空间,SomeClass或 Grids是类或操作和fooMethod和 loadGridData是方法。
用法示例:
金字塔ExtDirect的最低要求是创建ExtDirect API和路由器:
from pyramid.config import Configurator from exampleapp.resources import Root def main(global_config, **settings): """ This function returns a Pyramid WSGI application. """ config = Configurator(root_factory=Root, settings=settings) config.add_view('exampleapp.views.my_view', context='exampleapp:resources.Root', renderer='exampleapp:templates/mytemplate.pt') config.add_static_view('static', 'exampleapp:static') # let pyramid_extdirect create all the needed views automatically config.include('pyramid_extdirect') # scan your code once to make sure the @extdirect_method decorators # are picked up config.scan() return config.make_wsgi_app()
在此之后,您可以使用@extdirect_方法装饰任意函数或类方法:
@extdirect_method(action='SomeAction') def do_stuff(a, b, c): return a + b + c
或者,如果您想将方法分组到类(动作)中,可以通过修饰来实现 类方法:
UsersController类可以组合用户crud操作的所有方法,只有 要求这个类接受request作为它的第一个也是唯一的构造函数参数, 这是确保您的方法在任何时候都可以访问request所必需的:
from pyramid_extdirect import extdirect_method class UsersController(object): __extdirect_settings__ = { 'default_action_name': 'Users', 'default_permission': 'view' } def __init__(self, request): self.request = request # we don't need to set ``action`` here, because # it's already defined via __extdirect_settings__ @extdirect_method(permission='view', method_name='loadAll') def load_all(self, params): # params is a simple dict that will contain the # paging and sorting options as well as any other # extra parameters (defined using proxy.extraParams # your store config) users = [] for user in users_db.fetch_all(): users.append({ id: obj.id, name: obj.name, title: obj.title, # ... }) return dict(success=True, items=users)
如您所见,Users#loadAll方法甚至不知道它是通过 一个http请求,它只是一个普通的老python方法,返回一个dict。 @extdirect_method(permission='view')装饰将其添加到 Users操作(同时确保只允许具有view权限的用户 运行它)。我们在这里返回一个dict,仅仅是因为ajax响应发送到 客户端必须是可序列化的json。默认情况下,python json封送拆收器只能 对内置python原语进行编码/解码。pyramid_extdirect有一个小助手 不过,这会检查对象是否有一个名为json_repr()的方法(它应该 返回一个json可序列化的dict/list/string/number/etc),如果找到,这个方法是 用于将实例解码为其jsunable版本。 可以在类中定义__extdirect_settings__属性来定义默认值 action和permission,因此在上面的示例中,我们也可以只使用@extdirect_method()。
有时需要使用extdirect的上传功能。因为上传不能 使用ajax完成(通过json编码的请求体)ext有点小技巧 通过创建一个隐藏的iframe并将此iframe中的表单发布到服务器。 但是,extdirect需要事先知道,您的代码可能会收到上传。 在pyramid_extdirectdecorators中,这是通过添加accepts_files@extdirect_methoddecorator的参数:
class Users(object): ... @extdirect_method(accepts_files=True) def upload_avatar(self, uploaded_file): # uploaded_file is now a FieldStorage instance
在某些情况下,访问request对象是绝对必要的 在你的函数中,你不想创建一个额外的类 传递给类构造函数)–这可以通过将request_as_last_param传递给 装饰工:
from pyramid.security import authenticated_userid @extdirect_method(action='App', request_as_last_param=True): def get_current_user(request): return authenticated_userid(request)
- igor stroh,<;igor.stroh-at-rulim.de>;
金字塔ExtDirect变更日志
0.6.0
- 添加了元数据支持
0.5.2
- 添加了错误日志记录并修复了“json_编码器”时的问题 includeme(..)配置中未提供(感谢Fabian Maier)
0.5.1
- 向extdirect构造函数添加了可选的json_编码器参数 允许提供自定义JSON序列化程序(例如 pyramid.renderers.json)
0.5.0
- 我们现在符合python3(感谢fabien coronis)
0.4.0
- 修复了用户定义的异常视图处理
0.3.2
- 为extdirect_method修饰函数添加了权限检查(与methods相比)
- 为ExtDirect API路由添加了操作筛选器
0.3.1
添加了金字塔调试工具栏支持(感谢github.com/breath)
0.3
将项目重命名为“金字塔”extdirect
0.2
- 固定格式引用
- 将包结构从repoze.bfg移动到棱锥体
0.1.2
- 修正了jsonreprencoder中的输入错误
0.1.1
- 固定包装
- 修正了请求为最后参数的方法的numargs
0.1
- 初次发布。