1 | ## $Id: objecthistory.py 7193 2011-11-25 07:21:29Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | import grok |
---|
19 | from datetime import datetime |
---|
20 | from persistent.list import PersistentList |
---|
21 | from zope.annotation.interfaces import IAnnotations |
---|
22 | from waeup.sirp.interfaces import IObjectHistory, IWAeUPObject |
---|
23 | from waeup.sirp.utils.helpers import get_current_principal |
---|
24 | |
---|
25 | class ObjectHistory(grok.Adapter): |
---|
26 | """A history for objects. |
---|
27 | |
---|
28 | For any object implementing `IWAeUPObject` which is annotatable, |
---|
29 | we provide histories. A history for such an object can be obtained |
---|
30 | by adapting it to `IObjectHistory`. |
---|
31 | """ |
---|
32 | grok.context(IWAeUPObject) |
---|
33 | grok.implements(IObjectHistory) |
---|
34 | |
---|
35 | history_key = 'waeup.history' |
---|
36 | |
---|
37 | def __init__(self, context): |
---|
38 | from zope.security.proxy import removeSecurityProxy |
---|
39 | self.context = removeSecurityProxy(context) |
---|
40 | self._annotations = IAnnotations(self.context) |
---|
41 | |
---|
42 | def _getMessages(self): |
---|
43 | return self._annotations.get(self.history_key, PersistentList()) |
---|
44 | |
---|
45 | @property |
---|
46 | def messages(self): |
---|
47 | """Get all messages as a persistent list of strings. |
---|
48 | """ |
---|
49 | return self._getMessages() |
---|
50 | |
---|
51 | def addMessage(self, msg): |
---|
52 | """Add the message (history entry) in msg. |
---|
53 | |
---|
54 | Any message will be stored with a timestamp and the current |
---|
55 | user (principal). |
---|
56 | """ |
---|
57 | msgs = self._getMessages() |
---|
58 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
---|
59 | user = get_current_principal() |
---|
60 | if user is None: |
---|
61 | user = 'system' |
---|
62 | elif user.title == 'Applicant': |
---|
63 | user = 'applicant' |
---|
64 | else: |
---|
65 | user = user.id |
---|
66 | msg = '%s - %s by %s' % (timestamp, msg, user) |
---|
67 | msgs.append(msg) |
---|
68 | self._annotations[self.history_key] = msgs |
---|
69 | return |
---|