AlarmTool.py 7.33 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
##############################################################################
#
# Copyright (c) 2004 Nexedi SARL and Contributors. All Rights Reserved.
#                    Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

29
import time
Sebastien Robin's avatar
Sebastien Robin committed
30
from AccessControl import ClassSecurityInfo
31
from AccessControl.SecurityManagement import newSecurityManager
Sebastien Robin's avatar
Sebastien Robin committed
32 33 34 35 36 37 38 39
from Globals import InitializeClass, DTMLFile, PersistentMapping
from Products.ERP5Type.Document.Folder import Folder
from Products.ERP5Type.Tool.BaseTool import BaseTool
from Products.ERP5Type import Permissions
from Products.ERP5 import _dtmldir
from Products.CMFCore import CMFCorePermissions
from DateTime import DateTime

40
from zLOG import LOG, INFO
Sebastien Robin's avatar
Sebastien Robin committed
41

42 43 44
try:
  from Products.TimerService import getTimerService
except ImportError:
45
  def getTimerService(self):
46
    pass
Sebastien Robin's avatar
Sebastien Robin committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

class AlarmTool(BaseTool):
  """
  This tool will be usefull to  manage alarms. There's
  alarms everywhere in ERP5, and it is a nice idea to be able
  to manage all of them from a central point.

  Inside this tool we will have a way to retrieve all reports comings
  from Alarms,...
  """
  id = 'portal_alarms'
  meta_type = 'ERP5 Alarm Tool'
  portal_type = 'Alarm Tool'
  allowed_types = ('Supply Alarm Line',)

  # Declarative Security
  security = ClassSecurityInfo()

  security.declareProtected( Permissions.ManagePortal, 'manage_overview' )
  manage_overview = DTMLFile( 'explainAlarmTool', _dtmldir )

  security.declareProtected( Permissions.ManagePortal , 'manageAlarmList' )
  manageAlarmList = DTMLFile( 'manageAlarmList', _dtmldir )

  manage_options = ( ( { 'label'   : 'Overview'
             , 'action'   : 'manage_overview'
             }
            , { 'label'   : 'All Alarms'
             , 'action'   : 'manageAlarmList'
             }
            )
           + Folder.manage_options
           )

81 82
  interval = 60 # Default interval for alarms is 60 seconds
  last_tic = time.time()
Sebastien Robin's avatar
Sebastien Robin committed
83 84 85 86 87 88 89 90 91 92 93
  # Factory Type Information
  factory_type_information = \
    {    'id'             : portal_type
       , 'meta_type'      : meta_type
       , 'description'    : """\
TemplateTool manages Business Templates."""
       , 'icon'           : 'folder_icon.gif'
       , 'product'        : 'ERP5Type'
       , 'factory'        : 'addFolder'
       , 'immediate_view' : 'Folder_viewContentList'
       , 'allow_discussion'     : 1
94
       , 'allowed_content_types': ('Business Template',)
Sebastien Robin's avatar
Sebastien Robin committed
95 96 97 98 99 100 101 102 103 104 105 106
       , 'filter_content_types' : 1
       , 'global_allow'   : 1
       , 'actions'        :
      ( { 'id'            : 'view'
        , 'name'          : 'View'
        , 'category'      : 'object_view'
        , 'action'        : 'Folder_viewContentList'
        , 'permissions'   : (
            Permissions.View, )
        },
      )
    }
Sebastien Robin's avatar
Sebastien Robin committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120

  # API to manage alarms
  """
  This is what we should do:

  -- be able to see all alarms stored everywhere
  -- defines global alarms
  -- activate an alarm
  -- see reports
  -- see active alarms
  -- retrieve all alarms
  """

  security.declareProtected(Permissions.ModifyPortalContent, 'getAlarmList')
Sebastien Robin's avatar
Sebastien Robin committed
121
  def getAlarmList(self,to_active=0):
Sebastien Robin's avatar
Sebastien Robin committed
122 123 124
    """
    We retrieve thanks to the catalog the full list of alarms
    """
125 126 127 128 129 130 131 132 133 134
    user = self.portal_catalog.getOwner()
    newSecurityManager(self.REQUEST, user)
    if to_active:
      now = str(DateTime())
      date_expression = '<= %s' % now
      catalog_search = self.portal_catalog(portal_type = self.getPortalAlarmTypeList(), alarm_date=date_expression)
    else:
      catalog_search = self.portal_catalog(portal_type = self.getPortalAlarmTypeList())
    alarm_list = map(lambda x:x.getObject(),catalog_search)
    # LOG('AlarmTool.getAlarmList, alarm_list',0,alarm_list)
Sebastien Robin's avatar
Sebastien Robin committed
135
    if to_active:
136 137
      now = DateTime()
      date_expression = '<= %s' % str(now)
Sebastien Robin's avatar
Sebastien Robin committed
138
      catalog_search = self.portal_catalog(portal_type = self.getPortalAlarmTypeList(), alarm_date=date_expression)
139 140
      # check again the alarm date in case the alarm was not yet reindexed
      alarm_list = [x.getObject() for x in catalog_search if x.getObject().getAlarmDate()<=now]
Sebastien Robin's avatar
Sebastien Robin committed
141 142
    else:
      catalog_search = self.portal_catalog(portal_type = self.getPortalAlarmTypeList())
143
      alarm_list = map(lambda x:x.getObject(),catalog_search)
Sebastien Robin's avatar
Sebastien Robin committed
144 145 146 147 148 149 150 151 152
    return alarm_list

  security.declareProtected(Permissions.ModifyPortalContent, 'tic')
  def tic(self):
    """
    We will look at all alarms and see if they should be activated,
    if so then we will activate them.
    """
    current_date = DateTime()
153 154 155 156 157 158 159 160 161 162 163 164 165 166
    for alarm in self.getAlarmList(to_active=1):
      if alarm:
        user = alarm.getOwner()
        newSecurityManager(self.REQUEST, user)
        if alarm.isActive() or not alarm.isEnabled(): 
          # do nothing if already active, or not enabled
          continue
        alarm.activate().activeSense()

  security.declareProtected(Permissions.ManageProperties, 'subscribe')
  def subscribe(self):
    """ subscribe to the global Timer Service """
    service = getTimerService(self)
    if not service:
167 168 169
      LOG('AlarmTool', INFO, 'TimerService not available')
      return
      
170 171 172 173 174 175 176 177
    service.subscribe(self)
    return "Subscribed to Timer Service"

  security.declareProtected(Permissions.ManageProperties, 'unsubscribe')
  def unsubscribe(self):
    """ unsubscribe from the global Timer Service """
    service = getTimerService(self)
    if not service:
178 179 180
      LOG('AlarmTool', INFO, 'TimerService not available')
      return
    
181 182 183 184 185
    service.unsubscribe(self)
    return "Usubscribed from Timer Service"

  def manage_beforeDelete(self, item, container):
    self.unsubscribe()
186 187
    BaseTool.inheritedAttribute('manage_beforeDelete')(self, item, container)
    
188 189
  def manage_afterAdd(self, item, container):
    self.subscribe()
190 191
    BaseTool.inheritedAttribute('manage_afterAdd')(self, item, container)
        
192
  def process_timer(self, tick, interval, prev="", next=""):
193 194 195 196 197 198 199 200
    """ 
    Call tic() every x seconds. x is defined in self.interval
    This method is called by TimerService in the interval given
    in zope.conf. The Default is every 5 seconds.
    """
    if tick - self.last_tic >= self.interval:
      self.tic()
      self.last_tic = tick
Sebastien Robin's avatar
Sebastien Robin committed
201 202 203
    for alarm in self.getAlarmList(to_active=1):
      if alarm.isActive() or not alarm.isEnabled(): 
        # do nothing if already active, or not enabled
Sebastien Robin's avatar
Sebastien Robin committed
204
        continue
Sebastien Robin's avatar
Sebastien Robin committed
205
      alarm.activate().activeSense()
Sebastien Robin's avatar
Sebastien Robin committed
206