为plone添加路由配置的能力
项目描述
简介
为Plone实现的路由。路由实现驱动是查询门户目录。
示例路由
/posts/{effective:year}/{effective:month}/{effective:day}
添加路由
from collective.routes import addRoute addRoute('BlogItems', '/posts/{effective:year}/{effective:month}/{effective:day}', defaultQuery={'portal_type': 'News Item'}, allowPartialMatch=True)
启用它
在路由可用之前,您需要首先在您想要使用它的站点上启用路由。这可以通过在“站点设置”中的路由配置面板来完成。
开箱即用的路由
collective.routes附带一些示例路由,预先打包。
博客文章
- 博客文章
/posts/{effective:year}/{effective:month}/{effective:day}
示例URL
/posts/2011 ~ Show all posts from 2011 /posts/2011/10 ~ Show all from 2011 and the month of October /posts/2011/10/5 ~ Show the blog posted October 5, 2011
定义
addRoute('Blog Posts', '/posts/{effective:year}/{effective:month}/{effective:day}', defaultQuery={'portal_type': 'News Item', 'sort_on': 'effective', 'sort_order': 'reverse'}, allowPartialMatch=True)
标记内容
- 标记
/tagged/{Subject}/{Subject}/{Subject}
示例URL
/tagged/foo ~ Show all posts tagged `foo` /tagged/foo/bar ~ Show all posts tagged `foo` and `bar` /tagged/foo/bar/woo ~ Show all posts tagged `foo`, `bar` and `woo`
定义
addRoute('Tagged', '/tagged/{Subject}/{Subject}/{Subject}', defaultQuery={'portal_type': 'News Item', 'sort_on': 'effective', 'sort_order': 'reverse'}, allowPartialMatch=True)
路由语法
语法非常简单,只有几种变体。
文字
文字字符串匹配
/string-to-match
将匹配“string-to-match”
查询
匹配任何内容,并将其作为查询参数保持
/{Subject}
将匹配任何字符串,并将其值作为查询参数保持,用于门户目录查询。
日期查询
有三个子指令来匹配部分内容
/{effective:year}/{effective:month}/{effective:day}
然后将这些内容组合成门户目录查询。
自定义对象检索
如果您希望绕过正常的门户目录查询以检索对象,您可以提供自己的对象查找方法。
示例
def customObjectFinder(context, **kwargs): query = context.query site = getSite() return site[query['id']] addRoute('My Route', '/my-route/{id}', objectFinder=customObjectFinder)
调整已发布对象
如果您想在遍历发布之前最后一刻添加接口,这就是您会使用的。
这对于添加接口很有用,因为实际发布的对象被包裹,以保持发布时的面包屑。
示例
from interfaces import IMySpecialContext from zope.interface import alsoProvides def myMungeMethod(context): alsoProvides(context, IMySpecialContext) addRoute('My Route', '/foo/{bar}', mungeObject=myMungeMethod)
自定义视图渲染
您还可以自定义渲染的查找对象的视图
addRoute('My Route', '/foo/{bar}', customViewName='@@custom-view')
addRoute 签名
允许参数
- routeName(必需)
路由名称
- route(必需)
实际的路由规范
- defaultQuery(默认为 {})
为查找器提供的默认查询
- objectFinder(默认为 collective.routes.finders.catalogObjectFinder)
用于查找已发布对象的方法
- mungeObject(默认为 None)
由于实际发布的对象是一个包装器对象,这是一个在发布之前能够与临时包装器对象打交道的方
- customViewName(默认为 None)
为查找对象渲染的定制视图
- allowPartialMatch(默认为 False)
如果整个 URL 不匹配,您仍然可以尝试发布它。这在目录查找器路由中非常有用,您希望允许用户提供部分 URL 并仍然找到对象。
- breadcrumbFactory(默认为 None)
覆盖面包屑生成。必须返回一个包含 {'absolute_url': url, 'Title': title} 值的元组。
- customPredicates(默认为 [])
一个自定义谓词函数的可迭代对象,用于检查与传入请求是否匹配。谓词必须接受两个参数(request,query),其中 request 是当前请求对象,query 是从路由生成的当前查询。函数必须返回一个布尔值。如果匹配则为 True,如果不匹配则为 False。
变更日志
1.1a2 (2014-08-29)
添加了意大利翻译 [giacomos]
添加了一些 i18n 字符串和 2 个用于 i18n 重建的脚本 [giacomos]
plone 4.3 兼容导入 [Lewicki]
1.1a1 (2012-02-28)
添加了西班牙语和巴西葡萄牙语翻译 [hvelarde]
转移到使用 plone.app.registry [saibatizoku]
使其与 plone.locking 兼容 [vangheem]
在路由配置中提供指定要遍历的视图的能力 [vangheem]
不应用 IWrappedContext 接口 [vangheem]
在路由请求时动态地将 IRoutedRequest 层添加到请求中,以便您可以在路由时覆盖 plone 的特定部分。 [vangheem]
添加提供自定义谓词的能力 [vangheem]
添加提供自定义面包屑工厂的能力 [vangheem]
添加了 allowPartialMatch 的 addRoute 参数,这样在匹配 URL 与路由时,您就不必匹配整个 URL。 [vangheem]
1.0a1 (2011-10-07)
初始发布
collective.routes-1.1a2.zip 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | fe2571c0771b7305d73f8b7725dbfd781baa5274c8ae9947914e029efca8845f |
|
MD5 | 70ca807bbf7469dee30ccf6e4ab02593 |
|
BLAKE2b-256 | 9088ebb71258ea48582be6b0f2bdc8ef6191e6df4da590dbbb7343fbda2a7b3e |