<?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[

import random\n
open_sale_order = context\n
\n
computer_partition = None\n
query_kw = {\n
  \'software_release_url\': software_release_url,\n
  \'portal_type\': \'Computer Partition\',\n
}\n
if software_instance_portal_type == "Slave Instance":\n
  query_kw[\'free_for_request\'] = 0\n
  query_kw[\'software_type\'] = software_type\n
elif software_instance_portal_type == "Software Instance":\n
  query_kw[\'free_for_request\'] = 1\n
else:\n
  raise NotImplementedError, "Unknown portal type %s" % \\\n
     software_instance_portal_type \n
# support SLA\n
if "computer_guid" in filter_kw:\n
  query_kw["parent_reference"] = filter_kw["computer_guid"]\n
if "instance_guid" in filter_kw:\n
  portal = context.getPortalObject()\n
  instance = portal.portal_catalog.getResultValue(portal_type="Software Instance", reference=filter_kw["instance_guid"])\n
  if instance is None:\n
    # XXX Other user do not have access to the instance document...\n
    if filter_kw["instance_guid"] == "SOFTINST-9238":\n
      # XXX Let\'s be nice with Cedric and Vincent\n
      query_kw["uid"] = "2225067"\n
    elif filter_kw["instance_guid"] == "SOFTINST-11031":\n
      # XXX Let\'s be nice with KVM frontend\n
      query_kw["uid"] = "2874296"\n
    else:\n
      query_kw["uid"] = "-1"\n
  else:\n
    partition_uid = instance.getAggregateUid(portal_type="Computer Partition")\n
    if partition_uid:\n
      query_kw["uid"] = partition_uid\n
    else:\n
      query_kw["uid"] = "-1"\n
\n
SQL_WINDOW_SIZE = 50\n
\n
# fetch at mot 50 random Computer Partitions, and check if they are ok\n
isTransitionPossible = open_sale_order.getPortalObject().portal_workflow.isTransitionPossible\n
result_count = open_sale_order.portal_catalog.countResults(**query_kw)[0][0]\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 context.portal_catalog(\n
                                         limit=limit, **query_kw):\n
  computer_partition_candidate = computer_partition_candidate.getObject()    \n
  if software_instance_portal_type == "Software Instance":\n
    # Check if the computer partition can be marked as busy\n
    if isTransitionPossible(computer_partition_candidate, \'mark_busy\'):\n
      computer_partition = computer_partition_candidate\n
      computer_partition.markBusy()\n
      break\n
  elif computer_partition_candidate.getSlapState() == "busy":\n
    # Only assign slave instance on busy partition\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
return computer_partition.getRelativeUrl()\n


]]></string> </value>
        </item>
        <item>
            <key> <string>_params</string> </key>
            <value> <string>software_release_url, software_type, software_instance_portal_type, filter_kw</string> </value>
        </item>
        <item>
            <key> <string>id</string> </key>
            <value> <string>OpenSaleOrder_findPartition</string> </value>
        </item>
      </dictionary>
    </pickle>
  </record>
</ZopeData>
