def sendNotification(obj, transition, transitionName, workflow):
    '''Sends mail about p_transition named p_transitionName, that has been
       triggered on p_obj that is controlled by p_workflow.'''
    from appy.gen.descriptors import WorkflowDescriptor
    wfName = WorkflowDescriptor.getWorkflowName(workflow.__class__)
    zopeObj = obj.o
    tool = zopeObj.getTool()
    mailInfo = transition.notify(workflow, obj)
    if not mailInfo[0]: return  # Send a mail to nobody.
    # mailInfo may be one of the following:
    #   (to,)
    #   (to, cc)
    #   (to, mailSubject, mailBody)
    #   (to, cc, mailSubject, mailBody)
    # "to" and "cc" maybe simple strings (one simple string = one email
    # address or one role) or sequences of strings.
    # Determine mail subject and body.
    if len(mailInfo) <= 2:
        # The user didn't mention mail body and subject. We will use those
        # defined from i18n labels.
        wfHistory = zopeObj.getHistory()
        labelPrefix = '%s_%s' % (wfName, transitionName)
        tName = obj.translate(labelPrefix)
        keys = {
            'siteUrl': tool.getPath('/').absolute_url(),
            'siteTitle': tool.getAppName(),
            'objectUrl': zopeObj.absolute_url(),
            'objectTitle': zopeObj.Title(),
            'transitionName': tName,
            'transitionComment': wfHistory[0]['comments']
        }
        mailSubject = obj.translate(labelPrefix + '_mail_subject', keys)
        mailBody = obj.translate(labelPrefix + '_mail_body', keys)
    else:
        mailSubject = mailInfo[-1]
        mailBody = mailInfo[-2]
    # Determine "to" and "cc".
    to = mailInfo[0]
    cc = []
    if (len(mailInfo) in (2, 4)) and mailInfo[1]: cc = mailInfo[1]
    if type(to) not in sequenceTypes: to = [to]
    if type(cc) not in sequenceTypes: cc = [cc]
    # Send the mail
    sendMail(tool.appy(), to, mailSubject, mailBody)
Beispiel #2
0
Datei: mail.py Projekt: a-iv/appy
def sendNotification(obj, transition, transitionName, workflow):
    '''Sends mail about p_transition named p_transitionName, that has been
       triggered on p_obj that is controlled by p_workflow.'''
    from appy.gen.descriptors import WorkflowDescriptor
    wfName = WorkflowDescriptor.getWorkflowName(workflow.__class__)
    zopeObj = obj.o
    tool = zopeObj.getTool()
    mailInfo = transition.notify(workflow, obj)
    if not mailInfo[0]: return # Send a mail to nobody.
    # mailInfo may be one of the following:
    #   (to,)
    #   (to, cc)
    #   (to, mailSubject, mailBody)
    #   (to, cc, mailSubject, mailBody)
    # "to" and "cc" maybe simple strings (one simple string = one email
    # address or one role) or sequences of strings.
    # Determine mail subject and body.
    if len(mailInfo) <= 2:
        # The user didn't mention mail body and subject. We will use those
        # defined from i18n labels.
        wfHistory = zopeObj.getHistory()
        labelPrefix = '%s_%s' % (wfName, transitionName)
        tName = obj.translate(labelPrefix)
        keys = {'siteUrl': tool.getPath('/').absolute_url(),
                'siteTitle': tool.getAppName(),
                'objectUrl': zopeObj.absolute_url(),
                'objectTitle': zopeObj.Title(),
                'transitionName': tName,
                'transitionComment': wfHistory[0]['comments']}
        mailSubject = obj.translate(labelPrefix + '_mail_subject', keys)
        mailBody = obj.translate(labelPrefix + '_mail_body', keys)
    else:
        mailSubject = mailInfo[-1]
        mailBody = mailInfo[-2]
    # Determine "to" and "cc".
    to = mailInfo[0]
    cc = []
    if (len(mailInfo) in (2,4)) and mailInfo[1]: cc = mailInfo[1]
    if type(to) not in sequenceTypes: to = [to]
    if type(cc) not in sequenceTypes: cc = [cc]
    # Send the mail
    sendMail(tool.appy(), to, mailSubject, mailBody)
Beispiel #3
0
 def generateWorkflow(self, wfDescr):
     '''This method creates the i18n labels related to the workflow described
        by p_wfDescr.'''
     k = wfDescr.klass
     print 'Generating %s.%s (gen-workflow)...' % (k.__module__, k.__name__)
     # Identify workflow name
     wfName = WorkflowDescriptor.getWorkflowName(wfDescr.klass)
     # Add i18n messages for states
     for name in dir(wfDescr.klass):
         if not isinstance(getattr(wfDescr.klass, name), State): continue
         poMsg = PoMessage('%s_%s' % (wfName, name), '', name)
         poMsg.produceNiceDefault()
         self.labels.append(poMsg)
     # Add i18n messages for transitions
     for name in dir(wfDescr.klass):
         transition = getattr(wfDescr.klass, name)
         if not isinstance(transition, Transition): continue
         poMsg = PoMessage('%s_%s' % (wfName, name), '', name)
         poMsg.produceNiceDefault()
         self.labels.append(poMsg)
         if transition.confirm:
             # We need to generate a label for the message that will be shown
             # in the confirm popup.
             label = '%s_%s_confirm' % (wfName, name)
             poMsg = PoMessage(label, '', PoMessage.CONFIRM)
             self.labels.append(poMsg)
         if transition.notify:
             # Appy will send a mail when this transition is triggered.
             # So we need 2 i18n labels: one for the mail subject and one for
             # the mail body.
             subjectLabel = '%s_%s_mail_subject' % (wfName, name)
             poMsg = PoMessage(subjectLabel, '', PoMessage.EMAIL_SUBJECT)
             self.labels.append(poMsg)
             bodyLabel = '%s_%s_mail_body' % (wfName, name)
             poMsg = PoMessage(bodyLabel, '', PoMessage.EMAIL_BODY)
             self.labels.append(poMsg)
Beispiel #4
0
def sendMail(obj, transition, transitionName, workflow):
    '''Sends mail about p_transition that has been triggered on p_obj that is
       controlled by p_workflow.'''
    wfName = WorkflowDescriptor.getWorkflowName(workflow.__class__)
    ploneObj = obj.o
    portal = ploneObj.portal_url.getPortalObject()
    mailInfo = transition.notify(workflow, obj)
    if not mailInfo[0]: return # Send a mail to nobody.
    # mailInfo may be one of the following:
    #   (to,)
    #   (to, cc)
    #   (to, mailSubject, mailBody)
    #   (to, cc, mailSubject, mailBody)
    # "to" and "cc" maybe simple strings (one simple string = one email
    # address or one role) or sequences of strings.
    # Determine mail subject and body.
    if len(mailInfo) <= 2:
        # The user didn't mention mail body and subject. We will use
        # those defined from i18n labels.
        wfHistory = ploneObj.getWorkflowHistory()
        labelPrefix = '%s_%s' % (wfName, transitionName)
        tName = obj.translate(labelPrefix)
        keys = {'siteUrl': portal.absolute_url(),
                'siteTitle': portal.Title(),
                'objectUrl': ploneObj.absolute_url(),
                'objectTitle': ploneObj.Title(),
                'transitionName': tName,
                'transitionComment': wfHistory[0]['comments']}
        mailSubject = obj.translate(labelPrefix + '_mail_subject', keys)
        mailBody = obj.translate(labelPrefix + '_mail_body', keys)
    else:
        mailSubject = mailInfo[-1]
        mailBody = mailInfo[-2]
    # Determine "to" and "cc".
    to = mailInfo[0]
    cc = []
    if (len(mailInfo) in (2,4)) and mailInfo[1]: cc = mailInfo[1]
    if type(to) not in sequenceTypes: to = [to]
    if type(cc) not in sequenceTypes: cc = [cc]
    # Among "to" and "cc", convert all roles to concrete email addresses
    to = convertRolesToEmails(to, portal)
    cc = convertRolesToEmails(cc, portal)
    # Determine "from" address
    enc= portal.portal_properties.site_properties.getProperty('default_charset')
    fromAddress = getEmailAddress(
        portal.getProperty('email_from_name'),
        portal.getProperty('email_from_address'), enc)
    # Send the mail
    i = 0
    for recipient in to:
        i += 1
        try:
            if i != 1: cc = []
            portal.MailHost.secureSend(mailBody.encode(enc),
                recipient.encode(enc), fromAddress.encode(enc),
                mailSubject.encode(enc), mcc=cc, charset='utf-8')
        except socket.error, sg:
            obj.log(SENDMAIL_ERROR % str(sg), type='warning')
            break
        except UnicodeDecodeError, ue:
            obj.log(ENCODING_ERROR % str(ue), type='warning')
            break
Beispiel #5
0
def sendMail(obj, transition, transitionName, workflow):
    '''Sends mail about p_transition that has been triggered on p_obj that is
       controlled by p_workflow.'''
    wfName = WorkflowDescriptor.getWorkflowName(workflow.__class__)
    zopeObj = obj.o
    tool = zopeObj.getTool()
    mailInfo = transition.notify(workflow, obj)
    if not mailInfo[0]: return  # Send a mail to nobody.
    # mailInfo may be one of the following:
    #   (to,)
    #   (to, cc)
    #   (to, mailSubject, mailBody)
    #   (to, cc, mailSubject, mailBody)
    # "to" and "cc" maybe simple strings (one simple string = one email
    # address or one role) or sequences of strings.
    # Determine mail subject and body.
    if len(mailInfo) <= 2:
        # The user didn't mention mail body and subject. We will use those
        # defined from i18n labels.
        wfHistory = zopeObj.getHistory()
        labelPrefix = '%s_%s' % (wfName, transitionName)
        tName = obj.translate(labelPrefix)
        keys = {
            'siteUrl': tool.getPath('/').absolute_url(),
            'siteTitle': tool.getAppName(),
            'objectUrl': zopeObj.absolute_url(),
            'objectTitle': zopeObj.Title(),
            'transitionName': tName,
            'transitionComment': wfHistory[0]['comments']
        }
        mailSubject = obj.translate(labelPrefix + '_mail_subject', keys)
        mailBody = obj.translate(labelPrefix + '_mail_body', keys)
    else:
        mailSubject = mailInfo[-1]
        mailBody = mailInfo[-2]
    # Determine "to" and "cc".
    to = mailInfo[0]
    cc = []
    if (len(mailInfo) in (2, 4)) and mailInfo[1]: cc = mailInfo[1]
    if type(to) not in sequenceTypes: to = [to]
    if type(cc) not in sequenceTypes: cc = [cc]
    # Among "to" and "cc", convert all roles to concrete email addresses
    to = convertRolesToEmails(to, portal)
    cc = convertRolesToEmails(cc, portal)
    # Determine "from" address
    enc = portal.portal_properties.site_properties.getProperty(
        'default_charset')
    fromAddress = getEmailAddress(portal.getProperty('email_from_name'),
                                  portal.getProperty('email_from_address'),
                                  enc)
    # Send the mail
    i = 0
    for recipient in to:
        i += 1
        try:
            if i != 1: cc = []
            portal.MailHost.secureSend(mailBody.encode(enc),
                                       recipient.encode(enc),
                                       fromAddress.encode(enc),
                                       mailSubject.encode(enc),
                                       mcc=cc,
                                       charset='utf-8')
        except socket.error, sg:
            obj.log(SENDMAIL_ERROR % str(sg), type='warning')
            break
        except UnicodeDecodeError, ue:
            obj.log(ENCODING_ERROR % str(ue), type='warning')
            break