<?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 encoding="cdata"><![CDATA[

from DateTime import DateTime\n
import random\n
\n
software_instance = state_change[\'object\']\n
software_release_url_string = state_change.kwargs[\'software_release\']\n
hosting_subscription_uid = state_change.kwargs[\'hosting_subscription_uid\']\n
shared = state_change.kwargs[\'shared\']\n
software_type = state_change.kwargs["software_type"]\n
tag = state_change.kwargs[\'tag\']\n
filter_kw = state_change.kwargs["filter_kw"]\n
\n
# Assertion: No packing list line should be related to this software instance\n
packing_list_line = software_instance.getAggregateRelatedValue(portal_type=\'Sale Packing List Line\')\n
if packing_list_line is not None:\n
  raise ValueError("Software Instance %s is already associated to a packing list line" % software_instance.getRelativeurl())\n
\n
# Find a free computer partition. This means:\n
# Computer Partition which doesn\'t have non delivered sale packing list related\n
\n
SQL_WINDOW_SIZE = 50\n
\n
computer_partition = None\n
query_kw = {\n
  \'software_release_url\': software_release_url_string,\n
  \'portal_type\': \'Computer Partition\'\n
}\n
# support SLA\n
if "computer_guid" in filter_kw:\n
  query_kw["parent_reference"] = filter_kw["computer_guid"]\n
\n
# fetch at mot 50 random Computer Partitions, and check if they are ok\n
isTransitionPossible = software_instance.getPortalObject().portal_workflow.isTransitionPossible\n
if shared:\n
  query_kw.update(\n
    software_type=software_type,\n
    available_slave=1, # has slave\n
    free_for_request=0, # is not available for normal request\n
  )\n
  result_count = software_instance.portal_catalog.countResults(**query_kw)[0][0]\n
\n
  offset = max(0, result_count-1)\n
  if offset >= SQL_WINDOW_SIZE:\n
    limit = (random.randint(0, offset), SQL_WINDOW_SIZE)\n
  else:\n
    limit = (0, SQL_WINDOW_SIZE)\n
  for computer_partition_candidate in software_instance.portal_catalog(\n
                                           limit=limit, **query_kw):\n
    computer_partition_candidate = computer_partition_candidate.getObject()\n
    # Check if the computer partition can be marked as busy (done by an interaction workflow on sale packing list level)\n
    if computer_partition_candidate.getSlapState() == \'busy\' and computer_partition_candidate.ComputerPartition_isSlaveAvailable():\n
      computer_partition = computer_partition_candidate\n
      break\n
\n
  if computer_partition is None:\n
    raise ValueError(\'It was not possible to find Computer Partition with free slave\')\n
\n
  # find free slave\n
  slave_partition = None\n
  for slave_partition_candidate in computer_partition.contentValues(portal_type=\'Slave Partition\'):\n
    if isTransitionPossible(slave_partition_candidate, \'mark_busy\'):\n
      slave_partition = slave_partition_candidate\n
      slave_partition.serialize()\n
      break\n
\n
  if slave_partition is None:\n
    slave_partition = computer_partition.newContent(portal_type="Slave Partition")\n
    slave_partition.markFree()\n
  computer_partition = slave_partition\n
else:\n
  query_kw.update(free_for_request=1) # is free\n
  result_count = software_instance.portal_catalog.countResults(**query_kw)[0][0]\n
\n
  offset = max(0, result_count-1)\n
  if offset >= SQL_WINDOW_SIZE:\n
    limit = (random.randint(0, offset), SQL_WINDOW_SIZE)\n
  else:\n
    limit = (0, SQL_WINDOW_SIZE)\n
\n
  for computer_partition_candidate in software_instance.portal_catalog(\n
                                           limit=limit, **query_kw):\n
    computer_partition_candidate = computer_partition_candidate.getObject()\n
    # Check if the computer partition can be marked as busy (done by an interaction workflow on sale packing list level)\n
    if isTransitionPossible(computer_partition_candidate, \'mark_busy\'):\n
      computer_partition = computer_partition_candidate\n
      break\n
\n
  if computer_partition is None:\n
    raise ValueError(\'It was not possible to find free Computer Partition\')\n
\n
  # lock computer partition\n
  computer_partition.serialize()\n
\n
software_release_document = context.portal_catalog.getResultValue(\n
  portal_type=\'Software Release\',\n
  url_string=software_release_url_string)\n
\n
# protect computer partition from being selected again\n
portal = context.getPortalObject()\n
\n
hosting_subscription = software_instance.portal_catalog.getResultValue(uid=hosting_subscription_uid)\n
sale_packing_list_line = context.HostingSubscription_getInstancePackingListLine(state_change)\n
base_sale_packing_list = sale_packing_list_line.getParentValue()\n
# XXX: SoftwareInstance_createSalePackingList shall be used to create new Sale Packing List\n
sale_packing_list_module = portal.getDefaultModule(portal_type=\'Sale Packing List\')\n
\n
specialise = base_sale_packing_list.getSpecialise()\n
if not specialise and base_sale_packing_list.getCausalityValue():\n
  specialise = base_sale_packing_list.getCausalityValue().getSpecialise()\n
\n
sale_packing_list = sale_packing_list_module.newContent(\n
  portal_type=\'Sale Packing List\', \n
  start_date=DateTime(),\n
  specialise=specialise,\n
  destination=base_sale_packing_list.getDestination(),\n
  destination_section=base_sale_packing_list.getDestinationSection(),\n
  destination_decision=base_sale_packing_list.getDestinationDecision(),\n
  source=base_sale_packing_list.getSource(),\n
  source_section=base_sale_packing_list.getSourceSection(),\n
  price_currency=base_sale_packing_list.getPriceCurrency())\n
sale_packing_list_line = sale_packing_list.newContent(\n
  portal_type=\'Sale Packing List Line\',\n
  resource=context.portal_preferences.getPreferredInstanceSetupResource(),\n
  aggregate_value_list=[software_instance, software_release_document, computer_partition, hosting_subscription]\n
)\n
\n
# confirm Sale Packing List\n
sale_packing_list.confirm()\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>state_change</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>SlaveInstance_requestComputerPartition</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
