source: main/waeup.kofa/trunk/src/waeup/kofa/objecthistory.py @ 8576

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

Show only time zone name.

  • Property svn:keywords set to Id
File size: 2.8 KB
Line 
1## $Id: objecthistory.py 8234 2012-04-20 11:27:59Z 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.component import getUtility
22from zope.i18n import translate
23from zope.annotation.interfaces import IAnnotations
24from waeup.kofa.interfaces import IObjectHistory, IKofaObject, IKofaUtils
25from waeup.kofa.utils.helpers import get_current_principal, now
26
27from waeup.kofa.interfaces import MessageFactory as _
28
29class ObjectHistory(grok.Adapter):
30    """A history for objects.
31
32    For any object implementing `IKofaObject` 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(IKofaObject)
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):
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        """
61        msgs = self._getMessages()
62        tz = getUtility(IKofaUtils).tzinfo
63        timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
64        user = get_current_principal()
65        if user is None:
66            user = 'system'
67        elif user.id == 'zope.anybody':
68            user = 'Anonymous'
69        else:
70            user = user.title
71        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
72        by = translate(_('by'),'waeup.kofa',target_language=portal_language)
73        msg = translate(msg,'waeup.kofa',target_language=portal_language)
74        msg = u'%s - %s %s %s' % (timestamp, msg, by, user)
75        msgs.append(msg)
76        self._annotations[self.history_key] = msgs
77        return
Note: See TracBrowser for help on using the repository browser.