<?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 json\n
\n
portal = context.getPortalObject()\n
\n
if portal.ERP5Site_isSupportRequestCreationClosed():\n
  # Stop ticket creation\n
  return\n
\n
reference = context.getReference()\n
computer_title = context.getTitle()\n
ticket_title = "[MONITORING] Lost contact with computer %s" % reference\n
description = ""\n
should_notify = True\n
last_contact = "No Contact Information"\n
\n
memcached_dict = context.getPortalObject().portal_memcached.getMemcachedDict(\n
  key_prefix=\'slap_tool\',\n
  plugin_path=\'portal_memcached/default_memcached_plugin\')\n
\n
try:\n
  d = memcached_dict[reference]\n
  d = json.loads(d)\n
  last_contact = DateTime(d.get(\'created_at\'))\n
  if (DateTime() - last_contact) > 0.01:\n
    description = "The Computer %s (%s) has not contacted the server for more than 30 minutes" \\\n
    "(last contact date: %s)" % (computer_title, reference, last_contact)\n
  else:\n
    should_notify = False\n
except KeyError:\n
  ticket_title = "[MONITORING] No information about %s" % reference\n
  description = "The Computer %s (%s)  has not contacted the server (No Contact Information)" % (\n
                  computer_title, reference)\n
\n
if should_notify:\n
  support_request = context.Base_generateSupportRequestForSlapOS(\n
    ticket_title,\n
    description,\n
    context.getRelativeUrl()\n
  )\n
\n
  person = context.getSourceAdministrationValue(portal_type="Person")\n
  if not person:\n
    return support_request\n
\n
  # Send Notification message\n
  notification_reference = \'slapos-crm-computer_check_state.notification\'\n
  notification_message = portal.portal_notifications.getDocumentValue(\n
                 reference=notification_reference)\n
\n
  if notification_message is None:\n
    message = """%s""" % description\n
  else:\n
    mapping_dict = {\'computer_title\':context.getTitle(),\n
                    \'computer_id\':reference,\n
                    \'last_contact\':last_contact}\n
    message = notification_message.asText(\n
              substitution_method_parameter_dict={\'mapping_dict\':mapping_dict})\n
\n
  support_request.SupportRequest_trySendNotificationMessage(\n
              ticket_title,\n
              message, person.getRelativeUrl())\n
              \n
  return support_request\n


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