Commit b25c7e42 authored by Jérome Perrin's avatar Jérome Perrin

Make business template portable, from Klaus Wölfel



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@15717 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d02a534b
This diff is collapsed.
...@@ -50,6 +50,7 @@ import re ...@@ -50,6 +50,7 @@ import re
from xml.dom.minidom import parse from xml.dom.minidom import parse
import struct import struct
import cPickle import cPickle
import posixpath
try: try:
from base64 import b64encode, b64decode from base64 import b64encode, b64decode
except ImportError: except ImportError:
...@@ -57,6 +58,10 @@ except ImportError: ...@@ -57,6 +58,10 @@ except ImportError:
from Products.ERP5Type.Message import Message from Products.ERP5Type.Message import Message
N_ = lambda msgid, **kw: Message('ui', msgid, **kw) N_ = lambda msgid, **kw: Message('ui', msgid, **kw)
WIN = False
if os.name == 'nt':
WIN = True
class BusinessTemplateUnknownError(Exception): class BusinessTemplateUnknownError(Exception):
""" Exception raised when the business template """ Exception raised when the business template
is impossible to find in the repositories is impossible to find in the repositories
...@@ -250,7 +255,7 @@ class TemplateTool (BaseTool): ...@@ -250,7 +255,7 @@ class TemplateTool (BaseTool):
""" """
Import template from a temp file (as uploaded by the user) Import template from a temp file (as uploaded by the user)
""" """
file = open(path, 'r') file = open(path, 'rb')
try: try:
# read magic key to determine wich kind of bt we use # read magic key to determine wich kind of bt we use
file.seek(0) file.seek(0)
...@@ -274,7 +279,7 @@ class TemplateTool (BaseTool): ...@@ -274,7 +279,7 @@ class TemplateTool (BaseTool):
for prop in bt.propertyMap(): for prop in bt.propertyMap():
prop_type = prop['type'] prop_type = prop['type']
pid = prop['id'] pid = prop['id']
prop_path = os.path.join(tar.members[0].name, 'bt', pid) prop_path = posixpath.join(tar.members[0].name, 'bt', pid)
try: try:
info = tar.getmember(prop_path) info = tar.getmember(prop_path)
except KeyError: except KeyError:
...@@ -284,11 +289,11 @@ class TemplateTool (BaseTool): ...@@ -284,11 +289,11 @@ class TemplateTool (BaseTool):
or prop_type == 'int': or prop_type == 'int':
prop_dict[pid] = value prop_dict[pid] = value
elif prop_type == 'lines' or prop_type == 'tokens': elif prop_type == 'lines' or prop_type == 'tokens':
prop_dict[pid[:-5]] = value.split(str(os.linesep)) prop_dict[pid[:-5]] = value.splitlines()
prop_dict.pop('id', '') prop_dict.pop('id', '')
bt.edit(**prop_dict) bt.edit(**prop_dict)
# import all other files from bt # import all other files from bt
fobj = open(path, 'r') fobj = open(path, 'rb')
try: try:
bt.importFile(file=fobj) bt.importFile(file=fobj)
finally: finally:
...@@ -326,8 +331,16 @@ class TemplateTool (BaseTool): ...@@ -326,8 +331,16 @@ class TemplateTool (BaseTool):
id = self.generateNewId() id = self.generateNewId()
urltype, name = splittype(url) urltype, name = splittype(url)
if os.path.isdir(name): # new version of business template in plain # Windows compatibility
# format (folder) if WIN:
if os.path.isdir(os.path.normpath(url)):
name = os.path.normpath(url)
elif os.path.isfile(os.path.normpath(url)):
url = 'file:///%s' %os.path.normpath(url)
# new version of business template in plain format (folder)
if os.path.isdir(os.path.normpath(name)):
name = os.path.normpath(name)
file_list = [] file_list = []
def callback(arg, directory, files): def callback(arg, directory, files):
if 'CVS' not in directory and '.svn' not in directory: # XXX: if 'CVS' not in directory and '.svn' not in directory: # XXX:
...@@ -350,11 +363,11 @@ class TemplateTool (BaseTool): ...@@ -350,11 +363,11 @@ class TemplateTool (BaseTool):
prop_path = os.path.join('.', bt_path, pid) prop_path = os.path.join('.', bt_path, pid)
if not os.path.exists(prop_path): if not os.path.exists(prop_path):
continue continue
value = open(prop_path, 'r').read() value = open(prop_path, 'rb').read()
if prop_type in ('text', 'string', 'int', 'boolean'): if prop_type in ('text', 'string', 'int', 'boolean'):
prop_dict[pid] = value prop_dict[pid] = value
elif prop_type in ('lines', 'tokens'): elif prop_type in ('lines', 'tokens'):
prop_dict[pid[:-5]] = value.split(str(os.linesep)) prop_dict[pid[:-5]] = value.splitlines()
prop_dict.pop('id', '') prop_dict.pop('id', '')
bt.edit(**prop_dict) bt.edit(**prop_dict)
# import all others objects # import all others objects
...@@ -397,7 +410,7 @@ class TemplateTool (BaseTool): ...@@ -397,7 +410,7 @@ class TemplateTool (BaseTool):
tempid, temppath = mkstemp() tempid, temppath = mkstemp()
try: try:
os.close(tempid) # Close the opened fd as soon as possible os.close(tempid) # Close the opened fd as soon as possible
tempfile = open(temppath, 'w') tempfile = open(temppath, 'wb')
try: try:
tempfile.write(import_file.read()) tempfile.write(import_file.read())
finally: finally:
......
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