-
Notifications
You must be signed in to change notification settings - Fork 55
/
events.py
64 lines (53 loc) · 2.17 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
# -*- coding: utf-8 -*-
from collective.cover.config import PLONE_VERSION
from collective.cover.interfaces import ICover
from five import grok
from plone.app.iterate.interfaces import ICheckinEvent
from plone.app.linkintegrity.handlers import getObjectsFromLinks
from plone.app.linkintegrity.parser import extractLinks
from plone.app.textfield.value import RichTextValue
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):
"""
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 update_link_integrity(obj, event):
"""Update link integrity information on modification/removal of
tiles.
:param obj: cover object that was modified
:type obj: Dexterity-based content type
:param event: event fired
:type event:
"""
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)
if PLONE_VERSION.startswith('5'):
updateReferences(obj.context, refs)
else:
updateReferences(IReferenceable(obj.context), referencedRelationship, refs)