Commit c429e875 authored by Yoshinori Okuji's avatar Yoshinori Okuji

Clean up ModuleTemplateItem.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@30710 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent c2655244
...@@ -2838,32 +2838,29 @@ class ModuleTemplateItem(BaseTemplateItem): ...@@ -2838,32 +2838,29 @@ class ModuleTemplateItem(BaseTemplateItem):
def build(self, context, **kw): def build(self, context, **kw):
BaseTemplateItem.build(self, context, **kw) BaseTemplateItem.build(self, context, **kw)
p = context.getPortalObject() p = context.getPortalObject()
for id in self._archive.keys(): for module_id in self._archive.keys():
module = p.unrestrictedTraverse(id) module = p.unrestrictedTraverse(module_id)
dict = {} mapping = {}
dict['id'] = module.getId() mapping['id'] = module.getId()
dict['title'] = module.getTitle() mapping['title'] = module.getTitle()
dict['portal_type'] = module.getPortalType() mapping['portal_type'] = module.getPortalType()
permission_list = [] permission_list = []
# use show permission mapping['permission_list'] = module.showPermissions()
dict['permission_list'] = module.showPermissions() self._objects[module_id] = mapping
self._objects[id] = dict
# Function to generate XML Code Manually # Function to generate XML Code Manually
def generateXml(self, path=None): def generateXml(self, path=None):
dict = self._objects[path] dict = self._objects[path]
xml_data = '<module>' xml_data = ['<module>']
# sort key
keys = dict.keys() keys = dict.keys()
keys.sort() for key in sorted(keys):
for key in keys: if key == 'permission_list':
if key =='permission_list':
# separe permission dict into xml # separe permission dict into xml
xml_data += '\n <%s>' %(key,) xml_data.append(' <%s>' % (key, ))
permission_list = dict[key] permission_list = dict[key]
for perm in permission_list: for perm in permission_list:
# the type of the permission defined if we use acquired or not # the type of the permission defined if we use acquired or not
if type(perm[1]) == type([]): if isinstance(perm[1], list):
ptype = "list" ptype = "list"
else: else:
ptype = "tuple" ptype = "tuple"
...@@ -2871,32 +2868,32 @@ class ModuleTemplateItem(BaseTemplateItem): ...@@ -2871,32 +2868,32 @@ class ModuleTemplateItem(BaseTemplateItem):
# Skip if permission is not configured (i.e. no role at all # Skip if permission is not configured (i.e. no role at all
# with acquire permission, or Manager only without acquire # with acquire permission, or Manager only without acquire
# permission). # permission).
if (not len(role_list) and ptype == 'list') or \ if (len(role_list) == 0 and ptype == 'list') or \
(role_list == ['Manager'] and ptype == 'tuple'): (role_list == ['Manager'] and ptype == 'tuple'):
continue continue
role_list.sort() role_list.sort()
xml_data += "\n <permission type='%s'>" %(ptype,) xml_data.append(" <permission type='%s'>" % (ptype, ))
xml_data += '\n <name>%s</name>' %(perm[0]) xml_data.append(' <name>%s</name>' % (perm[0], ))
for role in role_list: for role in role_list:
xml_data += '\n <role>%s</role>' %(role) xml_data.append(' <role>%s</role>' % (role, ))
xml_data += '\n </permission>' xml_data.append(' </permission>')
xml_data += '\n </%s>' %(key,) xml_data.append(' </%s>' % (key, ))
else: else:
xml_data += '\n <%s>%s</%s>' %(key, dict[key], key) xml_data.append(' <%s>%s</%s>' % (key, dict[key], key))
xml_data += '\n</module>' xml_data.append('</module>')
return xml_data return '\n'.join(xml_data)
def export(self, context, bta, **kw): def export(self, context, bta, **kw):
if len(self._objects.keys()) == 0: if len(self._objects) == 0:
return return
path = os.path.join(bta.path, self.__class__.__name__) path = os.path.join(bta.path, self.__class__.__name__)
bta.addFolder(path) bta.addFolder(path)
keys = self._objects.keys() keys = self._objects.keys()
keys.sort() keys.sort()
for id in keys: for key in keys:
# export modules one by one # export modules one by one
xml_data = self.generateXml(path=id) xml_data = self.generateXml(path=key)
bta.addObject(obj=xml_data, name=id, path=path) bta.addObject(obj=xml_data, name=key, path=path)
def install(self, context, trashbin, **kw): def install(self, context, trashbin, **kw):
portal = context.getPortalObject() portal = context.getPortalObject()
...@@ -2909,35 +2906,36 @@ class ModuleTemplateItem(BaseTemplateItem): ...@@ -2909,35 +2906,36 @@ class ModuleTemplateItem(BaseTemplateItem):
valid_permissions = dict.fromkeys([x[0] for x in valid_permissions = dict.fromkeys([x[0] for x in
context.ac_inherited_permissions(all=1)]) context.ac_inherited_permissions(all=1)])
for id in items.keys(): for path, mapping in items.iteritems():
if update_dict.has_key(id) or force: if update_dict.has_key(path) or force:
if not force: if not force:
action = update_dict[id] action = update_dict[path]
if action == 'nothing': if action == 'nothing':
continue continue
mapping = items[id] path, module_id = posixpath.split(path)
path, id = posixpath.split(id) portal_type = str(mapping['portal_type'])
if id in portal.objectIds(): module = portal._getOb(module_id)
module = portal._getOb(id) if module is not None:
module.portal_type = str(mapping['portal_type']) module.portal_type = portal_type
else: else:
module = portal.newContent(id=id, portal_type=str(mapping['portal_type'])) module = portal.newContent(id=module_id, portal_type=portal_type)
module.setTitle(str(mapping['title'])) module.setTitle(str(mapping['title']))
for name in valid_permissions.keys(): permission_dict = dict(mapping['permission_list'])
for name in valid_permissions.iterkeys():
# By default, Manager only without acquire permission # By default, Manager only without acquire permission
role_list = dict(mapping['permission_list']).get(name, ('Manager',)) role_list = permission_dict.get(name, ('Manager',))
acquire = (type(role_list) == type([])) acquire = isinstance(role_list, list)
module.manage_permission(name, roles=role_list, acquire=acquire) module.manage_permission(name, roles=role_list, acquire=acquire)
def _importFile(self, file_name, file): def _importFile(self, file_name, file):
if not file_name.endswith('.xml'): if not file_name.endswith('.xml'):
LOG('Business Template', 0, 'Skipping file "%s"' % (file_name, )) LOG('Business Template', 0, 'Skipping file "%s"' % (file_name, ))
return return
dict = {} mapping = {}
xml = parse(file) xml = parse(file)
for id in ('portal_type', 'id', 'title', 'permission_list'): for key in ('portal_type', 'id', 'title', 'permission_list'):
elt = xml.getElementsByTagName(id)[0] elt = xml.getElementsByTagName(key)[0]
if id == 'permission_list': if key == 'permission_list':
plist = [] plist = []
perm_list = elt.getElementsByTagName('permission') perm_list = elt.getElementsByTagName('permission')
for perm in perm_list: for perm in perm_list:
...@@ -2951,20 +2949,20 @@ class ModuleTemplateItem(BaseTemplateItem): ...@@ -2951,20 +2949,20 @@ class ModuleTemplateItem(BaseTemplateItem):
role_node = role.childNodes[0] role_node = role.childNodes[0]
role = role_node.data role = role_node.data
rlist.append(str(role)) rlist.append(str(role))
if perm_type == "list" or perm_type is None: if perm_type == 'list' or perm_type is None:
perm_tuple = (str(name), list(rlist)) perm_tuple = (str(name), list(rlist))
else: else:
perm_tuple = (str(name), tuple(rlist)) perm_tuple = (str(name), tuple(rlist))
plist.append(perm_tuple) plist.append(perm_tuple)
dict[id] = plist mapping[key] = plist
else: else:
node_list = elt.childNodes node_list = elt.childNodes
if len(node_list) == 0: if len(node_list) == 0:
value='' value = ''
else: else:
value = node_list[0].data value = node_list[0].data
dict[id] = str(value) mapping[key] = str(value)
self._objects[file_name[:-4]] = dict self._objects[file_name[:-4]] = mapping
def uninstall(self, context, **kw): def uninstall(self, context, **kw):
trash = kw.get('trash', 0) trash = kw.get('trash', 0)
...@@ -2978,13 +2976,13 @@ class ModuleTemplateItem(BaseTemplateItem): ...@@ -2978,13 +2976,13 @@ class ModuleTemplateItem(BaseTemplateItem):
keys = [object_path] keys = [object_path]
p = context.getPortalObject() p = context.getPortalObject()
id_list = p.objectIds() id_list = p.objectIds()
for id in keys: for key in keys:
if id in id_list: if key in id_list:
try: try:
if trash and trashbin is not None: if trash and trashbin is not None:
container_path = id.split('/') container_path = key.split('/')
self.portal_trash.backupObject(trashbin, container_path, id, save=1, keep_subobjects=1) self.portal_trash.backupObject(trashbin, container_path, key, save=1, keep_subobjects=1)
p.manage_delObjects([id]) p.manage_delObjects([key])
except NotFound: except NotFound:
pass pass
BaseTemplateItem.uninstall(self, context, **kw) BaseTemplateItem.uninstall(self, context, **kw)
......
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