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): ...@@ -43,7 +43,7 @@ class PermissionRoles(XMLObject):
portal_type = 'PermissionRoles' portal_type = 'PermissionRoles'
add_permission = Permissions.AddPortalContent add_permission = Permissions.AddPortalContent
isIndexable = ConstantGetter('isIndexable', value=False) isIndexable = ConstantGetter('isIndexable', value=False)
is_selected = 0 # checkerbox selected = 0 # checkerbox
isPortalContent = 1 isPortalContent = 1
isRADContent = 1 isRADContent = 1
...@@ -63,7 +63,7 @@ class PermissionRoles(XMLObject): ...@@ -63,7 +63,7 @@ class PermissionRoles(XMLObject):
def getPermissionRole(self): def getPermissionRole(self):
permission = 'None' permission = 'None'
role = 'None' role = 'None'
if self.is_selected == 1: if self.selected == 1:
permission_id = int(self.id.split('_')[1]) permission_id = int(self.id.split('_')[1])
role_id = int(self.id.split('_')[2]) role_id = int(self.id.split('_')[2])
# zwj: make sure here gets the right coordinates # zwj: make sure here gets the right coordinates
...@@ -77,22 +77,21 @@ class PermissionRoles(XMLObject): ...@@ -77,22 +77,21 @@ class PermissionRoles(XMLObject):
role = ['Manager'] role = ['Manager']
return permission, role return permission, role
def getCellInfo(self): def _getPermissionIndex(self):
(cell_permission_index, cell_role_index) = self.id[len(self.base_id+'_'):]\ return int(self.id[len(self.base_id+'_'):].split('_')[0])
.split('_')
return {'prefix': self.base_id,
'permission_index': int(cell_permission_index),
'role_index': int(cell_role_index)}
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() state = self.getParentValue()
cell_info = self.getCellInfo()
cell_range = state.getCellRange() cell_range = state.getCellRange()
cell_permission = sorted(cell_range[0])[cell_info['permission_index']] cell_permission = sorted(cell_range[0])[self._getPermissionIndex()]
cell_role = sorted(cell_range[1])[cell_info['role_index']] cell_role = sorted(cell_range[1])[self._getRoleIndex()]
# update the state permission structure to take into account # update the state permission structure to take into account
# the selection/non-selection of this cell # the selection/non-selection of this cell
if value and (cell_role not in state.state_permission_roles[cell_permission]): if value and (cell_role not in state.state_permission_roles[cell_permission]):
...@@ -107,8 +106,3 @@ class PermissionRoles(XMLObject): ...@@ -107,8 +106,3 @@ class PermissionRoles(XMLObject):
roles = list(set(roles)) roles = list(set(roles))
roles.remove(cell_role) roles.remove(cell_role)
state.setPermission(cell_permission, acquired, roles) 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): ...@@ -41,7 +41,21 @@ class StateError(Exception):
""" """
pass 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. A ERP5 State.
""" """
...@@ -52,6 +66,7 @@ class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix): ...@@ -52,6 +66,7 @@ class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix):
isRADContent = 1 isRADContent = 1
default_reference = '' default_reference = ''
state_type = () state_type = ()
# Declarative security # Declarative security
security = ClassSecurityInfo() security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation) security.declareObjectProtected(Permissions.AccessContentsInformation)
...@@ -103,39 +118,32 @@ class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix): ...@@ -103,39 +118,32 @@ class State(IdAsReferenceMixin("state_", "prefix"), XMLObject, XMLMatrix):
'current_inventory', '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, # we want to get the permission or role from its index,
# so we want the retrieve the key of the dict which is like: # so we want the retrieve the key of the dict which is like:
# self.index[cell_info['prefix']][index] = {'Some Role Or Permission': 1} # self.index[cell_prefix][index] = {'Some Role Or Permission': 1,
# 'Some Other One': 0, ...}
for key, value in self.index[cell_prefix][index].items(): 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: if cell_permission_or_role_index == value:
return key return key
return None raise ValueError('No key found for value %s.' % value)
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
)
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. Creates a new content as a matrix box cell.
""" """
cell = self.newContent(id=cell_id, temp_object=True, **kw) cell_permission = self._getCellPermission(cell)
cell_info = cell.getCellInfo() cell_role = self._getCellRole(cell)
cell_permission = self.getPermissionFromInfo(cell_info) cell.selected = cell_role in self.state_permission_roles[cell_permission]
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
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