コード例 #1
0
 def __init__(self, context, request):
     self.context = context
     self.request = request
     self.fields = [fo for fo in context._getFieldObjects() if not implementedOrProvidedBy(IField, fo)]
     
     self.action_adapters = [a for a in self.context.contentValues()
         if IPloneFormGenActionAdapter.providedBy(a)
         and IMultiplexedActionAdapter.providedBy(a)
         ]
コード例 #2
0
def delete_action_adapters(form, id=None):
    """
    Removes all the action adapters for the form (self) or just the one
    specified by `id`
    """
    for obj in form.objectValues():
        if IPloneFormGenActionAdapter.providedBy(obj):
            if id == None or id == obj.id:
                api.content.delete(obj=obj)
                print "Deleting existing adapter at: %s" % obj.absolute_url()
コード例 #3
0
    def actionAdaptersDL(self):
        """ returns Display List (id, title) tuples of contained adapters """

        # an adapter provides IPloneFormGenActionAdapter
        allAdapters = [(obj.getId(), obj.title) for obj in self.objectValues()
          if IPloneFormGenActionAdapter.providedBy(obj)]

        if allAdapters:
            return DisplayList(allAdapters)

        return DisplayList()
コード例 #4
0
    def actionAdaptersDL(self):
        """ returns Display List (id, title) tuples of contained adapters """

        # an adapter provides IPloneFormGenActionAdapter
        allAdapters = [(obj.getId(), obj.title) for obj in self.objectValues()
          if IPloneFormGenActionAdapter.providedBy(obj)]

        if allAdapters:
            return DisplayList(allAdapters)

        return DisplayList()
コード例 #5
0
    def onSuccess(self, fields, REQUEST=None):
        # wrap _onSuccess so we can do fallback behavior when calls to Salesforce fail

        message = None
        try:
            self._onSuccess(fields, REQUEST)
        except:
            from Products.salesforcepfgadapter.tests import TESTING
            if TESTING:
                raise

            # swallow the exception, but log it
            t, v = sys.exc_info()[:2]
            logger.exception('Unable to save form data to Salesforce. (%s)' % '/'.join(self.getPhysicalPath()))

            formFolder = aq_parent(self)
            enabled_adapters = formFolder.getActionAdapter()
            adapters = [o for o in formFolder.objectValues() if IPloneFormGenActionAdapter.providedBy(o)]
            active_savedata = [o for o in adapters if isinstance(o, FormSaveDataAdapter)
                                                   and o in enabled_adapters]
            inactive_savedata = [o for o in adapters if isinstance(o, FormSaveDataAdapter)
                                                     and o not in enabled_adapters]

            # 1. If there's an enabled save data adapter, just suppress the exception
            #    so the data will still be saved.
            if active_savedata:
                message = """
Someone submitted this form, but the data couldn't be saved to Salesforce
due to an exception: %s The data was recorded in this Saved-data adapter instead: %s
""" % (formFolder.absolute_url(), active_savedata[0].absolute_url())

            # 2. If there's a *disabled* save data adapter, call it.
            #    (This can be used to record data locally *only* when recording to Salesforce fails.)
            elif inactive_savedata:
                message = """
Someone submitted this form, but the data couldn't be saved to Salesforce
due to an exception: %s The data was recorded in this Saved-data adapter instead: %s
""" % (formFolder.absolute_url(), inactive_savedata[0].absolute_url())
                inactive_savedata[0].onSuccess(fields, REQUEST)

            # 3. If there's no savedata adapter, send the data in an e-mail.
            else:
                message = """
Someone submitted this form, but the data couldn't be saved to Salesforce
due to an exception: %s
""" % formFolder.absolute_url()

            err_msg = 'Technical details on the exception: '
            err_msg += ''.join(traceback.format_exception_only(t, v))

            mailer = FormMailerAdapter('dummy').__of__(formFolder)
            mailer.msg_subject = 'Form submission to Salesforce failed'
            mailer.subject_field = None
            # we rely on the PFG mailer's logic to choose a good fallback recipient
            mailer.recipient_name = ''
            mailer.recipient_email = ''
            mailer.cc_recipients = []
            mailer.bcc_recipients = []
            mailer.additional_headers = []
            mailer.body_type = 'html'
            mailer.setBody_pre(message, mimetype='text/html')
            mailer.setBody_post(err_msg, mimetype='text/html')
            if 'credit_card' in REQUEST.form:
                REQUEST.form['credit_card'] = '(hidden)'
            mailer.send_form(fields, REQUEST)
コード例 #6
0
    def onSuccess(self, fields, REQUEST=None):
        # wrap _onSuccess so we can do fallback behavior when calls to Salesforce fail

        message = None
        try:
            self._onSuccess(fields, REQUEST)
        except:
            from Products.salesforcepfgadapter.tests import TESTING
            if TESTING:
                raise

            # swallow the exception, but log it
            t, v = sys.exc_info()[:2]
            logger.exception('Unable to save form data to Salesforce. (%s)' %
                             '/'.join(self.getPhysicalPath()))

            formFolder = aq_parent(self)
            enabled_adapters = formFolder.getActionAdapter()
            adapters = [
                o for o in formFolder.objectValues()
                if IPloneFormGenActionAdapter.providedBy(o)
            ]
            active_savedata = [
                o for o in adapters
                if isinstance(o, FormSaveDataAdapter) and o in enabled_adapters
            ]
            inactive_savedata = [
                o for o in adapters if isinstance(o, FormSaveDataAdapter)
                and o not in enabled_adapters
            ]

            # 1. If there's an enabled save data adapter, just suppress the exception
            #    so the data will still be saved.
            if active_savedata:
                message = """
Someone submitted this form, but the data couldn't be saved to Salesforce
due to an exception: %s The data was recorded in this Saved-data adapter instead: %s
""" % (formFolder.absolute_url(), active_savedata[0].absolute_url())

            # 2. If there's a *disabled* save data adapter, call it.
            #    (This can be used to record data locally *only* when recording to Salesforce fails.)
            elif inactive_savedata:
                message = """
Someone submitted this form, but the data couldn't be saved to Salesforce
due to an exception: %s The data was recorded in this Saved-data adapter instead: %s
""" % (formFolder.absolute_url(), inactive_savedata[0].absolute_url())
                inactive_savedata[0].onSuccess(fields, REQUEST)

            # 3. If there's no savedata adapter, send the data in an e-mail.
            else:
                message = """
Someone submitted this form, but the data couldn't be saved to Salesforce
due to an exception: %s
""" % formFolder.absolute_url()

            err_msg = 'Technical details on the exception: '
            err_msg += ''.join(traceback.format_exception_only(t, v))

            mailer = FormMailerAdapter('dummy').__of__(formFolder)
            mailer.msg_subject = 'Form submission to Salesforce failed'
            mailer.subject_field = None
            # we rely on the PFG mailer's logic to choose a good fallback recipient
            mailer.recipient_name = ''
            mailer.recipient_email = ''
            mailer.cc_recipients = []
            mailer.bcc_recipients = []
            mailer.additional_headers = []
            mailer.body_type = 'html'
            mailer.setBody_pre(message, mimetype='text/html')
            mailer.setBody_post(err_msg, mimetype='text/html')
            if 'credit_card' in REQUEST.form:
                REQUEST.form['credit_card'] = '(hidden)'
            mailer.send_form(fields, REQUEST)
    def onSuccess(self, fields, REQUEST=None):
        """
        Wrap _onSuccess so fallback behavior can be implemented when calls
        to the web service fail.
        """
        # heavily borrowed from: https://plone.org/products/salesforcepfgadapter

        message = None
        try:
            self._onSuccess(fields, REQUEST)
        except Exception as e:
            # swallow the exception
            t, v = sys.exc_info()[:2]

            # generate a log message regarding the exception
            log_msg = 'Unable to save form data to the web service at (%s). '
            logger.exception(log_msg % '/'.join(self.getPhysicalPath()))

            if not self.failSilently:
                raise

            else:
                # get all the active and inactive save data and mailer adapters
                formFolder = self._getParentForm()
                enabled_adapters = formFolder.getActionAdapter()
                adapters = [o for o in formFolder.objectValues() if IPloneFormGenActionAdapter.providedBy(o)]
                active_savedata = [o for o in adapters if isinstance(o, FormSaveDataAdapter)
                                                       and o.id in enabled_adapters]
                inactive_savedata = [o for o in adapters if isinstance(o, FormSaveDataAdapter)
                                                         and o.id not in enabled_adapters]
                active_mailer = [o for o in adapters if isinstance(o, FormMailerAdapter)
                                                     and o.id in enabled_adapters]
                inactive_mailer = [o for o in adapters if isinstance(o, FormMailerAdapter)
                                                       and o.id not in enabled_adapters]

                # start the failure email message
                message = "Someone submitted this form (%s), but the data " \
                    + "couldn't be saved to the web service due to an exception." \
                    + "\n\n" \
                    + "The data was saved in the following locations:\n"
                message = message % (
                        formFolder.absolute_url(),
                        )

                # add a list of where the data was stored to the email message
                for adapter in active_savedata:
                    message += "  - Save Data Adapter (%s)\n" % adapter.absolute_url()

                if self.runDisabledAdapters:
                    for adapter in inactive_savedata:
                        message += "  - Save Data Adapter (%s)\n" % adapter.absolute_url()
                        # Trigger the adapter since it's disabled.
                        # This can be used to record data *only* when submitting to
                        #   the web service fails.
                        adapter.onSuccess(fields, REQUEST)

                for adapter in active_mailer:
                    message += "  - Mailer Adapter (%s)\n" % adapter.absolute_url()

                if self.runDisabledAdapters:
                    for adapter in inactive_mailer:
                        message += "  - Mailer Adapter (%s)\n" % adapter.absolute_url()
                        # Trigger the adapter since it's disabled.
                        # This can be used to record data *only* when submitting to
                        #   the web service fails.
                        adapter.onSuccess(fields, REQUEST)

                if not active_savedata and not inactive_savedata and \
                   not active_mailer and not inactive_mailer:
                    message += "  - NO WHERE! The data was lost.\n"

                message += "\nTechnical details on the exception:\n"
                message += ''.join(traceback.format_exception_only(t, v))

                # send an email if an address is provided
                if self.notifyOnFailure:
                    # get email configuration from Plone
                    pprops = getToolByName(self, 'portal_properties')
                    site_props = getToolByName(pprops, 'site_properties')
                    portal = getToolByName(self, 'portal_url').getPortalObject()

                    from_addr = site_props.getProperty('email_from_address') or \
                        portal.getProperty('email_from_address')
                    mailer = getToolByName(self, 'MailHost')
                    try:
                        mailer.send(
                            message,
                            mto=self.notifyOnFailure,
                            mfrom=from_addr,
                            subject='Form submission to web service failed',
                            immediate=False,
                            )
                    except Exception as e:
                        logger.exception(e)