不忘记的zope词汇类型,就像大象一样
项目描述
简介
就像大象不会忘记任何事情一样,collective.elephantvocabulary 也不会忘记。它为现有的zope.schema词汇提供了一个包装器,使它们不会忘记任何事情。
例如,一个用例是一个用户的词汇(源),从某个时间点开始,想要隐藏/停用一些用户用于表单或列表。但与此同时,你希望保持对用户术语的旧引用仍然有效。这就是collective.elephantvocabulary发挥作用的地方。使用它,你可以包装现有的用户词汇并提供一组隐藏的用户列表(术语值)。
用法
一些示例内容和词汇表
>>> context = layer.context >>> example_vocab = layer.example_vocab >>> example_source = layer.example_source >>> [i.value for i in example_vocab] [1, 2, 3, 4]
以下是我们的包装方法,我们用它来使我们的现有词汇更像大象。
>>> from collective.elephantvocabulary import wrap_vocabulary
在第一个例子中,我们向我们的wrap_vocabulary传递一个[1, 2, 3, 4]的词汇表,并将术语2和3设置为隐藏。 wrap_vocabulary返回一个需要调用上下文(你也可以将其注册为实用工具)的VocabularyFactory。
>>> wrapped_vocab_factory = wrap_vocabulary(example_vocab, hidden_terms=[2, 3]) >>> print wrapped_vocab_factory <collective.elephantvocabulary.vocabulary.VocabularyFactory object at ...>>>> wrapped_vocab = wrapped_vocab_factory(context) >>> [i.value for i in wrapped_vocab] [1, 4]>>> len(wrapped_vocab) == len(example_vocab) True>>> 2 in wrapped_vocab True>>> 5 in wrapped_vocab False>>> wrapped_vocab.getTerm(3).value 3
同样,我们可以限制仅显示我们想要的集合中的项目(通过visible_terms)
>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... visible_terms=[2, 3])(context) >>> [i.value for i in wrapped_vocab] [2, 3]>>> len(wrapped_vocab) == len(example_vocab) True>>> 2 in wrapped_vocab True>>> 5 in wrapped_vocab False>>> wrapped_vocab.getTerm(1).value 1
如上所示,collective.elephantvocabulary就是关于什么的。当列出词汇时,隐藏的术语不会列出。但当请求具有其术语值的项时,也会返回术语。此外,词汇的长度没有变化。它仍然显示词汇的原始长度。
我们也可以通过ZCA机制注册的名称来调用词汇。
>>> wrapped_vocab = wrap_vocabulary('example-vocab', ... hidden_terms=[2, 3])(context) >>> [i.value for i in wrapped_vocab] [1, 4]
hidden_terms或visible_terms参数(传递给wrap_vocabulary的第二个参数)也可以是可调用的,它期望两个参数,context和original vocabulary。
>>> def hidden_terms(context, vocab): ... return [1, 4]>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... hidden_terms=hidden_terms)(context) >>> [i.value for i in wrapped_vocab] [2, 3]>>> def visible_terms(context, vocab): ... return [1, 4]>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... visible_terms=hidden_terms)(context) >>> [i.value for i in wrapped_vocab] [1, 4]
collective.elephantvocabulary也适用于源。
>>> [i.value for i in example_source] [1, 2, 3, 4]>>> [i.value for i in example_source.search()] [1, 2]>>> wrapped_source = wrap_vocabulary(example_source, hidden_terms=[1, 4])(context) >>> [i.value for i in wrapped_source.search()] [2]>>> wrapped_source = wrap_vocabulary(example_source, visible_terms=[1, 4])(context) >>> [i.value for i in wrapped_source.search()] [1]
如果词汇已经提供了一组隐藏的术语,它们将被传递给包装后的词汇。
>>> example_vocab.hidden_terms = [1, 2] >>> wrapped_vocab = wrap_vocabulary(example_vocab)(context) >>> [i.value for i in wrapped_vocab] [3, 4]>>> del example_vocab.hidden_terms>>> example_vocab.visible_terms= [1, 2] >>> wrapped_vocab = wrap_vocabulary(example_vocab)(context) >>> [i.value for i in wrapped_vocab] [1, 2]>>> del example_vocab.visible_terms
词汇将添加到传递的visible_terms或hidden_terms列表中。
>>> example_vocab.hidden_terms = [1, 2] >>> wrapped_vocab = wrap_vocabulary(example_vocab, ... hidden_terms=[2, 3])(context) >>> [i.value for i in wrapped_vocab] [4]>>> del example_vocab.hidden_terms>>> example_vocab.visible_terms= [1] >>> wrapped_vocab = wrap_vocabulary(example_vocab, ... visible_terms=[1, 2, 3])(context) >>> [i.value for i in wrapped_vocab] [1, 2, 3]>>> del example_vocab.visible_terms
hidden_terms和visible_terms也可以一起工作。
>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... visible_terms=[1, 2, 3], ... hidden_terms=[2])(context) >>> [i.value for i in wrapped_vocab] [1, 3]
我们也可以在plone.registry中存储hidden_terms和visible_terms。而不是创建我们自己的从plone.registry读取的方法,collective.elephantvocabulary提供了辅助参数:hidden_terms_from_registry和visible_terms_from_registry。
>>> from zope.component import getUtility >>> from plone.registry import field >>> from plone.registry import Record >>> from plone.registry.interfaces import IRegistry>>> example_registry_record = Record( ... field.List(title=u"Test", min_length=0, max_length=10, ... value_type=field.Int(title=u"Value"))) >>> example_registry_record.value = [1, 2]>>> registry = getUtility(IRegistry) >>> registry.records['example.hidden_terms'] = example_registry_record >>> registry.records['example.visible_terms'] = example_registry_record>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... visible_terms_from_registry='example.visible_terms')(context) >>> [i.value for i in wrapped_vocab] [1, 2]>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... hidden_terms_from_registry='example.hidden_terms')(context) >>> [i.value for i in wrapped_vocab] [3, 4]
或者我们可以将它们组合使用。
>>> example_registry_record2 = Record( ... field.List(title=u"Test", min_length=0, max_length=10, ... value_type=field.Int(title=u"Value"))) >>> example_registry_record2.value = [1, 2, 3] >>> registry.records['example.visible_terms'] = example_registry_record2>>> wrapped_vocab = wrap_vocabulary(example_vocab, ... visible_terms_from_registry='example.visible_terms', ... hidden_terms_from_registry='example.hidden_terms')(context) >>> [i.value for i in wrapped_vocab] [3]
如果我们没有向wrap_vocabulary传递任何内容,则它应该被视为正常词汇。
>>> wrapped_vocab5 = wrap_vocabulary(example_vocab)(context) >>> [i.value for i in wrapped_vocab5] [1, 2, 3, 4]
致谢
由4teamwork慷慨赞助。
Rok Garbas,作者
待办事项
为自定义包装类提供测试/文档
覆盖率应该显示100%,但在方法和导入行上失败,很奇怪。
历史
0.2.5 (2015-03-21)
空白清理。[gforcada]
0.2.4 (2013-10-26)
修复0.2.3版本。[garbas]
0.2.3 (2013-10-25)
不要在初始化时加载持久性plone.registry实用工具。这避免了跨不同ZODB连接的对象访问问题(见问题#2)。[lgraf]
避免嵌套内部术语列表(见问题#3)。[lgraf]
避免内部术语列表无限增长(见问题#4)。[lgraf]
将README.rst拆分为多个文件。将测试部分放在主目录中的tests.rst中,以便在我们在PyPI上分发时也可以找到此测试文件。[maurits]
0.2.2 (2010-10-12)
支持其他类型的词汇(IVocabulary,IIterableSource)[garbas]
BUG(已修复):不应在__init__时间加载注册表。[garbas]
0.2.1 (2010-10-11)
新增参数 visible_terms_from_registry 和 hidden_terms_from_registry,它们直接从 plone.registry 读取值。[garbas]
0.2 (2010-10-11)
将 visible_terms 参数添加到 wrap_vocabulary 中,默认情况下 visible_terms 和 hidden_terms 通过 WrapperBase “一起”工作。[garbas]
0.1.3 (2010-10-11)
使用 IElephantVocabulary 接口标记包装词汇表。[garbas]
0.1.2 (2010-10-08)
拼写错误依赖项,感觉有点愚蠢。[garbas]
0.1.1 (2010-10-08)
添加从我们导入的依赖项(使用 mr.igor)[garbas]
添加到 zope.schema 的链接,它破坏了 rst 格式化。[garbas]
初始版本有损坏(缺少 README.rst)[garbas]
0.1 (2010-10-08)
初始版本 [garbas]
项目详情
下载文件
下载您平台上的文件。如果您不确定要选择哪个,请了解更多关于 安装包 的信息。
源代码发行版
collective.elephantvocabulary-0.2.5.zip 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | a3f4ef008767007845ac7f792fd468936c192adc33d0be945814b8a609a8c01b |
|
MD5 | 544106fc69dfdacf9ddbb55723d0c2d2 |
|
BLAKE2b-256 | 93f1a86cb423095229d11544199a5cd64151deffdacc85bda28b300ea3ee7602 |