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

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

Uups

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