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