def __init__(self):
     self.sparePartsTask = SparePartsTask()
     self.cherryPyBackgroundTasks = cherrypy.process.plugins.BackgroundTask(
         self.BACKGROUND_TASKS_TIME_IN_SECONDS, self.scheduledTasks)
     self.logger = LoggingManager.getInstance().getLogger(
         self.__class__.__name__)
     self.emailUtility = CdbEmailUtility.getInstance()
Exemplo n.º 2
0
 def getInstance(cls):
     from cdb.common.utility.cdbEmailUtility import CdbEmailUtility
     try:
         cdbEmailUtility = CdbEmailUtility()
     except CdbEmailUtility as utility:
         cdbEmailUtility = utility
     return cdbEmailUtility
Exemplo n.º 3
0
 def __init__(self):
     self.itemDbApi = ItemDbApi()
     self.userDbApi = UserDbApi()
     self.propertyDbApi = PropertyDbApi()
     self.logDbApi = LogDbApi()
     self.emailUtility = CdbEmailUtility.getInstance()
     self.cdbPortalUrlUtility = CdbPortalUrlUtility.getInstance()
     self.logger = LoggingManager.getInstance().getLogger(self.__class__.__name__)
Exemplo n.º 4
0
    def checkSpares(self):
        self.logger.debug('Checking status of spare parts.')
        catalogItemsWithSparePartsConfiguration = self.itemDbApi.getItemsWithPropertyType(
            self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME,
            itemDomainName=self.CATALOG_DOMAIN_NAME)

        for catalogItem in catalogItemsWithSparePartsConfiguration:
            catalogItemId = catalogItem.data['id']
            catalogSelfElementId = self.getSelfElementIdForItemId(
                catalogItemId)

            sparePartsConfigurationPropertyValue = self.propertyDbApi.getPropertyValueListForItemElementId(
                catalogSelfElementId,
                propertyTypeName=self.
                SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME)

            if sparePartsConfigurationPropertyValue:
                metadataDict = self.getPropertyMetadataDict(
                    sparePartsConfigurationPropertyValue[0])
            else:
                raise ObjectNotFound(
                    "Could not find required property: %s" %
                    self.SPARE_PARTS_CONFIGURATION_PROPERTY_TYPE_NAME)

            if self.SPARE_PARTS_CONFIGURATION_MIN_KEY in metadataDict:
                minSpares = int(
                    metadataDict[self.SPARE_PARTS_CONFIGURATION_MIN_KEY])
            else:
                raise CdbException(
                    "required metadata %s not specified for spare part configuration"
                    % self.SPARE_PARTS_CONFIGURATION_MIN_KEY)

            email = ''
            if metadataDict.has_key(self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY):
                emailValue = metadataDict[
                    self.SPARE_PARTS_CONFIGURATION_EMAIL_KEY]
                if emailValue == self.SPARE_PARTS_CONFIGURATION_NOEMAIL_VALUE:
                    email = None
                else:
                    email = emailValue
            else:
                # Use owner user email.
                email = self.getOwnerUserEmail(catalogItemId)

            sparePartsList = self.itemDbApi.getItemsWithPropertyType(
                self.ITEM_DOMAIN_INVENTORY_STATUS_PROPERTY_TYPE_NAME,
                itemDerivedFromItemId=catalogItemId,
                propertyValueMatch=self.
                ITEM_DOMAIN_INVENTORY_STATUS_SPARE_VALUE)
            spares = sparePartsList.__len__()

            if minSpares > spares:
                validNotification = True
                sparesMessage = self.generateSparesMessage(minSpares, spares)

                spareLogEntries = self.logDbApi.getLogEntriesForItemElementId(
                    catalogSelfElementId, self.SPARE_PARTS_WARNING_LOG_LEVEL)
                if spareLogEntries:
                    lastSpareLogEntry = spareLogEntries[-1]
                    lastSparesLogMessage = lastSpareLogEntry.data['text']
                    if lastSparesLogMessage == sparesMessage:
                        validNotification = False

                if validNotification:
                    self.addSparePartsWarningLogEntryToItem(
                        catalogSelfElementId, sparesMessage)
                    if email is not None:
                        itemUrl = self.cdbPortalUrlUtility.getItemUrlAddress(
                            catalogItemId)
                        catalogItemName = catalogItem.data['name']
                        catalogItemModel = catalogItem.data['item_identifier1']
                        catalogItemAlternateName = catalogItem.data[
                            'item_identifier2']
                        itemDictValue = '<a href=%s>%s</a>' % (itemUrl,
                                                               catalogItemName)

                        # Create an ordered dict for the table emailed to user.
                        informationDict = OrderedDict()
                        informationDict['Name'] = itemDictValue
                        informationDict['Model'] = catalogItemModel
                        informationDict[
                            'Alternate Name'] = catalogItemAlternateName
                        informationDict['Spares On Hand'] = spares
                        informationDict['Minimum Spares Required '] = minSpares

                        emailTable = CdbEmailUtility.generateSimpleHtmlTableMessage(
                            informationDict)

                        emailMessage = '%s <br/><br/> %s' % (sparesMessage,
                                                             emailTable)

                        self.emailUtility.sendEmailNotification(
                            email, self.SPARE_PARTS_EMAIL_NOTIFICATION_NAME,
                            emailMessage)