Passed
Push — master ( 5aaf94...6509f4 )
by Jordi
07:38 queued 03:25
created

ObjectModifiedEventHandler()   A

Complexity

Conditions 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
# -*- coding: utf-8 -*-
2
3
from bika.lims import api
4
from bika.lims.api.snapshot import has_snapshots
5
from bika.lims.api.snapshot import supports_snapshots
6
from bika.lims.api.snapshot import take_snapshot
7
from bika.lims.interfaces import IDoNotSupportSnapshots
8
from DateTime import DateTime
9
from zope.interface import alsoProvides
10
11
12
def reindex_object(obj):
13
    """Reindex the object in the `auditlog_catalog` catalog
14
15
    This is needed *after* the event handlers fired, because the indexing takes
16
    place before the snapshot was created.
17
18
    Also see here for more details:
19
    https://docs.plone.org/develop/addons/components/events.html#modified-events
20
21
    TL;DR: `Products.Archetypes.interfaces.IObjectEditedEvent` is fired after
22
    `reindexObject()` is called. If you manipulate your content object in a
23
    handler for this event, you need to manually reindex new values.
24
    """
25
    auditlog_catalog = api.get_tool("auditlog_catalog")
26
    auditlog_catalog.reindexObject(obj)
27
28
29
def unindex_object(obj):
30
    """Unindex the object in the `auditlog_catalog` catalog
31
    """
32
    auditlog_catalog = api.get_tool("auditlog_catalog")
33
    auditlog_catalog.reindexObject(obj)
34
35
36
def ObjectTransitionedEventHandler(obj, event):
37
    """Object has been transitioned to an new state
38
    """
39
40
    # only snapshot supported objects
41
    if not supports_snapshots(obj):
42
        return
43
44
    # default transition entry
45
    entry = {
46
        "modified": DateTime().ISO(),
47
        "action": event.action,
48
    }
49
50
    # get the last history item
51
    history = api.get_review_history(obj, rev=True)
52
    if history:
53
        entry = history[0]
54
        # make transitions also a modification entry
55
        timestamp = entry.pop("time", DateTime())
56
        entry["modified"] = timestamp.ISO()
57
        entry["action"] = event.action
58
59
    # take a new snapshot
60
    take_snapshot(obj, **entry)
61
62
    # reindex the object in the auditlog catalog
63
    reindex_object(obj)
64
65
66
def ObjectModifiedEventHandler(obj, event):
67
    """Object has been modified
68
    """
69
70
    # only snapshot supported objects
71
    if not supports_snapshots(obj):
72
        return
73
74
    # take a new snapshot
75
    take_snapshot(obj, action="edit")
76
77
    # reindex the object in the auditlog catalog
78
    reindex_object(obj)
79
80
81
def ObjectInitializedEventHandler(obj, event):
82
    """Object has been created
83
    """
84
85
    # only snapshot supported objects
86
    if not supports_snapshots(obj):
87
        return
88
89
    # object has already snapshots
90
    if has_snapshots(obj):
91
        return
92
93
    # take a new snapshot
94
    take_snapshot(obj, action="create")
95
96
97
def ObjectRemovedEventHandler(obj, event):
98
    """Object removed
99
    """
100
101
    # only snapshot supported objects
102
    if not supports_snapshots(obj):
103
        return
104
105
    # unindex the object
106
    unindex_object(obj)
107
108
    # freeze the object
109
    alsoProvides(obj, IDoNotSupportSnapshots)
110