source: main/waeup.sirp/trunk/src/waeup/sirp/objecthistory.py @ 7390

Last change on this file since 7390 was 7371, checked in by Henrik Bettermann, 13 years ago

Uups

  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1## $Id: objecthistory.py 7371 2011-12-18 10:42:19Z 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##
18import grok
19from datetime import datetime
20from persistent.list import PersistentList
21from zope.annotation.interfaces import IAnnotations
22from waeup.sirp.interfaces import IObjectHistory, ISIRPObject
23from waeup.sirp.utils.helpers import get_current_principal
24
25class ObjectHistory(grok.Adapter):
26    """A history for objects.
27
28    For any object implementing `ISIRPObject` 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(ISIRPObject)
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.id == 'zope.anybody':
63            user = 'Anonymous'
64        else:
65            user = user.title
66        msg = '%s - %s by %s' % (timestamp, msg, user)
67        msgs.append(msg)
68        self._annotations[self.history_key] = msgs
69        return
Note: See TracBrowser for help on using the repository browser.