|
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 DateTime import DateTime |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def reindex_object(obj): |
|
11
|
|
|
"""Reindex the object in the `auditlog_catalog` catalog |
|
12
|
|
|
|
|
13
|
|
|
This is needed *after* the event handlers fired, because the indexing takes |
|
14
|
|
|
place before the snapshot was created. |
|
15
|
|
|
|
|
16
|
|
|
Also see here for more details: |
|
17
|
|
|
https://docs.plone.org/develop/addons/components/events.html#modified-events |
|
18
|
|
|
|
|
19
|
|
|
TL;DR: `Products.Archetypes.interfaces.IObjectEditedEvent` is fired after |
|
20
|
|
|
`reindexObject()` is called. If you manipulate your content object in a |
|
21
|
|
|
handler for this event, you need to manually reindex new values. |
|
22
|
|
|
""" |
|
23
|
|
|
auditlog_catalog = api.get_tool("auditlog_catalog") |
|
24
|
|
|
auditlog_catalog.reindexObject(obj) |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
def ObjectTransitionedEventHandler(obj, event): |
|
28
|
|
|
"""Object has been transitioned to an new state |
|
29
|
|
|
""" |
|
30
|
|
|
|
|
31
|
|
|
# only snapshot supported objects |
|
32
|
|
|
if not supports_snapshots(obj): |
|
33
|
|
|
return |
|
34
|
|
|
|
|
35
|
|
|
# default transition entry |
|
36
|
|
|
entry = { |
|
37
|
|
|
"modified": DateTime().ISO(), |
|
38
|
|
|
"action": event.action, |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
# get the last history item |
|
42
|
|
|
history = api.get_review_history(obj, rev=True) |
|
43
|
|
|
if history: |
|
44
|
|
|
entry = history[0] |
|
45
|
|
|
# make transitions also a modification entry |
|
46
|
|
|
timestamp = entry.pop("time", DateTime()) |
|
47
|
|
|
entry["modified"] = timestamp.ISO() |
|
48
|
|
|
entry["action"] = event.action |
|
49
|
|
|
|
|
50
|
|
|
# take a new snapshot |
|
51
|
|
|
take_snapshot(obj, **entry) |
|
52
|
|
|
|
|
53
|
|
|
# reindex the object in the auditlog catalog |
|
54
|
|
|
reindex_object(obj) |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
def ObjectModifiedEventHandler(obj, event): |
|
58
|
|
|
"""Object has been modified |
|
59
|
|
|
""" |
|
60
|
|
|
|
|
61
|
|
|
# only snapshot supported objects |
|
62
|
|
|
if not supports_snapshots(obj): |
|
63
|
|
|
return |
|
64
|
|
|
|
|
65
|
|
|
# take a new snapshot |
|
66
|
|
|
take_snapshot(obj, action="edit") |
|
67
|
|
|
|
|
68
|
|
|
# reindex the object in the auditlog catalog |
|
69
|
|
|
reindex_object(obj) |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
def ObjectInitializedEventHandler(obj, event): |
|
73
|
|
|
"""Object has been created |
|
74
|
|
|
""" |
|
75
|
|
|
|
|
76
|
|
|
# only snapshot supported objects |
|
77
|
|
|
if not supports_snapshots(obj): |
|
78
|
|
|
return |
|
79
|
|
|
|
|
80
|
|
|
# object has already snapshots |
|
81
|
|
|
if has_snapshots(obj): |
|
82
|
|
|
return |
|
83
|
|
|
|
|
84
|
|
|
# take a new snapshot |
|
85
|
|
|
take_snapshot(obj, action="create") |
|
86
|
|
|
|