-
Notifications
You must be signed in to change notification settings - Fork 55
/
events.py
71 lines (60 loc) · 2.4 KB
/
events.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- coding: utf-8 -*-
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
@grok.subscribe(ICover, ICheckinEvent)
def override_object_annotations(cover, event):
"""
We need to override the annotations stored in the old object with the
ones in the working copy, and to do that, we first need to remove
old entries.
"""
old_annotations = IAnnotations(event.baseline)
new_annotations = IAnnotations(event.object)
old_keys = list(old_annotations.keys())
for key in old_keys:
# First remove all annotations in relation to tiles
if key.startswith('plone.tiles.'):
del old_annotations[key]
for key in new_annotations:
# Now, copy the ones from the new annotations
if key.startswith('plone.tiles.'):
old_annotations[key] = new_annotations[key]
def modifiedCoverTile(obj, event):
"""Ensure link integrity on Rich Text tiles.
Keyword arguments:
obj -- Dexterity-based object that was modified
event -- event fired
"""
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():
if isinstance(value, RichTextValue):
value = value.raw
if not value:
continue
links = extractLinks(value)
refs |= getObjectsFromLinks(obj.context, links)
updateReferences(IReferenceable(obj.context), referencedRelationship, refs)