Skip to content

Commit

Permalink
Clean to make it run under Plone 5
Browse files Browse the repository at this point in the history
* remove hard dependency to Products.Archetypes
* remove subscriber to IObjectAddedEvent; when a tile is added, there nothing on it so it makes no sense to update link integrity references
* add conditionals to allow running the code under both, Plone 4 and 5
* use a better name for the event handler
* remove all checking that is irrelevant; all of those things have to be present under Plone 4
  • Loading branch information
hvelarde committed Mar 7, 2016
1 parent 95a501b commit 007c700
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 38 deletions.
1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -80,7 +80,6 @@
'plone.tiles',
'plone.uuid',
'plone.z3cform',
'Products.Archetypes',
'Products.CMFCore',
'Products.CMFPlone >=4.2',
'Products.GenericSetup',
Expand Down
45 changes: 19 additions & 26 deletions src/collective/cover/events.py
@@ -1,16 +1,20 @@
# -*- coding: utf-8 -*-
from collective.cover.config import PLONE_VERSION
from collective.cover.interfaces import ICover
from five import grok
from plone import api
from plone.app.iterate.interfaces import ICheckinEvent
from plone.app.linkintegrity.handlers import getObjectsFromLinks
from plone.app.linkintegrity.handlers import referencedRelationship
from plone.app.linkintegrity.parser import extractLinks
from plone.app.linkintegrity.references import updateReferences
from plone.app.textfield.value import RichTextValue
from Products.Archetypes.interfaces import IReferenceable
from zope.annotation.interfaces import IAnnotations

if PLONE_VERSION.startswith('5'):
from plone.app.linkintegrity.handlers import updateReferences
else:
from plone.app.linkintegrity.handlers import referencedRelationship
from plone.app.linkintegrity.references import updateReferences
from Products.Archetypes.interfaces import IReferenceable


@grok.subscribe(ICover, ICheckinEvent)
def override_object_annotations(cover, event):
Expand All @@ -35,29 +39,15 @@ def override_object_annotations(cover, event):
old_annotations[key] = new_annotations[key]


def modifiedCoverTile(obj, event):
"""Ensure link integrity on Rich Text tiles.
def update_link_integrity(obj, event):
"""Update link integrity information on modification/removal of
tiles.
Keyword arguments:
obj -- Dexterity-based object that was modified
event -- event fired
:param obj: cover object that was modified
:type obj: Dexterity-based content type
:param event: event fired
:type event:
"""
pu = api.portal.get_tool('portal_url')
if pu is None:
# `getObjectFromLinks` is not possible without access
# to `portal_url`
return
rc = api.portal.get_tool('reference_catalog')
if rc is None:
# `updateReferences` is not possible without access
# to `reference_catalog`
return
referenceable_parent = IReferenceable(obj.context, None)
if referenceable_parent is None:
# `updateReferences` is not possible
# if parent object isn't referenceable
return

refs = set()

for name, value in obj.data.items():
Expand All @@ -68,4 +58,7 @@ def modifiedCoverTile(obj, event):
links = extractLinks(value)
refs |= getObjectsFromLinks(obj.context, links)

updateReferences(IReferenceable(obj.context), referencedRelationship, refs)
if PLONE_VERSION.startswith('5'):
updateReferences(obj.context, refs)
else:
updateReferences(IReferenceable(obj.context), referencedRelationship, refs)
15 changes: 4 additions & 11 deletions src/collective/cover/subscribers.zcml
@@ -1,31 +1,24 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
xmlns:zcml="http://namespaces.zope.org/zcml"
xmlns:five="http://namespaces.zope.org/five"
>
xmlns:zcml="http://namespaces.zope.org/zcml">

<!-- event handlers for updating link integrity references -->

<subscriber
for="collective.cover.tiles.base.IPersistentCoverTile
OFS.interfaces.IObjectClonedEvent"
handler=".events.modifiedCoverTile"
/>

<subscriber
for="collective.cover.tiles.base.IPersistentCoverTile
zope.lifecycleevent.interfaces.IObjectAddedEvent"
handler=".events.modifiedCoverTile"
handler=".events.update_link_integrity"
/>

<subscriber
for="collective.cover.tiles.base.IPersistentCoverTile
zope.lifecycleevent.interfaces.IObjectModifiedEvent"
handler=".events.modifiedCoverTile"
handler=".events.update_link_integrity"
/>

<subscriber
<subscriber zcml:condition="not-have plone-5"
for="collective.cover.tiles.base.IPersistentCoverTile
zope.lifecycleevent.interfaces.IObjectRemovedEvent"
handler="plone.app.linkintegrity.handlers.referencedObjectRemoved"
Expand Down

0 comments on commit 007c700

Please sign in to comment.