Skip to content

Commit

Permalink
Awoid duplicate relations in catalog when republishing.
Browse files Browse the repository at this point in the history
When an object is published for the second time, having the same
relation, it should not be readded to the relation catalog but updated
instead.
By using ``updateRelations`` instead of ``addRelations`` this is handled
correctly by ``z3c.relationfield``.
  • Loading branch information
jone committed Feb 28, 2017
1 parent bd4f3a3 commit f5bcfda
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions ftw/publisher/core/adapters/dx_field_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
else:
HAS_RELATIONS = True
from z3c.relationfield import RelationValue
from z3c.relationfield.event import addRelations
from z3c.relationfield.event import updateRelations
from z3c.relationfield.interfaces import IRelation
from z3c.relationfield.interfaces import IRelationChoice
from z3c.relationfield.interfaces import IRelationList
Expand Down Expand Up @@ -92,7 +92,7 @@ def setData(self, data, metadata):
setattr(repr, name, value)

if HAS_RELATIONS:
addRelations(self.context, None)
updateRelations(self.context, None)

def pack(self, name, field, value):
"""Packs the field data and makes it ready for transportation with
Expand Down
5 changes: 4 additions & 1 deletion ftw/publisher/core/tests/test_dexterity.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from ftw.publisher.core.tests.interfaces import ITextSchema
from json import dumps
from json import loads
from plone import api
from plone.app.relationfield.behavior import IRelatedItems
from plone.app.testing import applyProfile
from plone.app.testing import IntegrationTesting
Expand Down Expand Up @@ -171,6 +170,10 @@ def test_relations_with_RelationValue_objects(self):
self.assertEquals(1, len(target.relatedItems),
'Relation missing')

self._set_field_data(target, data, json=True)
self.assertEquals(1, len(target.relatedItems),
'Publishing twice should not add more relations.')

relation, = target.relatedItems
self.assertEquals(foo, relation.to_object)

Expand Down

0 comments on commit f5bcfda

Please sign in to comment.