[7193] | 1 | ## $Id: objecthistory.py 7321 2011-12-10 06:15:17Z 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 |
---|
| 21 | from zope.annotation.interfaces import IAnnotations |
---|
[7321] | 22 | from waeup.sirp.interfaces import IObjectHistory, ISIRPObject |
---|
[6468] | 23 | from waeup.sirp.utils.helpers import get_current_principal |
---|
[6338] | 24 | |
---|
| 25 | class ObjectHistory(grok.Adapter): |
---|
| 26 | """A history for objects. |
---|
[6468] | 27 | |
---|
[7321] | 28 | For any object implementing `ISIRPObject` which is annotatable, |
---|
[6468] | 29 | we provide histories. A history for such an object can be obtained |
---|
| 30 | by adapting it to `IObjectHistory`. |
---|
[6338] | 31 | """ |
---|
[7321] | 32 | grok.context(ISIRPObject) |
---|
[6468] | 33 | grok.implements(IObjectHistory) |
---|
[6338] | 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): |
---|
[6468] | 47 | """Get all messages as a persistent list of strings. |
---|
| 48 | """ |
---|
[6338] | 49 | return self._getMessages() |
---|
| 50 | |
---|
| 51 | def addMessage(self, msg): |
---|
[6468] | 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 | """ |
---|
[6338] | 57 | msgs = self._getMessages() |
---|
[6468] | 58 | timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
---|
| 59 | user = get_current_principal() |
---|
| 60 | if user is None: |
---|
| 61 | user = 'system' |
---|
[6471] | 62 | elif user.title == 'Applicant': |
---|
| 63 | user = 'applicant' |
---|
[6468] | 64 | else: |
---|
[6471] | 65 | user = user.id |
---|
| 66 | msg = '%s - %s by %s' % (timestamp, msg, user) |
---|
[6338] | 67 | msgs.append(msg) |
---|
| 68 | self._annotations[self.history_key] = msgs |
---|
| 69 | return |
---|