<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <tuple>
        <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
        <tuple/>
      </tuple>
    </pickle>
    <pickle>
      <dictionary>
        <item>
            <key> <string>Script_magic</string> </key>
            <value> <int>3</int> </value>
        </item>
        <item>
            <key> <string>_bind_names</string> </key>
            <value>
              <object>
                <klass>
                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
                </klass>
                <tuple/>
                <state>
                  <dictionary>
                    <item>
                        <key> <string>_asgns</string> </key>
                        <value>
                          <dictionary>
                            <item>
                                <key> <string>name_container</string> </key>
                                <value> <string>container</string> </value>
                            </item>
                            <item>
                                <key> <string>name_context</string> </key>
                                <value> <string>context</string> </value>
                            </item>
                            <item>
                                <key> <string>name_m_self</string> </key>
                                <value> <string>script</string> </value>
                            </item>
                            <item>
                                <key> <string>name_subpath</string> </key>
                                <value> <string>traverse_subpath</string> </value>
                            </item>
                          </dictionary>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>_body</string> </key>
            <value> <string encoding="cdata"><![CDATA[

"""\n
Generic method called when submitting a form in dialog mode.\n
Responsible for validating form data and redirecting to the form action.\n
"""\n
from Products.ERP5Type.Log import log\n
\n
# XXX We should not use meta_type properly,\n
# XXX We need to discuss this problem.(yusei)\n
def isListBox(field):\n
  if field.meta_type==\'ListBox\':\n
    return True\n
  elif field.meta_type==\'ProxyField\':\n
    template_field = field.getRecursiveTemplateField()\n
    if template_field.meta_type==\'ListBox\':\n
      return True\n
  return False\n
\n
from Products.Formulator.Errors import FormValidationError\n
from ZTUtils import make_query\n
\n
request = container.REQUEST\n
request_form = request.form\n
error_message = \'\'\n
\n
# Make this script work alike wether called from another script or by a request\n
kw.update(request_form)\n
\n
# Exceptions for UI\n
if dialog_method == \'Base_configureUI\':\n
  return context.Base_configureUI(form_id=kw[\'form_id\'],\n
                                  selection_name=kw[\'selection_name\'],\n
                                  field_columns=kw[\'field_columns\'],\n
                                  stat_columns=kw[\'stat_columns\'])\n
# Exceptions for Sort\n
if dialog_method == \'Base_configureSortOn\':\n
  return context.Base_configureSortOn(form_id=kw[\'form_id\'],\n
                                      selection_name=kw[\'selection_name\'],\n
                                      field_sort_on=kw[\'field_sort_on\'],\n
                                      field_sort_order=kw[\'field_sort_order\'])\n
# Exceptions for Base_edit\n
# if dialog_method == \'Base_edit\':\n
#   return context.Base_edit(form_id=kw[\'form_id\'],\n
#                            dialog_id=dialog_id,\n
#                            selection_name=kw[\'selection_name\'])\n
# Exceptions for Workflow\n
if dialog_method == \'Workflow_statusModify\':\n
  value = context.Workflow_statusModify(form_id=kw[\'form_id\'],\n
                                        dialog_id=dialog_id)\n
  # XXX: This test is related to erp5_web and should not be present in configuration where it is not installed.\n
  #if not(getattr(context.REQUEST, \'ignore_layout\', 0)) and context.getApplicableLayout() :\n
  #  context.REQUEST.RESPONSE.redirect(context.WebSite_getDocumentPhysicalPath())\n
  return value\n
# Exception for edit relation\n
if dialog_method == \'Base_editRelation\':\n
  return context.Base_editRelation(form_id=kw[\'form_id\'],\n
                                   field_id=kw[\'field_id\'],\n
                                   selection_name=kw[\'list_selection_name\'],\n
                                   selection_index=kw[\'selection_index\'],\n
                                   uids=kw.get(\'uids\', ()),\n
                                   listbox_uid=kw.get(\'listbox_uid\', None),\n
                                   saved_form_data=kw[\'saved_form_data\'])\n
# Exception for create relation\n
if dialog_method == \'Base_createRelation\':\n
  return context.Base_createRelation(form_id=kw[\'form_id\'],\n
                                     selection_name=kw[\'list_selection_name\'],\n
                                     selection_index=kw[\'selection_index\'],\n
                                     base_category=kw[\'base_category\'],\n
                                     object_uid=kw[\'object_uid\'],\n
                                     catalog_index=kw[\'catalog_index\'],\n
                                     default_module=kw[\'default_module\'],\n
                                     dialog_id=dialog_id,\n
                                     portal_type=kw[\'portal_type\'],\n
                                     return_url=kw[\'cancel_url\'])\n
# Exception for folder delete\n
if dialog_method == \'Folder_delete\':\n
  return context.Folder_delete(form_id=kw[\'form_id\'],\n
                               selection_name=kw[\'selection_name\'],\n
                               uids=kw[\'listbox_uid\'],\n
                               md5_object_uid_list=kw[\'md5_object_uid_list\'])\n
\n
\n
form = getattr(context, dialog_id)\n
\n
# form can be a python script that returns the form\n
if not hasattr(form, \'validate_all_to_request\'):\n
  form = form()\n
\n
# Validate the form\n
try:\n
  # It is necessary to force editable_mode before validating\n
  # data. Otherwise, field appears as non editable.\n
  # This is the pending of form_dialog.\n
  editable_mode = request.get(\'editable_mode\', 1)\n
  request.set(\'editable_mode\', 1)\n
  form.validate_all_to_request(request)\n
  request.set(\'editable_mode\', editable_mode)\n
except FormValidationError, validation_errors:\n
  # Pack errors into the request\n
  field_errors = form.ErrorFields(validation_errors)\n
  request.set(\'field_errors\', field_errors)\n
  return form(request)\n
\n
# Use REQUEST.redirect if possible. It will not be possible if at least one of these is true :\n
#  * we got an import_file,\n
#  * we got a listbox\n
#  * a value is None or [] or (), because this is not supported by make_query\n
can_redirect = 1\n
MARKER = [] # A recognisable default value. Use with \'is\', not \'==\'.\n
listbox_id_list = [] # There should not be more than one listbox - but this give us a way to check.\n
file_id_list = [] # For uploaded files.\n
for field in form.get_fields():\n
  k = field.id\n
  v = request.get(k, MARKER)\n
  if v is not MARKER:\n
    if isListBox(field):\n
      listbox_id_list.append(k)\n
    elif can_redirect and (v in (None, [], ()) or hasattr(v, \'read\')) : # If we cannot redirect, useless to test it again\n
      can_redirect = 0\n
\n
    # Cleanup my_ and your_ prefixes\n
    splitted = k.split(\'_\', 1)\n
    if len(splitted) == 2 and splitted[0] in (\'my\', \'your\'):\n
\n
      if hasattr(v, \'as_dict\'):\n
        # This is an encapsulated editor\n
        # convert it\n
        kw.update(v.as_dict())\n
      else:\n
        kw[splitted[1]] = request_form[splitted[1]] = v\n
\n
    else:\n
      kw[k] = request_form[k] = v\n
\n
if len(listbox_id_list):\n
  can_redirect = 0\n
  # Warn if there are more than one listbox in form ...\n
  if len(listbox_id_list) > 1:\n
    log(\'Base_callDialogMethod\', \'There are %s listboxes in form %s.\' % (len(listbox_id_list), form.id))\n
  # ... but handle them anyway.\n
  for listbox_id in listbox_id_list:\n
    listbox_line_list = []\n
    listbox = kw[listbox_id]\n
    listbox_keys = listbox.keys()\n
    listbox_keys.sort()\n
    for key in listbox_keys:\n
      listbox_line = listbox[key]\n
      listbox_line[\'listbox_key\'] = key\n
      listbox_line_list.append(listbox_line)\n
    listbox_line_list = tuple(listbox_line_list)\n
    kw[listbox_id] = request_form[listbox_id] = listbox_line_list\n
\n
\n
# Check if the selection changed\n
if hasattr(kw, \'previous_md5_object_uid_list\'):\n
  selection_list = context.portal_selections.callSelectionFor(kw[\'list_selection_name\'], context=context)\n
  if selection_list is not None:\n
    object_uid_list = map(lambda x:x.getObject().getUid(), selection_list)\n
    error = context.portal_selections.selectionHasChanged(kw[\'previous_md5_object_uid_list\'], object_uid_list)\n
    if error:\n
      error_message = context.Base_translateString("Sorry, your selection has changed.")\n
\n
# if dialog_category is object_search, then edit the selection\n
if dialog_category == "object_search" :\n
  context.portal_selections.setSelectionParamsFor(kw[\'selection_name\'], kw)\n
\n
# if we have checked line in listbox, modify the selection\n
listbox_uid = kw.get(\'listbox_uid\', None)\n
# In some cases, the listbox exists, is editable, but the selection name\n
# has no meaning, for example fast input dialogs.\n
# In such cases, we must not try to update a non-existing selection.\n
if listbox_uid is not None and kw.has_key(\'list_selection_name\'):\n
  uids = kw.get(\'uids\')\n
  selected_uids = context.portal_selections.updateSelectionCheckedUidList(\n
    kw[\'list_selection_name\'],\n
    listbox_uid, uids)\n
# Remove values which doesn\'t work with make_query.\n
clean_kw = {}\n
for k, v in kw.items() :\n
  if v not in (None, [], ()) :\n
    clean_kw[k] = kw[k]\n
\n
# Handle deferred style\n
if clean_kw.get(\'deferred_style\', 0):\n
  clean_kw[\'deferred_portal_skin\'] = clean_kw.get(\'portal_skin\', None)\n
  # XXX Hardcoded Deferred style name\n
  clean_kw[\'portal_skin\'] = \'Deferred\'\n
\n
url_params_string = make_query(clean_kw)\n
\n
# XXX: We always redirect in report mode to make sure portal_skin\n
# parameter is taken into account by SkinTool.\n
# If url is too long, we do not redirect to avoid crash.\n
# XXX: 2000 is an arbitrary value resulted from trial and error.\n
if (not(can_redirect) or len(url_params_string) > 2000):\n
  if \'portal_skin\' in clean_kw:\n
    new_skin_name = clean_kw[\'portal_skin\']\n
    context.getPortalObject().portal_skins.changeSkin(new_skin_name)\n
    request.set(\'portal_skin\', new_skin_name)\n
    deferred_portal_skin = clean_kw.get(\'deferred_portal_skin\')\n
    if deferred_portal_skin:\n
      request.set(\'deferred_portal_skin\', deferred_portal_skin)\n
\n
  # If we cannot redirect, then call the form directly.\n
  dialog_form = getattr(context, dialog_method)\n
  # XXX: this is a hack that should not be needed anymore with the new listbox.\n
  # set the URL in request, so that we can immediatly call method\n
  # that depend on it (eg. Show All). This is really related to\n
  # current ListBox implementation which edit Selection\'s last_url\n
  # with the content of REQUEST.URL\n
  request.set(\'URL\', \'%s/%s\' % (context.absolute_url(), dialog_method))\n
  return dialog_form(**kw)\n
\n
if error_message != \'\':\n
  redirect_url = \'%s/%s?%s\' % ( context.absolute_url()\n
                              , dialog_method\n
                              , \'portal_status_message=%s\' % error_message\n
                              )\n
elif url_params_string != \'\':\n
  redirect_url = \'%s/%s?%s\' % ( context.absolute_url()\n
                              , dialog_method\n
                              , url_params_string\n
                              )\n
\n
else:\n
  redirect_url = \'%s/%s\' % ( context.absolute_url()\n
                            , dialog_method\n
                            )\n
\n
return request.RESPONSE.redirect(redirect_url)\n
\n
# vim: syntax=python\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_code</string> </key>
            <value>
              <none/>
            </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>dialog_method, dialog_id, dialog_category=\'\', **kw</string> </value>
        </item>
        <item>
            <key> <string>errors</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
        <item>
            <key> <string>func_code</string> </key>
            <value>
              <object>
                <klass>
                  <global name="FuncCode" module="Shared.DC.Scripts.Signature"/>
                </klass>
                <tuple/>
                <state>
                  <dictionary>
                    <item>
                        <key> <string>co_argcount</string> </key>
                        <value> <int>3</int> </value>
                    </item>
                    <item>
                        <key> <string>co_varnames</string> </key>
                        <value>
                          <tuple>
                            <string>dialog_method</string>
                            <string>dialog_id</string>
                            <string>dialog_category</string>
                            <string>kw</string>
                            <string>Products.ERP5Type.Log</string>
                            <string>log</string>
                            <string>isListBox</string>
                            <string>Products.Formulator.Errors</string>
                            <string>FormValidationError</string>
                            <string>ZTUtils</string>
                            <string>make_query</string>
                            <string>_getattr_</string>
                            <string>container</string>
                            <string>request</string>
                            <string>request_form</string>
                            <string>error_message</string>
                            <string>context</string>
                            <string>_getitem_</string>
                            <string>value</string>
                            <string>None</string>
                            <string>getattr</string>
                            <string>form</string>
                            <string>hasattr</string>
                            <string>editable_mode</string>
                            <string>validation_errors</string>
                            <string>field_errors</string>
                            <string>can_redirect</string>
                            <string>MARKER</string>
                            <string>listbox_id_list</string>
                            <string>file_id_list</string>
                            <string>_getiter_</string>
                            <string>field</string>
                            <string>k</string>
                            <string>v</string>
                            <string>splitted</string>
                            <string>len</string>
                            <string>_write_</string>
                            <string>listbox_id</string>
                            <string>listbox_line_list</string>
                            <string>listbox</string>
                            <string>listbox_keys</string>
                            <string>key</string>
                            <string>listbox_line</string>
                            <string>tuple</string>
                            <string>selection_list</string>
                            <string>map</string>
                            <string>object_uid_list</string>
                            <string>error</string>
                            <string>listbox_uid</string>
                            <string>uids</string>
                            <string>selected_uids</string>
                            <string>clean_kw</string>
                            <string>url_params_string</string>
                            <string>new_skin_name</string>
                            <string>deferred_portal_skin</string>
                            <string>dialog_form</string>
                            <string>_apply_</string>
                            <string>redirect_url</string>
                          </tuple>
                        </value>
                    </item>
                  </dictionary>
                </state>
              </object>
            </value>
        </item>
        <item>
            <key> <string>func_defaults</string> </key>
            <value>
              <tuple>
                <string></string>
              </tuple>
            </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>Base_callDialogMethod</string> </value>
        </item>
        <item>
            <key> <string>warnings</string> </key>
            <value>
              <tuple/>
            </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
