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

z3c.preference的Python项目详细描述


https://travis-ci.com/zopefoundation/z3c.preference.svg?branch=masterhttps://coveralls.io/repos/github/zopefoundation/z3c.preference/badge.svgCurrent version on PyPISupported Python versions

这个包提供了一个用户界面,可以使用 z3c.pageletz3c.form

Changes

1.0 (2018-12-21)

  • 将测试更新为zope.testbrowser>=5
  • 添加对Python3.6和3.7的支持。

0.5 (2013-03-09)

  • idCategoryEditForm中的首选项进行排序以稳定化 排序顺序。

0.4 (2012-04-20)

  • 偏好组的描述现在呈现在group-header 组错误消息上方窗体的槽。
  • 修正了版本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)

  • 初次发布。

Overview

z3c.preference在浏览器中呈现首选项的表单 使用zope.preference定义的集合。

Using z3c.preference

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

  • 已为++preferences++命名空间注册视图 层z3c.preference.interfaces.IPreferenceLayer。所以你 必须将此界面添加到应用程序的浏览器层。
  • 只有具有权限的用户z3c.preference.EditPreference 允许访问首选项视图。所以你得加上这个 对用户的权限。应该能够访问的角色 首选项视图。

Editing preferences

Set up for tests

首先,我们必须定义一个首选项接口:

>>> 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')

Editing preferences using browser

有一个命名空间可访问首选项。页面上的表单是 显示默认值:

>>> 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

Editing preference group trees

zope.preferencepreference group trees的概念 preferencecategories对首选项进行分组。

如果使用z3c.preference自动显示preferencecategory 属于该类别的所有preferencegroups在 编辑表单(又名GroupForm)。

注意:当前只有首选项类别及其direct子项 在树中呈现。

Set up

首先,我们必须定义一些表示树的首选项接口。在 这个例子我们认为一个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...

Editing preference group trees using browser

有一个命名空间可访问首选项。页面上的表单是 显示默认值:

>>> 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

To do

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
用于批量操作的java RESTful API分块响应   java读取在线存储的文本文件   在Java ME中将双精度舍入到小数点后5位   java查找一个数字的最接近因子   java更改JMenuBar的字体   java Kmeans聚类算法运行时间和复杂性   java是否可以阻止try catch返回null   java内容解析器指向具有正确URI的错误表   java Android Kotlin插装测试未被识别为插装测试   java TestNG@Dataprovider   在forloop和print语句中声明变量时发生java错误   java在Android Studio 3中设置JNI