<?xml version="1.0"?>
<ZopeData>
  <record id="1" aka="AAAAAAAAAAE=">
    <pickle>
      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
    </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>from DateTime import DateTime\n
from Products.DCWorkflow.DCWorkflow import ValidationFailed\n
from zExceptions import Unauthorized\n
\n
def markHistory(document, comment):\n
  document.portal_workflow.doActionFor(document, action=\'edit_action\', comment=comment)\n
\n
def assignComputerPartition(order):\n
  portal = context.getPortalObject()\n
\n
  setup_service_relative_url = order.portal_preferences.getPreferredInstanceSetupResource()\n
  isTransitionPossible = portal.portal_workflow.isTransitionPossible\n
\n
  instance_portal_type_list = ["Software Instance","Slave Instance"]\n
  for order_line in order.getMovementList():\n
    if order_line.getResource() == setup_service_relative_url:\n
      computer_partition = order_line.getAggregateValue(portal_type="Computer Partition")\n
      if computer_partition is None:\n
        if not order.Delivery_isDestinationSectionAllowedToRequest():\n
          raise Unauthorized(\'Request disallowed\')\n
        software_release = order_line.getAggregateValue(portal_type="Software Release")\n
        software_instance = order_line.getAggregateValue(portal_type=instance_portal_type_list)\n
\n
        hosting_subscription = order_line.getAggregateValue(portal_type="Hosting Subscription")\n
        open_order_line = portal.portal_catalog.getResultValue(\n
          portal_type="Open Sale Order Line",\n
          validation_state="validated",\n
          aggregate_relative_url=hosting_subscription.getRelativeUrl(),\n
          destination_section_relative_url=order.getDestinationSection())\n
        if open_order_line is not None:\n
          open_order = open_order_line.getParentValue()\n
          computer_partition_relative_url = open_order.OpenSaleOrder_restrictMethodAsShadowUser(\n
            open_order=open_order,\n
            callable_object=open_order.OpenSaleOrder_findPartition,\n
            argument_list=[software_release.getUrlString(), software_instance.getSourceReference(),\n
            software_instance.getPortalType(), software_instance.getSlaXmlAsDict()])\n
          order_line.edit(\n
            aggregate_list=order_line.getAggregateList()+[computer_partition_relative_url])\n
          return (software_instance, computer_partition_relative_url)\n
        else:\n
          markHistory(order, \'Not confirmed: no open order line found.\')\n
  return (None, None)\n
\n
sale_order = context.getParentValue()\n
if sale_order.getSimulationState() == \'ordered\':\n
  try:\n
    software_instance, computer_partition = assignComputerPartition(sale_order)\n
  except ValueError:\n
    # It was not possible to find free Computer Partition\n
    markHistory(sale_order, \'Not confirmed: no free Computer Partition\')\n
  except Unauthorized, e:\n
    # user has bad balance\n
    markHistory(sale_order, \'Not confirmed: %s\' % e)\n
  else:\n
    if computer_partition is not None:\n
      try:\n
        sale_order.Base_checkConsistency()\n
      except ValidationFailed:\n
        # order not ready yet\n
        markHistory(sale_order, \'Not confirmed: consistency failed\')\n
      else:\n
        sale_order.setStopDate(DateTime())\n
        sale_order.confirm()\n
        software_instance.allocatePartition(computer_partition=computer_partition)\n
</string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string></string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>SaleOrderLine_tryToAllocatePartition</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
