在我过于复杂的简单内容的无尽追求中,我正在研究最"Pythonic"的方式,在Python egg包中的典型' config.py '中提供全局配置变量.
传统方式(aah,good ol'#define!)如下:
MYSQL_PORT = 3306
MYSQL_DATABASE = 'mydb'
MYSQL_DATABASE_TABLES = ['tb_users', 'tb_groups']
Run Code Online (Sandbox Code Playgroud)
因此,全局变量以下列方式之一导入:
from config import *
dbname = MYSQL_DATABASE
for table in MYSQL_DATABASE_TABLES:
print table
Run Code Online (Sandbox Code Playgroud)
要么:
import config
dbname = config.MYSQL_DATABASE
assert(isinstance(config.MYSQL_PORT, int))
Run Code Online (Sandbox Code Playgroud)
这是有道理的,但有时可能有点混乱,特别是当你试图记住某些变量的名称时.此外,提供了一个"配置"对象,用变量的属性,可能会更加灵活.所以,从bpython config.py文件中取得领先,我想出了:
class Struct(object):
def __init__(self, *args):
self.__header__ = str(args[0]) if args else None
def __repr__(self):
if self.__header__ is None:
return super(Struct, self).__repr__()
return self.__header__
def next(self):
""" Fake iteration functionality.
"""
raise StopIteration
def …Run Code Online (Sandbox Code Playgroud) 我创建了一个名为"Résumé"的自定义Archetypes内容类型,并希望强制执行一个限制,允许成员在文件夹中只添加此类型的一个项目.如果成员已存在于该文件夹中,则更好的方法是将成员重定向到他或她的项目的编辑页面.
我如何强制执行此限制并提供此额外功能?
我正在构建一个应用程序,使用collective.lead(trunk)查询外部关系数据库中的某些数据.用户可以在自定义Plone控制面板工具中修改数据库连接设置(我按照Aspeli的Professional Plone Development书中的示例).以这种方式查询数据库设置.
我的产品base.zcml为数据库设置了一个实用程序:
<include package="plone.app.registry" />
<include package="collective.lead" />
<i18n:registerTranslations directory="locales" />
<utility
provides="collective.lead.interfaces.IDatabase"
factory=".dbsettings.CalculatorDatabase"
name="test.calc.db"
/>
Run Code Online (Sandbox Code Playgroud)
dbsettings.py有:
from zope.component import getUtility
from plone.registry.interfaces import IRegistry
class CalculatorDatabase(Database):
@property
def _url(self):
registry = getUtility(IRegistry)
settings = registry.forInterface(IDatabaseSettings)
return URL(
drivername=settings.drivername,
username=settings.username,
password=settings.password,
host=settings.hostname,
port=settings.port,
database=settings.database,
)
Run Code Online (Sandbox Code Playgroud)
这会在运行时引发ComponentLookupError异常:
File "/home/zope/envs/test-web/src/test.calc/test/calc/dbsettings.py", line 38, in _url
registry = getUtility(IRegistry)
File "/home/zope/envs/test-web/eggs/zope.component-3.7.1-py2.6.egg/zope/component/_api.py", line 171, in getUtility
raise ComponentLookupError(interface, name)
zope.configuration.config.ConfigurationExecutionError: <class 'zope.component.interfaces.ComponentLookupError'>: (<InterfaceClass plone.registry.interfaces.IRegistry>, '')
in:
File "/home/zope/envs/test-web/src/test.calc/test/calc/configure.zcml", line 26.2-30.6
<utility
provides="collective.lead.interfaces.IDatabase"
factory=".dbsettings.CalculatorDatabase" …Run Code Online (Sandbox Code Playgroud) 我希望为我的基于Dexterity的自定义内容类型的属性('sector')启用一个名为Sectors的特殊索引.
在我的架构中,在types/mycontent.py中我有:
class IMyContent(form.Schema):
"""
My Content
"""
sectors = schema.Set(
title=_(u"Sectors"),
description=_(u"Select some sectors"),
value_type=schema.Choice(vocabulary=vocs.sectors),
required=True,
)
(...)
Run Code Online (Sandbox Code Playgroud)
然后我在indexers.py中以这种方式定义索引
from plone.indexer.decorator import indexer
from zr.content.types.mycontent import IMyContent
@indexer(IMyContent)
def Sectors(obj):
"""Indexer for Sectors attribute.
"""
d = getattr(obj, "sectors", u"")
return d if d else None
Run Code Online (Sandbox Code Playgroud)
最后在root包configure.zcml中:
<adapter name="Sectors" factory=".indexers.Sectors"/>
Run Code Online (Sandbox Code Playgroud)
但是,它似乎不起作用.即使重新安装产品后,我也看不到portal_catalog和catalog脑对象中的索引似乎也没有.
我究竟做错了什么?
每个Web应用程序 - 每个Web站点 - 都是一项服务.(...)使网站易于使用的网站功能也使网络服务API易于程序员使用.
Richardson和Ruby,"RESTFul Web Services"
根据我的意图,一个也是Web服务的Web站点提供其资源的多种表示,具体取决于用户代理请求的内容.可以这么说的API是网站本身,不是单独提供的.
对于许多流行的"REST API"而言,情况并非如此.例如,Twitter的API位于http://api.twitter.com/1/,URI中的"1"是API本身的版本.Socialcast还在https://demo.socialcast.com/api/上提供了REST API ,第三级名称是它所处理的网络的名称.
这对我来说似乎不对.如果我的博客位于http://www.example.com/blog,我不需要在其他位置提供API,仅为机器人提供JSON.而不是http://www.example.com/blog/posts/和http://api.example.com/blog/posts,两个不同的URI,我应该只有前者,并且可以使用多种表示,其中application/json对于我希望提供给我的用户的JSON API.
示例1:浏览器询问我博客上的帖子;
请求:
curl -i \
-H "Accept: text/html" \
-X GET \
http://www.example.org/blog/posts/
Run Code Online (Sandbox Code Playgroud)
响应:
200 OK
Content-Type: text/html; charset=utf-8
<html><body><h1>Posts</h1><ol><li><h2>My first post ...
Run Code Online (Sandbox Code Playgroud)
示例2:相同的URI,但这次机器人发出请求;
请求:
curl -i \
-H "Accept: application/json" \
-X GET \
http://www.example.org/blog/posts/
Run Code Online (Sandbox Code Playgroud)
响应:
200 OK
Content-Type: text/html; charset=utf-8
{
"posts": [
{
"id": 1,
"title": "My first …Run Code Online (Sandbox Code Playgroud) 我创建了一个自定义内容类型(简历),我希望提供一个自定义的"编辑"视图.我的用例非常简单,我只想在编辑表单之前显示一个HTML"免责声明"框.
首先我复制了:
Products/ATContentTypes/skins/ATContentTypes/atct_edit.cpt
Products/ATContentTypes/skins/ATContentTypes/atct_edit.cpt.metadata
Run Code Online (Sandbox Code Playgroud)
进入我的/ product/browser / as
my/product/browser/resumeedit.cpt
my/product/browser/resumeedit.cpt.metadata
Run Code Online (Sandbox Code Playgroud)
然后我定义了一个新的浏览器:my/product/browser/configure.zcml中的页面节:
<browser:page
for="..interfaces.IResume"
name="resume_edit"
class=".resumeview.ResumeEdit"
template="resumeedit.cpt"
permission="cmf.ModifyPortalContent"
/>
Run Code Online (Sandbox Code Playgroud)
我的/ product/browser/resumeview.py中的resume类只是:
class ResumeEdit(BrowserView):
""" A customization of the Resume Edit form
"""
__call__ = ViewPageTemplateFile('resumeedit.cpt')
Run Code Online (Sandbox Code Playgroud)
最后,我在/ product/profiles/default/types/Resume.xml中更改了'edit'的默认别名:
<alias from="edit" to="resume_edit" />
Run Code Online (Sandbox Code Playgroud)
安装后,添加或编辑Resume内容类型会引发此异常:
Unauthorized: The container has no security assertions. Access to 'id' of (Products.Five.browser.pagetemplatefile.ViewPageTemplateFile object at 0x1e8b7a50) denied.
Run Code Online (Sandbox Code Playgroud)
通过提供修补版本的edit_macros.pt可以减轻这种情况:
85c85
< tal:attributes="action python:context.absolute_url()+'/'+template.id;
---
> tal:attributes="action python:context.absolute_url()+'/'+path('template/getId|nothing');
203c203
< tal:attributes="value python:(last_referer …Run Code Online (Sandbox Code Playgroud) 我一直在使用pas.plugins.sqlalchemy为MySQL提供身份验证和成员数据存储的RDBMS后端.身份验证工作完美,成员数据在RDBMS上正确存储和恢复.目前的用户超过20.000
但是,用户枚举需要很长时间.我已经检查了Plone控制面板/用户和组部分中的" 许多用户 ",但即使是简单的用户搜索也需要几乎无限的时间.通过调试plugin.py脚本,我注意到enumerateUsers()被调用的次数与存储的用户数一样多; 因此,完成简单搜索请求需要大量的CPU时间,因为查询与每个用户名匹配,一次一个用户,一次一个查询.
我在这里错过了什么吗?是不是pas.plugins.sqlalchemy有用,特别是当你有一个非常大的用户?目前,我在我的*acl_users/plugins/User Enumeration*设置中将sql插件作为最高优先级.我应该改变吗?
我想重新定义stock folder_contents浏览器View的安全性,以便只有具有Reviewer角色的成员才能访问它.
该类在plone.app.content.browser.foldercontents.FolderContentsView中定义
在我的custom.policy产品中,我有
浏览器/ configure.zcml中:
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="custom.policy">
<browser:page
for="*"
class=".overrides.FolderContentsView"
name="folder_contents"
template="folder_contents.pt"
permission="cmf.ReviewPortalContent"
/>
</configure>
Run Code Online (Sandbox Code Playgroud)
在browser/overrides.py中
from plone.app.content.browser.foldercontents import FolderContentsView
class ProtectedFolderContentsView(FolderContentsView):
""" Customized FolderContentsView """
Run Code Online (Sandbox Code Playgroud)
但是,当我启动实例时,我得到:
zope.configuration.config.ConfigurationConflictError: Conflicting configuration actions
For: ('view', None, u'folder_contents', <InterfaceClass zope.publisher.interfaces.browser.IBrowserRequest>, <InterfaceClass zope.publisher.interfaces.browser.IDefaultBrowserLayer>)
File "src/custom.policy/custom/policy/browser/configure.zcml", line 30.2-36.6
<browser:page
for="*"
class=".overrides.FolderContentsView"
name="folder_contents"
template="folder_contents.pt"
permission="cmf.ReviewPortalContent"
/>
File "eggs/plone.app.content-2.0.7-py2.6.egg/plone/app/content/browser/configure.zcml", line 15.4-20.46
<browser:page
for="*"
class=".foldercontents.FolderContentsView"
name="folder_contents"
template="folder_contents.pt"
permission="cmf.ListFolderContents" />
Run Code Online (Sandbox Code Playgroud)
如何通过遇到冲突来完成此覆盖?
plone ×6
zope ×4
archetypes ×2
python ×2
config ×1
content-type ×1
dexterity ×1
edit ×1
egg ×1
http ×1
indexing ×1
mysql ×1
permissions ×1
registry ×1
rest ×1
sql ×1
sqlalchemy ×1
url-design ×1
view ×1
web ×1