Commit 2b13e39f authored by iv's avatar iv

ERP5Workflow: WIP for folder comparison

parent b1d54882
This diff is collapsed.
from Products.ERP5Type.Utils import sortValueList
from Products.ERP5Type.Globals import Persistent, PersistentMapping
from Products.ERP5Type import Permissions
from AccessControl import ClassSecurityInfo
from AccessControl import getSecurityManager
from App.special_dtml import DTMLFile
from zope.event import notify
from OFS.event import ObjectWillBeAddedEvent
from OFS.subscribers import compatibilityCall
from zope.lifecycleevent import ObjectAddedEvent
from zope.lifecycleevent import ObjectRemovedEvent
from zope.container.contained import notifyContainerModified
from OFS.Folder import Folder
from Products.CMFCore.PortalFolder import PortalFolderBase
from Products.ERP5Type.Base import Base
from zLOG import LOG
_marker=[]
class ERP5PersistentMappingFolder(PortalFolderBase):
meta_type = 'ERP5 Persistent Mapping Folder'
portal_type = 'ERP5PersistentMappingFolder'
_object_dict = None
# Declarative security
security = ClassSecurityInfo()
security.declareProtected(Permissions.ViewManagementScreens, 'manage_main')
manage_main = DTMLFile('contents', globals())
def __init__(self, id, title=''):
PortalFolderBase.__init__(self, id, title)
self._object_dict = PersistentMapping()
def __getattr__(self, name):
if self._object_dict is None:
LOG("### self._object_dict is None", 0, '')
raise AttributeError(name)
try:
return self._object_dict[name]
except KeyError:
LOG("### KeyError for key " + name + " not in:", 0, self._object_dict)
raise AttributeError(name)
def __len__(self):
return self.objectCount()
security.declareProtected(Permissions.AccessContentsInformation, 'get')
def get(self, name, default=None):
return self._getOb(name, default)
def __getitem__(self, name):
return self._getOb(name)
def _setOb(self, object_id, object_value):
if self._object_dict is None:
self._object_dict = PersistentMapping()
self._object_dict[object_id] = object_value
def _delOb(self, object_id):
del self._object_dict[object_id]
def _getOb(self, object_id, default=_marker):
"""
Return the named object from the folder.
"""
try:
return self._object_dict[object_id].__of__(self)
except KeyError:
if default is _marker:
raise
else:
return default
def objectIds(self, spec=None, **kw):
# we don't plan to use meta_type now, so spec is just here for compatibility
assert(spec is None)
return self._object_dict.keys()
def objectCount(self):
return len(self._object_dict)
def objectItems(self, spec=None):
assert(spec is None)
return self._object_dict
def objectIds_d(self, t=None):
assert(t is None)
return objectIds(self, t)
security.declareProtected( Permissions.AccessContentsInformation,
'objectValues' )
def objectValues(self, spec=None, portal_type=None, sort_on=None,
sort_order=None, checked_permission=None, **kw):
# Returns list of objects contained in this folder.
# (no docstring to prevent publishing)
assert(spec is None)
if self._object_dict is None:
return []
object_list = self._object_dict.values()
if portal_type is not None:
if isinstance(portal_type, str):
portal_type = (portal_type,)
object_list = filter(lambda x: x.getPortalType() in portal_type,
object_list)
if checked_permission is not None:
checkPermission = getSecurityManager().checkPermission
object_list = [o for o in object_list
if checkPermission(checked_permission, o)]
return sortValueList(object_list, sort_on, sort_order, **kw)
security.declareProtected(Permissions.AccessContentsInformation,
'keys', 'items', 'values')
keys = objectIds
values = objectValues
items = objectItems
def _setObject(self, id, object, roles=None, user=None, set_owner=True,
suppress_events=False):
ob = object # better name, keep original function signature
v = self._checkId(id)
if v is not None:
id = v
# If an object by the given id already exists, remove it.
if id in self:
self._delObject(id)
if not suppress_events:
notify(ObjectWillBeAddedEvent(ob, self, id))
self._setOb(id, ob)
ob = self._getOb(id)
if set_owner:
# TODO: eventify manage_fixupOwnershipAfterAdd
# This will be called for a copy/clone, or a normal _setObject.
ob.manage_fixupOwnershipAfterAdd()
# Try to give user the local role "Owner", but only if
# no local roles have been set on the object yet.
if getattr(ob, '__ac_local_roles__', _marker) is None:
user = getSecurityManager().getUser()
if user is not None:
userid = user.getId()
if userid is not None:
ob.manage_setLocalRoles(userid, ['Owner'])
if not suppress_events:
notify(ObjectAddedEvent(ob, self, id))
notifyContainerModified(self)
compatibilityCall('manage_afterAdd', ob, ob, self)
return id
def _cleanup(self):
cleaned = False
for attribute in ('_tree', '_mt_index', '_count'):
try:
if object.__getattribute__(self, attribute):
del self._tree
cleaned = True
except AttributeError:
pass
return cleaned
def has_key(self, key):
if (key in ('.', '..') or
key.startswith('_') or
key.startswith('aq_') or
key.endswith('__') or
self._object_dict is None):
return False
return self._object_dict.get(key, None) is not None
hasObject = has_key
def hashId(self, id):
raise NotImplementedError
def _populateFromFolder(self, source):
"""Fill this folder with the contents of another folder.
"""
for name in source.objectIds():
value = source._getOb(name, None)
if value is not None:
self._setOb(name, aq_base(value))
def tpValues(self):
"""
Ensures the items don't show up in the left pane.
"""
# is this really useful?
return ()
def _checkId(self, id, allow_dup=0):
if self._object_dict is not None and not allow_dup and id in self._object_dict:
raise BadRequestException('The id "%s" is invalid--'
'it is already in use.' % id)
from ERP5PersistentMappingFolder import ERP5PersistentMappingFolder
from Products.ERP5Type.Globals import PersistentMapping
from Products.ERP5Type.Utils import sortValueList
from Products.ERP5Type import Permissions
from AccessControl import ClassSecurityInfo
class ERP5PersistentMappingIndexFolder(ERP5PersistentMappingFolder):
meta_type = 'ERP5 Persistent Mapping Index Folder'
portal_type = 'ERP5PersistentMappingIndexFolder'
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
def __init__(self):
ERP5PersistentMappingFolder.__init__(self)
self._object_index = PersistentMapping()
def _setOb(self, object_id, object_value):
self._object_dict[object_id] = object_value
self._object_index[object_id] = {'portal_type': object_value.getPortalType()}
def _delOb(self, object_id):
del self._object_dict[object_id]
del self._object_index[object_id]
security.declareProtected( Permissions.AccessContentsInformation,
'objectValues' )
def objectValues(self, spec=None, meta_type=None, portal_type=None,
sort_on=None, sort_order=None, checked_permission=None,
**kw):
# Returns list of objects contained in this folder.
# (no docstring to prevent publishing)
if meta_type is not None:
raise NotImplementedError
object_list = self._object_dict.values()
if portal_type is not None:
if isinstance(portal_type, str):
portal_type = (portal_type,)
object_list = [self._object_dict[key] for key in self._object_dict.keys()
if self._object_index[key]['portal_type'] in portal_type]
if checked_permission is not None:
checkPermission = getSecurityManager().checkPermission
object_list = [o for o in object_list
if checkPermission(checked_permission, o)]
return sortValueList(object_list, sort_on, sort_order, **kw)
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment