1 | ## $Id: objecthistory.py 11949 2014-11-13 14:40:27Z 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.component import getUtility |
---|
22 | from zope.i18n import translate |
---|
23 | from zope.annotation.interfaces import IAnnotations |
---|
24 | from waeup.ikoba.interfaces import IObjectHistory, IIkobaObject, IIkobaUtils |
---|
25 | from waeup.ikoba.utils.helpers import get_current_principal, now |
---|
26 | |
---|
27 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
28 | |
---|
29 | class ObjectHistory(grok.Adapter): |
---|
30 | """A history for objects. |
---|
31 | |
---|
32 | For any object implementing `IIkobaObject` 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(IIkobaObject) |
---|
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, user=None): |
---|
56 | """Add the message (history entry) in msg. |
---|
57 | |
---|
58 | Any message will be stored with a timestamp and the current |
---|
59 | user (principal) if user parameter is None. |
---|
60 | """ |
---|
61 | msgs = self._getMessages() |
---|
62 | tz = getUtility(IIkobaUtils).tzinfo |
---|
63 | timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z") |
---|
64 | portal_language = getUtility(IIkobaUtils).PORTAL_LANGUAGE |
---|
65 | msg = translate(msg,'waeup.ikoba',target_language=portal_language) |
---|
66 | by = translate(_('by'),'waeup.ikoba',target_language=portal_language) |
---|
67 | if user == None: |
---|
68 | user = get_current_principal() |
---|
69 | if user is None: |
---|
70 | usertitle = 'system' |
---|
71 | elif user.id == 'zope.anybody': |
---|
72 | usertitle = 'Anonymous' |
---|
73 | else: |
---|
74 | usertitle = getattr(user, 'public_name', None) |
---|
75 | if not usertitle: |
---|
76 | usertitle = user.title |
---|
77 | msg = u'%s - %s %s %s' % (timestamp, msg, by, usertitle) |
---|
78 | elif user == 'undisclosed': |
---|
79 | msg = u'%s - %s' % (timestamp, msg) |
---|
80 | else: |
---|
81 | msg = u'%s - %s %s %s' % (timestamp, msg, by, user) |
---|
82 | msgs.append(msg) |
---|
83 | self._annotations[self.history_key] = msgs |
---|
84 | return |
---|
85 | |
---|
86 | def modifyMessages(self, old, new): |
---|
87 | """Replaces history messages. |
---|
88 | |
---|
89 | Substring 'old' will be replaced by 'new' in all |
---|
90 | messages. |
---|
91 | """ |
---|
92 | old_msgs = self._getMessages() |
---|
93 | new_msgs = PersistentList() |
---|
94 | for msg in old_msgs: |
---|
95 | new_msg = msg.replace(old, new) |
---|
96 | new_msgs.append(new_msg) |
---|
97 | self._annotations[self.history_key] = new_msgs |
---|
98 | return |
---|
99 | |
---|
100 | def removeMessage(self, number): |
---|
101 | """Removes a single history message. |
---|
102 | |
---|
103 | """ |
---|
104 | msgs = self._getMessages() |
---|
105 | if not isinstance(number, int): |
---|
106 | return False, 'Not a number' |
---|
107 | try: |
---|
108 | line = msgs[number] |
---|
109 | except IndexError: |
---|
110 | return False, 'Number out of range' |
---|
111 | msgs.pop(number) |
---|
112 | self._annotations[self.history_key] = msgs |
---|
113 | return True, line |
---|