Skip to content

Commit

Permalink
Merge pull request #52 from plone/thet-anyreference
Browse files Browse the repository at this point in the history
check any reference in delete confirmation dialog
  • Loading branch information
mauritsvanrees authored Apr 4, 2017
2 parents 6d82b49 + a02c009 commit 36f777c
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 12 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Breaking changes:

New features:

- *add item here*
- Support for any ``zc.relation`` refercences being checked by ``delete_confirmation_info`` dialog,
not only references linked in text.
[thet]

Bug fixes:

Expand Down
2 changes: 1 addition & 1 deletion plone/app/linkintegrity/browser/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def check_object(self, obj, excluded_path=None):
Breaches originating from excluded_path are ignored.
"""
breaches = {}
direct_links = getIncomingLinks(obj)
direct_links = getIncomingLinks(obj, from_attribute=None)
has_breaches = False
for direct_link in direct_links:
source_path = direct_link.from_path
Expand Down
33 changes: 31 additions & 2 deletions plone/app/linkintegrity/tests/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
from plone.app.linkintegrity.parser import extractLinks
from plone.app.linkintegrity.tests.base import ATBaseTestCase
from plone.app.linkintegrity.tests.base import DXBaseTestCase
from plone.app.linkintegrity.utils import hasIncomingLinks
from plone.app.linkintegrity.utils import hasOutgoingLinks
from plone.app.linkintegrity.utils import getIncomingLinks
from plone.app.linkintegrity.utils import getOutgoingLinks
from plone.app.linkintegrity.utils import hasIncomingLinks
from plone.app.linkintegrity.utils import hasOutgoingLinks
from plone.app.testing import login
from plone.app.testing import logout
from plone.app.testing import TEST_USER_NAME
from z3c.relationfield import RelationValue
from z3c.relationfield.event import _setRelation
from zc.relation.interfaces import ICatalog
from zope.component import getUtility
from zope.intid.interfaces import IIntIds
from zope.lifecycleevent import modified


Expand Down Expand Up @@ -164,6 +169,30 @@ def test_reference_orthogonality(self):
# And again the related item reference remains in place:
self.assertEqual(self._get_related_items(doc), [img, ])

def test_delete_confirmation_for_any_reference(self):
"""Test, if delete confirmation shows also a warning if items are
deleted, which are referenced by other items via a reference field.
"""
img1 = self.portal['image1']
doc1 = self.portal['doc1']

intids_tool = getUtility(IIntIds)
to_id = intids_tool.getId(img1)
rel = RelationValue(to_id)
_setRelation(doc1, 'related_image', rel)

# Test, if relation is present in the relation catalog
catalog = getUtility(ICatalog)
rels = list(catalog.findRelations({'to_id': to_id}))
self.assertEqual(len(rels), 1)

# Test, if delete_confirmation_info shows also other relations than
# ``isReferencing``.
info = img1.restrictedTraverse('@@delete_confirmation_info')
breaches = info.get_breaches()
self.assertEqual(len(breaches), 1)
self.assertEqual(len(info.get_breaches()[0]['sources']), 1)


class ReferenceGenerationDXTestCase(DXBaseTestCase, ReferenceGenerationTestCase):
"""Reference generation testcase for dx content types"""
Expand Down
26 changes: 18 additions & 8 deletions plone/app/linkintegrity/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
from zope.intid.interfaces import IIntIds


def getIncomingLinks(obj=None, intid=None):
def getIncomingLinks(
obj=None,
intid=None,
from_attribute=referencedRelationship
):
"""Return a generator of incoming relations created using
plone.app.linkintegrity (Links in Richtext-Fields).
"""
catalog = getUtility(ICatalog)
intid = intid if intid is not None else getUtility(IIntIds).getId(obj)
return catalog.findRelations({
'to_id': intid,
'from_attribute': referencedRelationship})
query = {'to_id': intid}
if from_attribute:
query['from_attribute'] = from_attribute
return catalog.findRelations(query)


def hasIncomingLinks(obj=None, intid=None):
Expand All @@ -29,15 +34,20 @@ def hasIncomingLinks(obj=None, intid=None):
return False


def getOutgoingLinks(obj=None, intid=None):
def getOutgoingLinks(
obj=None,
intid=None,
from_attribute=referencedRelationship
):
"""Return a generator of outgoing relations created using
plone.app.linkintegrity (Links in Richtext-Fields).
"""
catalog = getUtility(ICatalog)
intid = intid if intid is not None else getUtility(IIntIds).getId(obj)
return catalog.findRelations({
'from_id': intid,
'from_attribute': referencedRelationship})
query = {'from_id': intid}
if from_attribute:
query['from_attribute'] = from_attribute
return catalog.findRelations(query)


def hasOutgoingLinks(obj=None, intid=None):
Expand Down

0 comments on commit 36f777c

Please sign in to comment.