1 # -*- coding: utf-8 -*-
2 #######################################################################################
3 # Plinn - http://plinn.org #
4 # Copyright (C) 2005-2007 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 #######################################################################################
20 """ Plinn implementation of CMFBTree
27 from Products
.BTreeFolder2
.BTreeFolder2
import BTreeFolder2Base
28 from Products
.ZCatalog
.Lazy
import LazyMap
29 from BTrees
.IOBTree
import IOBTree
30 from BTrees
.OIBTree
import OIBTree
31 from Folder
import PlinnFolder
32 from zope
.event
import notify
33 from zope
.app
.container
.contained
import notifyContainerModified
34 from events
import ObjectPositionModified
35 from zope
.component
.factory
import Factory
36 from Products
.CMFCore
.permissions
import AddPortalFolders
, \
38 AccessContentsInformation
39 from AccessControl
import ClassSecurityInfo
40 from Globals
import InitializeClass
41 from types
import StringType
44 class HugePlinnFolder(BTreeFolder2Base
, PlinnFolder
) :
45 """ Plinn Folder for large set of objects
48 security
= ClassSecurityInfo()
50 def __init__(self
, id, title
='') :
51 PlinnFolder
.__init
__(self
, id, title
)
52 BTreeFolder2Base
.__init
__(self
, id)
54 def _initBTrees(self
):
55 super(HugePlinnFolder
, self
)._initBTrees
()
56 self
._pos
2id
_index
= IOBTree()
57 self
._id
2pos
_index
= OIBTree()
59 def _checkId(self
, id, allow_dup
=0) :
60 PlinnFolder
._checkId
(self
, id, allow_dup
)
61 BTreeFolder2Base
._checkId
(self
, id, allow_dup
)
63 security
.declareProtected(AddPortalFolders
, 'manage_addHugePlinnFolder')
64 def manage_addHugePlinnFolder(self
, id, title
='', REQUEST
=None) :
65 """ Add new a new HugePlinnFolder object with id *id*.
67 ob
= HugePlinnFolder(id, title
)
68 self
._setObject
(id, ob
)
69 if REQUEST
is not None :
70 return self
.folder_contents(self
, REQUEST
, portal_status_message
='Folder added')
72 def _setOb(self
, id, object):
73 super(HugePlinnFolder
, self
)._setOb
(id, object)
74 pos
= self
.objectCount() - 1
75 self
._pos
2id
_index
[pos
] = id
76 self
._id
2pos
_index
[id] = pos
79 pos
= self
._id
2pos
_index
[id]
80 self
._id
2pos
_index
.pop(id)
82 for p
in xrange(pos
+1, self
.objectCount()) :
83 ident
= self
._pos
2id
_index
[p
]
84 self
._pos
2id
_index
[p
-1] = ident
85 self
._id
2pos
_index
[ident
] = p
-1
87 self
._pos
2id
_index
.pop(self
.objectCount()-1)
89 super(HugePlinnFolder
, self
)._delOb
(id)
91 security
.declareProtected(AccessContentsInformation
, 'objectIds')
92 def objectIds(self
, spec
=None) :
94 return super(HugePlinnFolder
, self
).objectIds(spec
)
96 pos2id
= lambda pos
: self
._pos
2id
_index
[pos
]
97 return LazyMap(pos2id
, xrange(self
.objectCount()))
101 security
.declareProtected(ManageProperties
, 'moveObjectsByDelta')
102 def moveObjectsByDelta(self
, ids
, delta
, subset_ids
=None,
103 suppress_events
=False):
104 """ Move specified sub-objects by delta.
106 if isinstance(ids
, StringType
):
109 id2pos
= self
._id
2pos
_index
110 pos2id
= self
._pos
2id
_index
112 oldPosition
= id2pos
[id]
113 newPosition
= max(oldPosition
+ delta
, 0)
115 shift
= delta
> 0 and 1 or -1
116 for p
in xrange(oldPosition
, newPosition
, shift
) :
117 ident
= pos2id
[p
+shift
]
120 if not suppress_events
:
121 notify(ObjectPositionModified(self
[ident
], self
, p
))
123 id2pos
[id] = newPosition
124 pos2id
[newPosition
] = id
125 if not suppress_events
:
126 notify(ObjectPositionModified(self
[id], self
, newPosition
))
128 if not suppress_events
:
129 notifyContainerModified(self
)
132 def getObjectPosition(self
, id):
133 """ Get the position of an object by its id.
136 return self
._id
2pos
_index
[id]
138 raise ValueError('The object with the id "%s" does not exist.' % id)
141 InitializeClass(HugePlinnFolder
)
142 HugePlinnFolderFactory
= Factory(HugePlinnFolder
)
143 manage_addHugePlinnFolder
= HugePlinnFolder
.manage_addHugePlinnFolder
.im_func