跳转到主要内容

使用z3c.pagelet和z3c.form的zope.preference界面。

项目描述

https://github.com/zopefoundation/z3c.preference/actions/workflows/tests.yml/badge.svg https://coveralls.io/repos/github/zopefoundation/z3c.preference/badge.svg Current version on PyPI Supported Python versions

此包提供了使用z3c.pageletz3c.formzope.preference提供的用户界面。

更改

2.0 (2023-02-21)

  • 停止支持Python 2.7、3.5、3.6。

  • 添加对Python 3.8、3.9、3.10、3.11的支持。

1.0 (2018-12-21)

  • 更新测试到zope.testbrowser >= 5

  • 添加对Python 3.6和3.7的支持。

0.5 (2013-03-09)

  • 按其id对CategoryEditForm中的首选项进行排序,以稳定排序顺序。

0.4 (2012-04-20)

  • 现在在表单上方的错误消息之前渲染首选项分组的描述。

  • 修复了版本0.3的描述,它实际上为首选项类别添加了描述,而不是首选项分组。

0.3 (2012-03-15)

  • 偏好类别描述现在显示在表单的 extra-info 插槽中。

0.2 (2012-02-23)

0.1.1 (2010-07-17)

0.1.0 (2010-07-10)

  • 首次发布。

概述

z3c.preference 在浏览器中渲染使用 zope.preference 定义的偏好集表单。

使用z3c.preference

使用 z3c.preference 有一些先决条件

  • ++preferences++ 命名空间的视图注册了 z3c.preference.interfaces.IPreferenceLayer 层。因此,您必须将此接口添加到应用程序的浏览器层。

  • 只有具有 z3c.preference.EditPreference 权限的用户才能访问偏好视图。因此,您必须将此权限添加到应能够访问偏好视图的用户或角色。

编辑首选项

设置测试

首先,我们必须定义一个偏好接口

>>> import zope.interface
>>> import zope.schema
>>> class IBackEndSettings(zope.interface.Interface):
...     """Backend User Preferences"""
...
...     email = zope.schema.TextLine(
...         title=u"E-mail Address",
...         description=u"E-mail address used to send notifications")
...
...     skin = zope.schema.Choice(
...         title=u"Skin",
...         description=u"The skin that should be used for the back end.",
...         values=['Hipp', 'Lame', 'Basic'],
...         default='Basic')
...
...     showLogo = zope.schema.Bool(
...         title=u"Show Logo",
...         description=u"Specifies whether the logo should be displayed.",
...         default=True)

该接口必须注册为偏好

>>> from zope.configuration import xmlconfig
>>> import zope.preference
>>> context = xmlconfig.file('meta.zcml', zope.preference)
>>> context = xmlconfig.string('''
...     <configure
...         xmlns="http://namespaces.zope.org/zope"
...         i18n_domain="test">
...
...       <preferenceGroup
...           id="BackEndSettings"
...           title="Back End Settings"
...           schema="z3c.preference.README.IBackEndSettings"
...           />
...
...     </configure>''', context)

要访问表单,需要一个浏览器,用户必须被授权,因为偏好存储在主体注解中

>>> from zope.testbrowser.wsgi import Browser
>>> browser = Browser()
>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')

使用浏览器编辑首选项

存在一个命名空间来访问偏好。在页面上显示一个表单,显示默认值

>>> browser.open('http://localhost/++preferences++/BackEndSettings')
>>> browser.getControl('E-mail Address').value
''
>>> browser.getControl('Skin').displayValue
['Basic']
>>> browser.getControl('yes').selected
True
>>> browser.getControl('no').selected
False

可以更改这些值,提交表单使它们持久化

>>> browser.getControl('E-mail Address').value = 'me@example.com'
>>> browser.getControl('Skin').displayValue = ['Hipp']
>>> browser.getControl('no').click()
>>> browser.getControl('Apply').click()

提交表单后,再次显示并显示更改后的值

>>> 'Data successfully updated.' in browser.contents
True
>>> browser.getControl('E-mail Address').value
'me@example.com'
>>> browser.getControl('Skin').displayValue
['Hipp']
>>> browser.getControl('no').selected
True

编辑首选项分组树

zope.preference偏好分组树偏好类别 的概念来分组偏好。

如果使用 z3c.preference 显示 偏好类别,则自动将属于该类别的所有 偏好分组 作为组渲染到编辑表单中(即 GroupForm)。

注意:当前只渲染树中的偏好类别及其 直接 子类别。

设置

首先,我们必须定义一些表示树的偏好接口。在这个例子中,我们考虑一个具有一些区域的 Web 应用程序

>>> import zope.interface
>>> import zope.schema
>>> class IGeneralSettings(zope.interface.Interface):
...     """General preferences"""
...
...     language = zope.schema.Choice(
...         title=u"Language",
...         description=u"The language which should be used for display.",
...         values=['German', 'English', 'Russian'],
...         default='German')
>>> class IRSSSettings(zope.interface.Interface):
...     """Preferences for the RSS area of the application."""
...
...     number = zope.schema.Int(
...         title=u"Item count",
...         description=u"Maximum number of items in each feed.")
>>> class ISearchSettings(zope.interface.Interface):
...     """Preferences for the search area of the application."""
...
...     store_searches = zope.schema.Bool(
...         title=u"Store searches?",
...         description=u"Should searches be kept for later use?",
...         default=True)

这些接口必须注册为偏好

>>> from zope.configuration import xmlconfig
>>> import zope.preference
>>> context = xmlconfig.file('meta.zcml', zope.preference)
>>> context = xmlconfig.string('''
...     <configure
...         xmlns="http://namespaces.zope.org/zope"
...         i18n_domain="test">
...
...       <preferenceGroup
...           id="app"
...           title="General Settings"
...           description="Settings for the whole app"
...           schema="z3c.preference.categories.IGeneralSettings"
...           category="true"
...           />
...
...       <preferenceGroup
...           id="app.search"
...           title="Search Settings"
...           schema="z3c.preference.categories.ISearchSettings"
...           category="false"
...           />
...
...       <preferenceGroup
...           id="app.rss"
...           title="RSS Settings"
...           description="Settings for the RSS feeds"
...           schema="z3c.preference.categories.IRSSSettings"
...           category="false"
...           />
...
...     </configure>''', context)

要访问表单,需要一个浏览器,用户必须被授权,因为偏好存储在主体注解中

>>> from zope.testbrowser.wsgi import Browser
>>> browser = Browser()
>>> browser.addHeader('Authorization', 'Basic mgr:mgrpw')

表单显示类别的标题和描述

>>> browser.open('http://localhost/++preferences++/app')
>>> print(browser.contents)
<!DOCTYPE ...
...General Settings...
...Settings for the whole app...
...RSS Settings...
...Settings for the RSS feeds...
...Search Settings...

使用浏览器编辑首选项分组树

存在一个命名空间来访问偏好。在页面上显示一个表单,显示默认值

>>> browser.open('http://localhost/++preferences++/app')
>>> browser.getControl('Language').displayValue
['German']
>>> browser.getControl('Item count').value
''
>>> browser.getControl('yes').selected
True
>>> browser.getControl('no').selected
False

可以更改这些值,提交表单使它们持久化

>>> browser.getControl('Language').displayValue = ['English']
>>> browser.getControl('Item count').value = '20'
>>> browser.getControl('no').click()
>>> browser.getControl('Apply').click()

提交表单后,再次显示并显示更改后的值

>>> 'Data successfully updated.' in browser.contents
True
>>> browser.getControl('Language').displayValue
['English']
>>> browser.getControl('Item count').value
'20'
>>> browser.getControl('no').selected
True

待办事项

  • 记录如何在自己的项目中使用它。(ZCML 和皮肤)

项目详细信息


下载文件

下载适合您平台的应用程序。如果您不确定要选择哪个,请了解有关 安装包 的更多信息。

源分布

z3c.preference-2.0.tar.gz (13.2 kB 查看哈希值

上传时间

构建分布

z3c.preference-2.0-py3-none-any.whl (14.8 kB 查看哈希值

上传时间 Python 3

由以下提供支持