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

Last change on this file since 9309 was 9126, checked in by Henrik Bettermann, 12 years ago

Catch errors and return message removed.

  • Property svn:keywords set to Id
File size: 3.8 KB
RevLine 
[7193]1## $Id: objecthistory.py 9126 2012-08-30 08:11:58Z 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
[7679]21from zope.component import getUtility
22from zope.i18n import translate
[6338]23from zope.annotation.interfaces import IAnnotations
[7819]24from waeup.kofa.interfaces import IObjectHistory, IKofaObject, IKofaUtils
[8186]25from waeup.kofa.utils.helpers import get_current_principal, now
[6338]26
[7811]27from waeup.kofa.interfaces import MessageFactory as _
[7679]28
[6338]29class 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()
[8183]62        tz = getUtility(IKofaUtils).tzinfo
[8234]63        timestamp = now(tz).strftime("%Y-%m-%d %H:%M:%S %Z")
[6468]64        user = get_current_principal()
65        if user is None:
[8758]66            usertitle = 'system'
[7370]67        elif user.id == 'zope.anybody':
[8758]68            usertitle = 'Anonymous'
[6468]69        else:
[8758]70            usertitle = getattr(user, 'public_name', None)
71            if not usertitle:
72                usertitle = user.title
[7819]73        portal_language = getUtility(IKofaUtils).PORTAL_LANGUAGE
[7959]74        by = translate(_('by'),'waeup.kofa',target_language=portal_language)
75        msg = translate(msg,'waeup.kofa',target_language=portal_language)
[8758]76        msg = u'%s - %s %s %s' % (timestamp, msg, by, usertitle)
[6338]77        msgs.append(msg)
78        self._annotations[self.history_key] = msgs
79        return
[9012]80
81    def modifyMessages(self, old, new):
[9013]82        """Replaces history messages.
[9012]83
[9013]84        Substring 'old' will be replaced by 'new' in all
[9012]85        messages.
86        """
87        old_msgs = self._getMessages()
88        new_msgs = PersistentList()
89        for msg in old_msgs:
90            new_msg = msg.replace(old, new)
91            new_msgs.append(new_msg)
92        self._annotations[self.history_key] = new_msgs
[9125]93        return
94
95    def removeMessage(self, number):
96        """Removes a single history message.
97
98        """
99        msgs = self._getMessages()
[9126]100        if not isinstance(number, int):
101            return False, 'Not a number'
102        try:
103            line = msgs[number]
104        except IndexError:
105            return False, 'Number out of range'
[9125]106        msgs.pop(number)
107        self._annotations[self.history_key] = msgs
[9126]108        return True, line
Note: See TracBrowser for help on using the repository browser.