Commit 76b1a106 authored by Jérome Perrin's avatar Jérome Perrin

Base_getCategoriesSpreadSheetMapping performs checks on the spreadsheet, and...

Base_getCategoriesSpreadSheetMapping performs checks on the spreadsheet, and can be provided a callback function to report errors to the user.
CategoryTool_importCategoryFile uses this feature to redirect with a portal status message instead of raising an error.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@24908 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 0f795c57
...@@ -57,7 +57,12 @@ ...@@ -57,7 +57,12 @@
"""Parses a spreadsheet containing categories and returns a mapping.\n """Parses a spreadsheet containing categories and returns a mapping.\n
\n \n
import_file must be a spreadsheet in a format supported by openoffice\n `import_file` must be a spreadsheet in a format supported by openoffice\n
\n
`invalid_spreadsheet_error_handler` is the callback method that will be called if\n
the spreadsheet is invalid. The method must recieve as only parameter a string\n
explaining the error.\n
If no error_callback is given, the default action is to raise a ValueError.\n
\n \n
The returned mapping has the following structure:\n The returned mapping has the following structure:\n
\n \n
...@@ -73,10 +78,18 @@ The returned mapping has the following structure:\n ...@@ -73,10 +78,18 @@ The returned mapping has the following structure:\n
This scripts guarantees that the list of category info is sorted in such a\n This scripts guarantees that the list of category info is sorted in such a\n
way that parent always precedes their children.\n way that parent always precedes their children.\n
"""\n """\n
from Products.ERP5Type.Message import translateString\n
from Products.ERP5OOo.OOoUtils import OOoParser\n from Products.ERP5OOo.OOoUtils import OOoParser\n
parser = OOoParser()\n parser = OOoParser()\n
categories_spreadsheet_mapping = dict()\n categories_spreadsheet_mapping = dict()\n
\n \n
\n
def default_invalid_spreadsheet_error_handler(error_message):\n
raise ValueError(error_message)\n
\n
if invalid_spreadsheet_error_handler is None:\n
invalid_spreadsheet_error_handler = default_invalid_spreadsheet_error_handler\n
\n
def getIDFromString(string=None):\n def getIDFromString(string=None):\n
"""\n """\n
This function transform a string to a safe and beautiful ID.\n This function transform a string to a safe and beautiful ID.\n
...@@ -249,12 +262,13 @@ for table_name in spreadsheets.keys():\n ...@@ -249,12 +262,13 @@ for table_name in spreadsheets.keys():\n
category_properties[\'title\'] = clean_title\n category_properties[\'title\'] = clean_title\n
\n \n
# Detect duplicate IDs\n # Detect duplicate IDs\n
#if path_elements and path_elements[-1][\'depth\'] == element_depth:\n
for element in path_elements[::-1]:\n for element in path_elements[::-1]:\n
if element[\'depth\'] != element_depth:\n if element[\'depth\'] != element_depth:\n
break\n break\n
if element[\'value\'] == path_element_id:\n if element[\'value\'] == path_element_id:\n
raise ValueError("Duplicate id %s" % element[\'value\'])\n invalid_spreadsheet_error_handler(\n
translateString("Duplicate id found: ${id}",\n
mapping=dict(id=element[\'value\'])))\n
\n \n
# Save the path element\n # Save the path element\n
path_elements.append({ \'depth\': element_depth\n path_elements.append({ \'depth\': element_depth\n
...@@ -280,7 +294,7 @@ return categories_spreadsheet_mapping\n ...@@ -280,7 +294,7 @@ return categories_spreadsheet_mapping\n
</item> </item>
<item> <item>
<key> <string>_params</string> </key> <key> <string>_params</string> </key>
<value> <string>import_file</string> </value> <value> <string>import_file, invalid_spreadsheet_error_handler=None</string> </value>
</item> </item>
<item> <item>
<key> <string>errors</string> </key> <key> <string>errors</string> </key>
...@@ -300,18 +314,22 @@ return categories_spreadsheet_mapping\n ...@@ -300,18 +314,22 @@ return categories_spreadsheet_mapping\n
<dictionary> <dictionary>
<item> <item>
<key> <string>co_argcount</string> </key> <key> <string>co_argcount</string> </key>
<value> <int>1</int> </value> <value> <int>2</int> </value>
</item> </item>
<item> <item>
<key> <string>co_varnames</string> </key> <key> <string>co_varnames</string> </key>
<value> <value>
<tuple> <tuple>
<string>import_file</string> <string>import_file</string>
<string>invalid_spreadsheet_error_handler</string>
<string>Products.ERP5Type.Message</string>
<string>translateString</string>
<string>Products.ERP5OOo.OOoUtils</string> <string>Products.ERP5OOo.OOoUtils</string>
<string>OOoParser</string> <string>OOoParser</string>
<string>parser</string> <string>parser</string>
<string>dict</string> <string>dict</string>
<string>categories_spreadsheet_mapping</string> <string>categories_spreadsheet_mapping</string>
<string>default_invalid_spreadsheet_error_handler</string>
<string>None</string> <string>None</string>
<string>getIDFromString</string> <string>getIDFromString</string>
<string>content_type</string> <string>content_type</string>
...@@ -363,7 +381,6 @@ return categories_spreadsheet_mapping\n ...@@ -363,7 +381,6 @@ return categories_spreadsheet_mapping\n
<string>element</string> <string>element</string>
<string>path</string> <string>path</string>
<string>clean_title</string> <string>clean_title</string>
<string>ValueError</string>
</tuple> </tuple>
</value> </value>
</item> </item>
...@@ -375,7 +392,9 @@ return categories_spreadsheet_mapping\n ...@@ -375,7 +392,9 @@ return categories_spreadsheet_mapping\n
<item> <item>
<key> <string>func_defaults</string> </key> <key> <string>func_defaults</string> </key>
<value> <value>
<tuple>
<none/> <none/>
</tuple>
</value> </value>
</item> </item>
<item> <item>
......
...@@ -95,7 +95,17 @@ deleted_category_counter = 0\n ...@@ -95,7 +95,17 @@ deleted_category_counter = 0\n
kept_category_counter = 0\n kept_category_counter = 0\n
\n \n
filename = getattr(import_file, \'filename\', \'?\')\n filename = getattr(import_file, \'filename\', \'?\')\n
categories_spreadsheet_mapping = context.Base_getCategoriesSpreadSheetMapping(import_file)\n \n
def invalid_category_spreadsheet_handler(message):\n
# action taken when an invalid spreadsheet is provided.\n
# we *raise* a Redirect, because we don\'t want the transaction to succeed\n
raise \'Redirect\', \'%s/view?portal_status_message=%s\' % (\n
context.portal_categories.absolute_url(),\n
message)\n
\n
categories_spreadsheet_mapping = context.Base_getCategoriesSpreadSheetMapping(import_file,\n
invalid_spreadsheet_error_handler=invalid_category_spreadsheet_handler)\n
\n
\n \n
for base_category, categories in \\\n for base_category, categories in \\\n
categories_spreadsheet_mapping.items():\n categories_spreadsheet_mapping.items():\n
...@@ -269,6 +279,7 @@ return context.REQUEST.RESPONSE.redirect(\n ...@@ -269,6 +279,7 @@ return context.REQUEST.RESPONSE.redirect(\n
<string>kept_category_counter</string> <string>kept_category_counter</string>
<string>getattr</string> <string>getattr</string>
<string>filename</string> <string>filename</string>
<string>invalid_category_spreadsheet_handler</string>
<string>categories_spreadsheet_mapping</string> <string>categories_spreadsheet_mapping</string>
<string>_getiter_</string> <string>_getiter_</string>
<string>base_category</string> <string>base_category</string>
......
1042 1043
\ No newline at end of file \ 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