跳转到主要内容

适用于plone.relations的ATField

项目描述

简介

此包允许您在Archetypes字段中使用基于 plone.app.relations 的关系。它公开了与标准AT引用相同的API,使其成为简单的即插即用替代品。

测试

质数测试,设置我们的组件和一些基本内容

>>> from plonerelations.ATField import tests
>>> tests.base_setup(portal)
>>> ob1 = portal['ob1']
>>> ob2 = portal['ob2']
>>> ob3 = portal['ob3']
>>> ob4 = portal['ob4']
>>> ob5 = portal['ob5']

导入字段,创建其实例,使其为多值

>>> from plonerelations.ATField.ploneRelationsATField import PloneRelationsATField
>>> multiValueATField = PloneRelationsATField ()
>>> multiValueATField.multiValued = True
>>> multiValueATField.relationship = 'test multi valued relationship'

为了保险起见,将单值设置为一个

>>> singleValueATField = PloneRelationsATField ()
>>> singleValueATField.multiValued = False
>>> singleValueATField.relationship = 'test single relationship'

创建一个接口

>>> from zope.interface import Interface
>>> class IMyInterface (Interface):
...     pass

在ATFields上设置relationship_interface

>>> multiValueATField.relationship_interface = IMyInterface
>>> singleValueATField.relationship_interface = IMyInterface

从ob1到objs 2和3创建多关系,从ob4到ob5创建单关系

>>> singleValueATField.set ( ob4, ob5 )
>>> multiValueATField.set ( ob1, [ob2,ob3] )

测试get方法是否返回正确的对象。多值返回一个列表

>>> list (multiValueATField.get( ob1 ))
[<Demo ob2>, <Demo ob3>]
>>> list (multiValueATField.getRaw( ob1, aslist=False))
['ob2', 'ob3']
>>> list (multiValueATField.getRaw( ob1, aslist=True ))
['ob2', 'ob3']

单值返回一个对象

>>> singleValueATField.get ( ob4 )
<Demo ob5>
>>> singleValueATField.getRaw( ob4, aslist=False )
'ob5'
>>> singleValueATField.getRaw( ob4, aslist=True )
['ob5']

再次确认plone.app.interface是否返回相同的结果

>>> from plone.app.relations import interfaces
>>> multiValueSource = interfaces.IRelationshipSource(ob1)
>>> singleValueSource = interfaces.IRelationshipSource(ob4)
>>> multiValueRelationships = list(multiValueSource.getRelationships())
>>> singleValueRelationship = list(singleValueSource.getRelationships())
>>> multiValueRelationships
[<Relationship 'test multi valued relationship' from (<Demo ob1>,) to (<Demo ob2>,)>, <Relationship 'test multi valued relationship' from (<Demo ob1>,) to (<Demo ob3>,)>]
>>> singleValueRelationship
[<Relationship 'test single relationship' from (<Demo ob4>,) to (<Demo ob5>,)>]

检查关系是否提供接口

>>> IMyInterface.providedBy (singleValueRelationship[0])
True
>>> IMyInterface.providedBy (multiValueRelationships[0])
True

检查是否可以删除

>>> multiValueATField.set ( ob1, [ob2,ob5] )
>>> list(multiValueSource.getRelationships())
[<Relationship 'test multi valued relationship' from (<Demo ob1>,) to (<Demo ob2>,)>, <Relationship 'test multi valued relationship' from (<Demo ob1>,) to (<Demo ob5>,)>]
>>> singleValueATField.set ( ob4, ob1 )
>>> singleValueRelationship = list(singleValueSource.getRelationships())
>>> singleValueRelationship
[<Relationship 'test single relationship' from (<Demo ob4>,) to (<Demo ob1>,)>]

变更日志

1.0 - 2011-10-10

  • 为了与Plone 4兼容,移除了__implements__。[hannosch]

  • 更新分发元数据,一些代码清理。[hannosch]

0.1 - 2008年5月20日

  • 首次发布。

项目详情


下载文件

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

源分发

plonerelations.ATField-1.0.zip (16.5 kB 查看哈希)

上传时间

支持者