1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Plinn - http://plinn.org #
4 # Copyright © 2005-2009 Benoît PIN <benoit.pin@ensmp.fr> #
6 # This program is free software; you can redistribute it and/or #
7 # modify it under the terms of the GNU General Public License #
8 # as published by the Free Software Foundation; either version 2 #
9 # of the License, or (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program; if not, write to the Free Software #
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
19 #######################################################################################
21 Module to manage history of contents (comparisons, copy to present).
27 from Globals
import InitializeClass
28 from AccessControl
import ClassSecurityInfo
29 from ExtensionClass
import Base
31 from zope
.interface
import implements
32 from OFS
.History
import historicalRevision
33 from Products
.PluginIndexes
.common
import safe_callable
34 from interfaces
import IContentHistory
35 from Products
.CMFCore
.permissions
import ModifyPortalContent
36 from permissions
import ViewHistory
37 from struct
import pack
, unpack
38 from DateTime
import DateTime
40 class ContentHistory(Base
, Acquisition
.Implicit
) :
41 """ Utility to manage historical entries of a content
43 implements(IContentHistory
)
44 security
= ClassSecurityInfo()
46 def __init__(self
, content
) :
47 self
._content
= content
49 security
.declareProtected(ViewHistory
, 'listEntries')
50 def listEntries(self
, first
=0, last
=20):
51 oid
= self
._content
._p
_oid
52 db
= self
._content
._p
_jar
.db()
53 r
= db
.history(oid
, None, last
)
56 # storage doesn't support history
62 d
['time']=DateTime(d
['time'])
63 d
['key']='.'.join(map(str, unpack(">HHHH", d
['tid'])))
67 security
.declareProtected(ViewHistory
, 'getHistoricalRevisionByKey')
68 def getHistoricalRevisionByKey(self
, key
, withContext
=False):
70 serial
= apply(pack
, ('>HHHH',) + tuple(map(int, key
.split('.'))))
71 content
= self
._content
72 rev
= historicalRevision(content
, serial
)
73 rev
= rev
.__of
__(content
.aq_parent
)
74 if withContext
is False :
75 return rev
, DateTime(rev
._p
_mtime
)
78 if isinstance(withContext
, int) :
79 first
= max(withContext
-1, 0)
80 entries
= self
.listEntries(first
=first
, last
=withContext
+2)
82 if len(entries
) == 3 :
83 ctx
['next'], ctx
['current'], ctx
['previous'] = entries
84 elif len(entries
) == 2 :
85 serials
= [e
['tid'] for e
in entries
]
86 i
= serials
.index(serial
)
88 # last (newest) transaction
89 ctx
['current'], ctx
['previous'] = entries
91 ctx
['next'], ctx
['current'] = entries
92 elif len(entries
) == 1 :
93 ctx
['current'] = entries
[0]
97 security
.declareProtected(ViewHistory
, 'compare')
98 def compare(self
, leftkey
, rightkey
):
99 raise NotImplementedError
102 security
.declareProtected(ModifyPortalContent
, 'restore')
103 def restore(self
, key
):
104 raise NotImplementedError
106 InitializeClass(ContentHistory
)