跳转到主要内容

一个简单的评论包。

项目描述

一个简单的包,用于支持为对象添加评论列表。

详细文档

评论

评论包是一种简单的方法,可以为任何 IAnnotatable Zope内容添加评论。日期时间和当前主体将被打印到评论中。评论正文目前是简单的Unicode文本,但计划稍后将其变为HTML片段(“富文本”)。

包含当前主体的要求需要进行交互,这是我们在此处使用系统之前需要设置的内容。下面,我们使用虚拟的交互和参与者设置了一个虚拟的交互,创建了一些IAttributeAnnotatable内容,然后最终展示了系统的使用。

为了创建一个参与,我们需要一些主体

>>> import zope.security.management
>>> import zope.security.interfaces
>>> from zope import interface
>>> class Principal(object):
...     interface.implements(zope.security.interfaces.IPrincipal)
...
...     def __init__(self, id, title, description):
...         self.id = id
...         self.title = title
...         self.description = description
...
...     def __repr__(self):
...         return '<%s %r>' %(self.__class__.__name__, self.id)
>>> alice = Principal('alice', 'Alice Aal', 'first principal')
>>> betty = Principal('betty', 'Betty Barnes', 'second principal')

现在我们可以创建一个参与

>>> class Participation(object):
...     zope.interface.implements(
...         zope.security.interfaces.IParticipation,
...         zope.publisher.interfaces.IRequest)
...     interaction = principal = None
...
...     def __init__(self, principal):
...         self.principal = principal
...
...     def __repr__(self):
...         return '<%s %r>' %(self.__class__.__name__, self.principal)

接下来,我们需要确保注释机制已设置,因为评论适配器需要能够注释被适配的对象

>>> import zope.component
>>> import zope.annotation
>>> zope.component.provideAdapter(
...     zope.annotation.attribute.AttributeAnnotations)

现在让我们确保所有可评论的对象都可以接收评论

>>> from zc.comment import comment
>>> zope.component.provideAdapter(comment.CommentsFactory)

现在我们已经设置了一切,让我们看看它是如何工作的。首先我们需要一个简单的内容组件

>>> class SimpleContent(object):
...     interface.implements(
...         zope.annotation.interfaces.IAttributeAnnotatable)
...     def __init__(self, name):
...         self.name = name
...     def __repr__(self):
...         return '<%s %r>' %(self.__class__.__name__, self.name)
>>> content = SimpleContent(u'content')

为了与评论互动,我们现在必须注册一个新的参与。在我们的例子中,Alice想要创建一个评论

>>> zope.security.management.endInteraction()
>>> zope.security.management.newInteraction(Participation(alice))

我们可以通过适配到 IComments 来访问对象的评论

>>> from zc.comment import interfaces
>>> comments = interfaces.IComments(content)
Traceback (most recent call last):
...
TypeError: ('Could not adapt',
            <SimpleContent u'content'>,
            <InterfaceClass zc.comment.interfaces.IComments>)

最初,该组件不可评论,因为它没有提供正确的接口

>>> zope.interface.directlyProvides(content, interfaces.ICommentable)
>>> comments = interfaces.IComments(content)
>>> comments
<Comments (0) for <SimpleContent u'content'>>

现在让我们添加一个评论

>>> import datetime, pytz
>>> before = datetime.datetime.now(pytz.utc)
>>> comments.add(u"Foo!  Bar!")
>>> after = datetime.datetime.now(pytz.utc)

如您所见,无需手动创建注释对象,只需传入文本即可。显然,已经添加了注释

>>> len(comments)
1

现在让我们确保数据已正确设置

>>> comments[0].body
u'Foo!  Bar!'
>>> before <= comments[0].date <= after
True
>>> comments[0].principal_ids
('alice',)

现在让我们以Betty的身份登录

>>> zope.security.management.endInteraction()
>>> zope.security.management.newInteraction(Participation(betty))

Betty也可以添加注释

>>> comments = interfaces.IComments(content)
>>> before = datetime.datetime.now(pytz.utc)
>>> comments.add(u"Shazam")
>>> after = datetime.datetime.now(pytz.utc)
>>> len(comments)
2

她的注释也已正确存储

>>> comments[1].body
u'Shazam'
>>> before <= comments[1].date <= after
True
>>> comments[1].principal_ids
('betty',)

现在让我们确保在多个参与者交互时,所有参与者都能被选中

>>> zope.security.management.endInteraction()
>>> zope.security.management.newInteraction(
...     Participation(alice), Participation(betty))
>>> comments.add(u"Boom.")
>>> len(comments)
3
>>> comments[2].body
u'Boom.'
>>> comments[2].principal_ids
('alice', 'betty')

最后,请注意,我们只能添加Unicode文本作为有效的注释

>>> comments.add(42)
Traceback (most recent call last):
...
WrongType: (42, <type 'unicode'>)

如果您愿意,您始终可以清除所有注释

>>> comments.clear()
>>> len(comments)
0

当然,还需要一些清理

>>> zope.security.management.endInteraction()

评论界面

创建我们将使用的浏览器对象。

>>> from zope.testbrowser.testing import Browser
>>> browser = Browser()
>>> browser.addHeader('Accept-Language', 'test')

为了了解注释如何工作,我们将创建一个简单内容对象的实例

>>> browser.open('https://127.0.0.1/@@contents.html')
>>> browser.getLink('[[zope][[top]]]').click()
>>> browser.getLink('[[zc.comment][Content]]').click()
>>> browser.getControl(name='new_value').value = 'number'
>>> browser.getControl('[[zope][container-apply-button (Apply)]]').click()

现在让我们访问该对象并点击注释选项卡

>>> browser.handleErrors = False
>>> browser.getLink('number').click()
>>> browser.getLink('[[zc.comment][Comments]]').click()

我们看到还没有发表任何评论

>>> '[[zc.intranet][No comments have been made.]]' in browser.contents
True

现在让我们添加一条新的多行注释

>>> browser.getControl('[[zc.comment][New Comment]]').value = '''\
... I give my pledge, as an Earthling
... to save, and faithfully defend from waste
... the natural resources of my planet.
... It's soils, minerals, forests, waters, and wildlife.
... '''
>>> browser.getControl('[[zc.comment][Add Comment]]').click()

现在,我们得到一个表格,显示注释及其日期、文本和创建注释的用户

>>> print browser.contents
<...
      <th>
      ...[[zc.comment][comment_column-date (Date)]]...
      </th>
      <th>
      ...[[zc.comment][comment_column-principals (Principals)]]...
      </th>
      <th>
        [[zc.comment][comment_column-comment (Comment)]]
      </th>
    ...
    <td>
      2005 11 14  12:00:55 -500
    </td>
    <td>
      Unauthenticated User
    </td>
    <td>
      I give my pledge, as an Earthling<br />
to save, and faithfully defend from waste<br />
the natural resources of my planet.<br />
It's soils, minerals, forests, waters, and wildlife.<br />
...
 <label for="form.comment">
    <span class="required">*</span><span>[[zc.comment][New Comment]]</span>
  </label>
  ...<textarea class="zc-comment-text"
               style="width: 50ex; height: 6em;"
               cols="60" id="form.comment"
               name="form.comment" rows="15" ></textarea></div>
...
    <input type="submit"
           id="form.actions.41646420436f6d6d656e74"
           name="form.actions.41646420436f6d6d656e74"
           value="[[zc.comment][Add Comment]]"
           class="button" />
...

现在,我们将添加另一条评论。

>>> browser.getControl('[[zc.comment][New Comment]]'
...     ).value = 'another comment'
>>> browser.getControl('[[zc.comment][Add Comment]]').click()
>>> print browser.contents
<...
      <th>
...[[zc.comment][comment_column-date (Date)]]...
      </th>
      <th>
...[[zc.comment][comment_column-principals (Principals)]]...
      </th>
      <th>
        [[zc.comment][comment_column-comment (Comment)]]
      </th>
  </tr>
...
    <td>
      2005 11 14  12:10:18 -500
    </td>
    <td>
      Unauthenticated User
    </td>
    <td>
      I give my pledge, as an Earthling<br />
to save, and faithfully defend from waste<br />
the natural resources of my planet.<br />
It's soils, minerals, forests, waters, and wildlife.<br />
<BLANKLINE>
    </td>
  </tr>
  ...
    <td>
      2005 11 14  12:10:18 -500
    </td>
    <td>
      Unauthenticated User
    </td>
    <td>
      another comment
    </td>
  </tr>
...
<label for="form.comment">
  <span class="required">*</span><span>[[zc.comment][New Comment]]</span>
</label>
...
...<textarea class="zc-comment-text"
             style="width: 50ex; height: 6em;"
             cols="60"
             id="form.comment"
             name="form.comment"
             rows="15" ></textarea>...
    <input type="submit"
           id="form.actions.41646420436f6d6d656e74"
           name="form.actions.41646420436f6d6d656e74"
           value="[[zc.comment][Add Comment]]"
           class="button" />
...

变更

0.1.0 (2008-04-21)

  • 初始发布

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

zc.comment-0.1.0.tar.gz (13.1 kB 查看哈希)

上传时间

由以下支持