一个简单的评论包。

zc.comment的Python项目详细描述


支持对象注释列表的简单包。

详细文档

评论

comment包是向任何^{tt1}添加注释的简单方法$ 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)

接下来我们需要使sute设置注释机制,因为 Comments适配器需要能够对调整后的对象进行注释:

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

如您所见,不需要手动创建comments对象, 但只要把课文交上来就行了。显然添加了一条评论:

>>> len(comments)
1

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

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

现在我们以贝蒂的身份登录:

>>> zope.security.management.endInteraction()
>>> zope.security.management.newInteraction(Participation(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('http://localhost/@@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)

  • 初始版本

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

推荐PyPI第三方库


热门话题
java理解泛型   java Guava:如何自定义减少多重映射?   java无法构建实体管理器工厂JPA/Hibernate   不区分大小写的LDAP搜索   在java中同时调用所有类对象中的方法   java做高级数字计算?2.1k等于2100等。。?   java Camel netty组件:未能创建选择器   exceljava。lang.ClassCastException:ExcelStreamAction无法强制转换为com。开放交响乐团。xwork2。行动   java避免对嵌套a4j:区域进行验证   java如何使一帧在1秒内显示50次,每次显示时消失   java一个HashMap的遍历,我得到NullPointerException   windows HP Stream 8平板电脑。。。Java swing JScrollPane滚动在触摸屏上不工作   java如何在运行时根据用户/程序员的需要自动增加数组的大小?