Commit 1e0b59d4 authored by iv's avatar iv

ERP5Workflow: improve previous work to avoid overriding edit +

remove custom methods or rename them to start with _ not to be confused with API methods
+ add first prototype of mixinCustomStorageMatrixMixin
parent 1755c61b
......@@ -43,7 +43,7 @@ class PermissionRoles(XMLObject):
portal_type = 'PermissionRoles'
add_permission = Permissions.AddPortalContent
isIndexable = ConstantGetter('isIndexable', value=False)
is_selected = 0 # checkerbox
selected = 0 # checkerbox
isPortalContent = 1
isRADContent = 1
......@@ -63,7 +63,7 @@ class PermissionRoles(XMLObject):
def getPermissionRole(self):
permission = 'None'
role = 'None'
if self.is_selected == 1:
if self.selected == 1:
permission_id = int(self.id.split('_')[1])
role_id = int(self.id.split('_')[2])
# zwj: make sure here gets the right coordinates
......@@ -77,22 +77,21 @@ class PermissionRoles(XMLObject):
role = ['Manager']
return permission, role
def getCellInfo(self):
(cell_permission_index, cell_role_index) = self.id[len(self.base_id+'_'):]\
.split('_')
return {'prefix': self.base_id,
'permission_index': int(cell_permission_index),
'role_index': int(cell_role_index)}
def _getPermissionIndex(self):
return int(self.id[len(self.base_id+'_'):].split('_')[0])
def editStatePermissionRolesFromCellSelection(self, value):
def _getRoleIndex(self):
return int(self.id[len(self.base_id+'_'):].split('_')[1])
def _setSelected(self, value):
"""
edit the parent state's permission/role dict to reflect current cell selection (is_selected) status
edit the parent state's permission/role dict to reflect current cell selection (selected) status
"""
state = self.getParentValue()
cell_info = self.getCellInfo()
cell_range = state.getCellRange()
cell_permission = sorted(cell_range[0])[cell_info['permission_index']]
cell_role = sorted(cell_range[1])[cell_info['role_index']]
cell_permission = sorted(cell_range[0])[self._getPermissionIndex()]
cell_role = sorted(cell_range[1])[self._getRoleIndex()]
# update the state permission structure to take into account
# the selection/non-selection of this cell
if value and (cell_role not in state.state_permission_roles[cell_permission]):
......@@ -107,8 +106,3 @@ class PermissionRoles(XMLObject):
roles = list(set(roles))
roles.remove(cell_role)
state.setPermission(cell_permission, acquired, roles)
def edit(self, *args, **kw):
super(PermissionRoles, self).edit(*args, **kw)
if kw.get('is_selected') is not None:
self.editStatePermissionRolesFromCellSelection(kw['is_selected'])
......@@ -41,7 +41,21 @@ class StateError(Exception):
"""
pass
class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix):
# Prototype of a mixin allowing to have custom storage for matrix
class CustomStorageMatrixMixin(XMLMatrix):
def newCellContent(self, cell_id, **kw):
"""
Creates a new content as a matrix box cell.
"""
cell = self.newContent(id=cell_id, temp_object=True, **kw)
self.updateCellFromCustomStorage(cell)
return cell
def getCell(self, *kw , **kwd):
return self.newCell(*kw , **kwd)
class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, CustomStorageMatrixMixin):
"""
A ERP5 State.
"""
......@@ -52,6 +66,7 @@ class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix):
isRADContent = 1
default_reference = ''
state_type = ()
# Declarative security
security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation)
......@@ -103,39 +118,32 @@ class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix):
'current_inventory',
)
def _getPermissionOrRoleFromInfo(self, cell_prefix, cell_permission_or_role_index, index):
# move next 3 methods to PermissionRoles.py
def _getCellPermissionOrRole(self, cell, is_role):
# we want to get the permission or role from its index,
# so we want the retrieve the key of the dict which is like:
# self.index[cell_info['prefix']][index] = {'Some Role Or Permission': 1}
for key, value in self.index[cell_prefix][index].items():
# self.index[cell_prefix][index] = {'Some Role Or Permission': 1,
# 'Some Other One': 0, ...}
if is_role:
cell_permission_or_role_index = cell._getRoleIndex()
else:
cell_permission_or_role_index = cell._getPermissionIndex()
index = int(is_role)
for key, value in self.index[cell.base_id][index].items():
if cell_permission_or_role_index == value:
return key
return None
def getPermissionFromInfo(self, cell_info):
return self._getPermissionOrRoleFromInfo(
cell_info['prefix'],
cell_info['permission_index'],
0
)
def getRoleFromInfo(self, cell_info):
return self._getPermissionOrRoleFromInfo(
cell_info['prefix'],
cell_info['role_index'],
1
)
raise ValueError('No key found for value %s.' % value)
def newCellContent(self, cell_id, **kw):
def _getCellPermission(self, cell):
return self._getCellPermissionOrRole(cell, False)
def _getCellRole(self, cell):
return self._getCellPermissionOrRole(cell, True)
def updateCellFromCustomStorage(self, cell, **kw):
"""
Creates a new content as a matrix box cell.
"""
cell = self.newContent(id=cell_id, temp_object=True, **kw)
cell_info = cell.getCellInfo()
cell_permission = self.getPermissionFromInfo(cell_info)
cell_role = self.getRoleFromInfo(cell_info)
is_selected = cell_role in self.state_permission_roles[cell_permission]
cell.setIsSelected(is_selected)
return cell
\ No newline at end of file
cell_permission = self._getCellPermission(cell)
cell_role = self._getCellRole(cell)
cell.selected = cell_role in self.state_permission_roles[cell_permission]
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