发表于 : 2005-10-12 23:14
不译吧!我觉得译了反而没有了原来的味道,还有点别扭!呵呵leal 写了: Just run it (只待运行了) 或者不译?
Views 视图? 或不译?
不译吧!我觉得译了反而没有了原来的味道,还有点别扭!呵呵leal 写了: Just run it (只待运行了) 或者不译?
Views 视图? 或不译?
下面就是我们的interfaces.py:Here is our interfaces.py:
这里是我们的interfaces.py:
IMark继承自Interface,因此是个接口。Our first interface IMark has two attributes, one is the URL of the site and the other one is the description. Please note, IMark is not a class even though we used Python's class definition. We inherited from Interface to make it an interface. Second one is a container interface, which is an extended IContainer interface. By using this container interface we can persist our data (instances of IMark implementations). We will put all objects of IMark in a container object of IBookMarker. We will implement IMark along with IMarkContained as a constraint interface. So that IMark object will be only contained in an IBookMarker object.
我们的第一个接口IMark有两个属性,一个是站点的URL,另一个是它的描述。请注意,IMark不是一个类,尽管我们使用了Python的类定义。我们继承了Interface,从而使之成了一个 interface(接口)。第二个是一个容器接口,它是一个扩展的Icontainer接口。通过使用这个容器接口我们可以保存我们的数据(IMark 的实例)。我们将IMark的所有对象放到IBookMarker的一个容器对象里。我们连同IMarkContained(作为一个约束接口)一起来实现IMark。所以IMark对象只能包含在IBookMarker对象中。
我已经看到了,不过他们已经翻得差不多了,只是没有放出来而已,而且好象任务也已经有人认领。
代码: 全选
import unittest
from zope.testing.doctestunit import DocTestSuite
from zope.app.container.tests.test_icontainer import TestSampleContainer
from boom.bookmarker import BookMarker, Mark
class BookMarkerContainerTest(TestSampleContainer):
def makeBookMarkerObject(self):
return BookMarker()
def test_suite():
return unittest.TestSuite((
DocTestSuite('boom.bookmarker'),
unittest.makeSuite(BookMarkerContainerTest),
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
代码: 全选
$ cd $HOME/myzope/lib
$ ../bin/test -vpu --dir boom
代码: 全选
__docformat__ = 'restructuredtext'
from zope.interface import implements
from zope.app.container.btree import BTreeContainer
from zope.app.container.contained import Contained
from boom.interfaces import IMark, IMarkContained, IBookMarker
class Mark(Contained):
"""Implementation of IMark
Make sure that the `Mark` implements the `IMark` interface::
>>> from zope.interface.verify import verifyClass
>>> verifyClass(IMark, Mark)
True
Make sure that the `Mark` implements the `IMarkContained` interface:
>>> from zope.interface.verify import verifyClass
>>> verifyClass(IMarkContained, Mark)
True
An example of checking the url of Mark::
>>> mk = Mark()
>>> mk.url
u'http://www.zope.org'
>>> mk.url = u'http://www.python.org'
>>> mk.url
u'http://www.python.org'
An example of checking the description of Mark::
>>> mk = Mark()
>>> mk.description
u''
>>> mk.description = u'Zope Project Web Site'
>>> mk.description
u'Zope Project Web Site'
"""
implements(IMark, IMarkContained)
url = u"http://www.zope.org"
description = u""
class BookMarker(BTreeContainer):
"""Implementation of IBookMarker using B-Tree Container
Make sure that the `BookMarker` implements the `IBookMarker` interface::
>>> from zope.interface.verify import verifyClass
>>> verifyClass(IBookMarker, BookMarker)
True
An example of changing the name of BookMarker::
>>> bm = BookMarker()
>>> bm.name
u''
>>> bm.name = u'MyBookMarker'
>>> bm.name
u'MyBookMarker'
"""
implements(IBookMarker)
name = u""
代码: 全选
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser">
<interface
interface=".interfaces.IBookMarker"
type="zope.app.content.interfaces.IContentType"
/>
<content class=".bookmarker.BookMarker">
<implements
interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
/>
<implements
interface="zope.app.container.interfaces.IContentContainer"
/>
<factory
id="boom.bookmarker.BookMarker"
description="Book Marker"
/>
<require
permission="zope.ManageContent"
interface=".interfaces.IBookMarker"
/>
<require
permission="zope.ManageContent"
set_schema=".interfaces.IBookMarker"
/>
</content>
<interface
interface=".interfaces.IMark"
type="zope.app.content.interfaces.IContentType"
/>
<content class=".bookmarker.Mark">
<implements
interface="zope.app.annotation.interfaces.IAttributeAnnotatable"
/>
<factory
id="boom.bookmarker.Mark"
description="A book mark."
/>
<require
permission="zope.ManageContent"
interface=".interfaces.IMark"/>
<require
permission="zope.ManageContent"
set_schema=".interfaces.IMark"
/>
</content>
<browser:addform
label="Add Book Marker"
name="AddBookMarker.html"
schema="boom.interfaces.IBookMarker"
content_factory="boom.bookmarker.BookMarker"
fields="name"
permission="zope.ManageContent"
/>
<browser:addMenuItem
class=".bookmarker.BookMarker"
title="Book Marker"
permission="zope.ManageContent"
view="AddBookMarker.html"
/>
<browser:editform
schema="boom.interfaces.IBookMarker"
for="boom.interfaces.IBookMarker"
label="Change Book Marker"
name="edit.html"
permission="zope.ManageContent"
menu="zmi_views" title="Edit"
/>
<browser:containerViews
for="boom.interfaces.IBookMarker"
index="zope.View"
contents="zope.View"
add="zope.ManageContent"
/>
<browser:addform
label="Add Mark"
name="AddMark.html"
schema="boom.interfaces.IMark"
content_factory="boom.bookmarker.Mark"
fields="url description"
permission="zope.ManageContent"
/>
<browser:addMenuItem
class="boom.bookmarker.Mark"
title="Mark"
description="URL of Website"
permission="zope.ManageContent"
view="AddMark.html"
/>
<browser:editform
schema="boom.interfaces.IMark"
for="boom.interfaces.IMark"
label="Change Mark"
fields="url description"
name="edit.html"
permission="zope.ManageContent"
menu="zmi_views" title="Edit"
/>
<browser:page
name="marks.html"
for="boom.interfaces.IBookMarker"
class=".browser.BookMarks"
template="marks.pt"
permission="zope.Public"
menu="zmi_views"
title="Marks"
/>
</configure>
代码: 全选
<include package="boom"/>
代码: 全选
from boom.interfaces import IMark
class BookMarks:
def __init__(self, context, request, base_url=''):
self.context = context
self.request = request
self.base_url = base_url
def listMarks(self):
marks = []
for name, child in self.context.items():
if IMark.providedBy(child):
info = {}
info['url'] = child.url
info['description'] = child.description
marks.append(info)
return marks
代码: 全选
<html metal:use-macro="views/standard_macros/view">
<body>
<div metal:fill-slot="body">
<div class="row">
<div class="label">Book Marks:</div>
<br/><br/>
<li tal:repeat="item view/listMarks">
<a href="" tal:attributes="href item/url">
<span tal:content="item/url">Link</span>
</a>
<pre tal:content="item/description">Description</pre>
<br/>
</li>
</div>
</div>
</body>
</html>
代码: 全选
import unittest
from zope.app.testing.functional import BrowserTestCase
class BookMarksTest(BrowserTestCase):
def testMarksListing(self):
pass
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(BookMarksTest),
))
if __name__ == '__main__':
unittest.main(defaultTest='test_suite')
代码: 全选
$ cd $HOME/myzope/lib
$ ../bin/test -vpf --dir boom
它能不言自明(即能够自我解释,XML之类的标记语言都有这个特点)吗?“不...!”好吧,我们将稍后简单的谈谈Zope Configuration Markup Language(ZCML,Zope配置标记语言)。实际上,如果你对ZCML熟悉的话,你会发现这个配置不止是不言自明,它将带给你的是对整个应用的总体思路。现在你可能在想,这并不Pythonic嘛Is it self explanatory? "no...!" then ok! we will discuss Zope Configuration Markup Language (ZCML) briefly later. Actually, if you are familiar with ZCML this configuartion will be more than self explanatory. It will give you an overall idea about the entire application. Now you might think, it is not Pythonic Sad Hey! think twice!.
是对它本身的说明吗?“不...!”好吧,我们将稍后简单的谈谈Zope Configuration Markup Language(ZCML)。实际上,如果你对ZCML熟悉的话,你会发现这个配置不止是对自身的一个说明,它将带给你的是对整个应用的总体思路。现在你可能在想,这并不Pythonic嘛Sad,嗨!再想想!
在文件$HOME/myzope/etc/package-icludes/boom-configure.zcml中插入下列行:As the last step to work our application, put the following line in:$HOME/myzope/etc/package-icludes/boom-configure.zcml:
最后一步就是要让我们的应用运行起来,将下列行插入文件$HOME/myzope/etc/package-icludes/boom-configure.zcml中:
代码:
<include package="boom"/>
…………,暂时休息一下先,之后我们将为书签……Now you want to arrange your your book marks in a better way, don't you?. For the time being, just relax, then we will create a view for book marks.
现在,你不想让你的书签排列得更好点吗?请先放松一下,然后我们来为书签创建一个视图
现在新建一个名为browser.py的文件,并输入下列代码:Now create a file named browser.py with following code:
现在用下列语句来创建一个browser.py文件:
Then one template (marks.pt):
然后是一个模板(marks.pt)