[7193] | 1 | ## $Id: objecthistory.py 7959 2012-03-22 15:51:23Z henrik $ |
---|
[6338] | 2 | ## |
---|
[7193] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[6338] | 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. |
---|
[7193] | 8 | ## |
---|
[6338] | 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. |
---|
[7193] | 13 | ## |
---|
[6338] | 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 |
---|
[6468] | 19 | from datetime import datetime |
---|
[6338] | 20 | from persistent.list import PersistentList |
---|
[7679] | 21 | from zope.component import getUtility |
---|
| 22 | from zope.i18n import translate |
---|
[6338] | 23 | from zope.annotation.interfaces import IAnnotations |
---|
[7819] | 24 | from waeup.kofa.interfaces import IObjectHistory, IKofaObject, IKofaUtils |
---|
[7811] | 25 | from waeup.kofa.utils.helpers import get_current_principal |
---|
[6338] | 26 | |
---|
[7811] | 27 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7679] | 28 | |
---|
[6338] | 29 | class ObjectHistory(grok.Adapter): |
---|
| 30 | """A history for objects. |
---|
[6468] | 31 | |
---|
[7819] | 32 | For any object implementing `IKofaObject` which is annotatable, |
---|
[6468] | 33 | we provide histories. A history for such an object can be obtained |
---|
| 34 | by adapting it to `IObjectHistory`. |
---|
[6338] | 35 | """ |
---|
[7819] | 36 | grok.context(IKofaObject) |
---|
[6468] | 37 | grok.implements(IObjectHistory) |
---|
[6338] | 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): |
---|
[6468] | 51 | """Get all messages as a persistent list of strings. |
---|
| 52 | """ |
---|
[6338] | 53 | return self._getMessages() |
---|
| 54 | |
---|
| 55 | def addMessage(self, msg): |
---|
[6468] | 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 | """ |
---|
[6338] | 61 | msgs = self._getMessages() |
---|
[6468] | 62 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
---|
| 63 | user = get_current_principal() |
---|
| 64 | if user is None: |
---|
| 65 | user = 'system' |
---|
[7370] | 66 | elif user.id == 'zope.anybody': |
---|
| 67 | user = 'Anonymous' |
---|
[6468] | 68 | else: |
---|
[7370] | 69 | user = user.title |
---|
[7819] | 70 | portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE |
---|
[7959] | 71 | by = translate(_('by'),'waeup.kofa',target_language=portal_language) |
---|
| 72 | msg = translate(msg,'waeup.kofa',target_language=portal_language) |
---|
[7681] | 73 | msg = u'%s - %s %s %s' % (timestamp, msg, by, user) |
---|
[6338] | 74 | msgs.append(msg) |
---|
| 75 | self._annotations[self.history_key] = msgs |
---|
| 76 | return |
---|