Esempio n. 1
0
    def subscribe(self, action, data):
        connector = interfaces.IConnector(self.context)

        email = data.pop('email')
        if 'subscriber_list' in data:
            subscriber_list = data.pop('subscriber_list')
        else:
            subscriber_list = [li.token for li in self.subscriber_list_voc]

        if len(subscriber_list) == 0:
            self.status = _(u"You must choose at last one list.")
            self.info = True
            self.form_reset = False
            return
        success, errors = connector.addSubscribe(subscriber_list, email, data)
        if len(errors):
            msgs = [' '.join([translate(msg, context=self.request) \
                              for msg in err.args]) for err in errors]
            self.status = ', '.join(msgs)
            self.errors = True
            self.form_reset = False
        if success:
            self.form_reset = True
            msgid = u"You successfully subscribed to: ${lists}."
            self.successMessage = _(msgid,
                                    mapping={'lists': ', '.join(success)})
            self.template = self.template_message
Esempio n. 2
0
    def unsubscribe(self, ids, email, **kwargs):
        self.checkConnection()

        defaults = dict(delete_member=self.props.unsubscribe_delete_member,
                        send_goodbye=self.props.unsubscribe_send_goodbye,
                        send_notify=self.props.unsubscribe_send_notify)
        defaults.update(kwargs)

        errors = []
        success = []

        for id in ids:
            if id in self.cleanUpLists(ids):
                list_name = self.getListName(id)
                try:
                    self.mailChimp.lists.unsubscribe(id=id,
                                                     email={'email':email},
                                                     **defaults)
                    success.append(list_name)

                except mailchimp.Error, error:
                    mapping = dict(email=email, list_name=list_name)
                    error = self.translateError(error)
                    error.args = [_(msg, mapping=mapping)\
                                  for msg in error.args or [error.message]]
                    errors.append(error)

            else:
                msg = _(u'The chosen list is not available anymore.')
                errors.append(msg)
Esempio n. 3
0
    def subscribe(self, ids, email, merge_vars={}, **kwargs):
        self.checkConnection()

        defaults = dict(email_type=self.props.subscribe_email_type,
                        double_optin=self.props.subscribe_double_optin,
                        update_existing=self.props.subscribe_update_existing,
                        replace_interests=self.props.subscribe_replace_interests,
                        send_welcome=self.props.subscribe_send_welcome)
        defaults.update(kwargs)

        errors = []
        success = []

        for id in ids:
            if id in self.cleanUpLists(ids):
                list_name = self.getListName(id)
                try:
                    self.mailChimp.lists.subscribe(id=id,
                                                   email={'email':email},
                                                   merge_vars=merge_vars,
                                                   **defaults)
                    success.append(list_name)

                except mailchimp.Error, error:
                    mapping = dict(email=email, list_name=list_name)
                    error = self.translateError(error)
                    error.args = [_(msg, mapping=mapping)\
                                  for msg in error.args or [error.message]]
                    errors.append(error)

            else:
                msg = _(u'The chosen list is not available anymore.')
                errors.append(msg)
Esempio n. 4
0
    def addSubscribe(self, ids, email_address, merge_vars={}, **kwargs):
        if self.mailChimp is None:
            self.setNewAPIKey(self.props.mailchimp_api_key)
        defaults = dict(email_type=self.props.lists_email_type,
                        double_optin=self.props.lists_double_optin,
                        update_existing=self.props.lists_update_existing,
                        replace_interests=self.props.lists_replace_interests,
                        send_welcome=self.props.lists_send_welcome)
        defaults.update(kwargs)

        # quick fix if merge_vars are empty
        if not len(merge_vars):
            merge_vars = dict(dummy='dummy')

        errors = []
        success = []
        lists = self.getLists()
        for id in ids:
            if id in [li['id'] for li in lists]:
                name = [i for i in lists if i['id'] == id][0]['name']
                try:
                    self.mailChimp(method='listSubscribe',
                                   id=id,
                                   email_address=email_address,
                                   merge_vars=merge_vars,
                                   **defaults)
                    success.append(name)
                except KeyError, error:
                    # special error for non-ascii chars
                    # backward compatibility
                    error.args = getattr(error, 'args', tuple())
                    error.args = [_('Invalid character: ${char}',
                                    mapping=dict(char=msg))\
                                  for msg in error.args or [error.msg]]
                    errors.append(error)
                except Exception, error:
                    mapping = dict(email=email_address, list=name)
                    # backward compatibility
                    error.args = getattr(error, 'args', tuple())
                    error.args = [_(msg.replace(email_address, '${email}')\
                                    .replace(name, '${list}'),
                                    mapping=mapping)\
                                  for msg in error.args or [error.msg]]
                    errors.append(error)
Esempio n. 5
0
    def delSubscribe(self, ids, email_address, **kwargs):
        if self.mailChimp is None:
            self.setNewAPIKey(self.props.mailchimp_api_key)
        defaults = dict(delete_member=False,
                        send_goodbye=True,
                        send_notify=True)
        defaults.update(kwargs)

        errors = []
        success = []
        lists = self.getLists()
        for id in ids:
            if id in [li['id'] for li in lists]:
                name = [i for i in lists if i['id'] == id][0]['name']
                try:
                    self.mailChimp(method='listUnsubscribe',
                                   id=id,
                                   email_address=email_address,
                                   **defaults)
                    success.append(name)
                except KeyError, error:
                    # special error for non-ascii chars
                    # backward compatibility
                    error.args = getattr(error, 'args', tuple())
                    error.args = [_('Invalid character: ${char}',
                                    mapping=dict(char=msg))\
                                  for msg in error.args or [error.msg]]
                    errors.append(error)
                except Exception, error:
                    mapping = dict(email=email_address, list=name)
                    # backward compatibility
                    error.args = getattr(error, 'args', tuple())
                    error.args = [_(msg.replace(email_address, '${email}')\
                                    .replace(name, '${list}'),
                                    mapping=mapping)\
                                  for msg in error.args or [error.msg]]
                    errors.append(error)
Esempio n. 6
0
 def name(self):
     return self.data.name or _(u"Subscribe to newsletter")
Esempio n. 7
0
 def title(self):
     return _(u"MailChimp")
Esempio n. 8
0
def errorMessage(context):
    if not interfaces.IConnector(context).isValid:
        utils = getToolByName(context, 'plone_utils')
        msg = _(u'API key entry required for editing this portlet.')
        utils.addPortalMessage(msg, 'error')
Esempio n. 9
0
from plone.portlets.interfaces import IPortletDataProvider
from raptus.mailchimp import MessageFactory as _
from raptus.mailchimp import interfaces


def available_list(context):
    connector = interfaces.IConnector(context)
    lists = connector.getLists()
    if hasattr(context, 'data'):
        available = context.data.available_list
        context.data.available_list = connector.cleanUpLists(available)
    terms = [SimpleTerm(value=li['id'], title=li['name']) for li in lists]
    return SimpleVocabulary(terms)

custom_fields = SimpleVocabulary.fromItems((
    (_(u"First name"), "FNAME"),
    (_(u"Last name"), "LNAME"),
    (_(u"E-mail format"), "EMAILTYPE")))

def errorMessage(context):
    if not interfaces.IConnector(context).isValid:
        utils = getToolByName(context, 'plone_utils')
        msg = _(u'API key entry required for editing this portlet.')
        utils.addPortalMessage(msg, 'error')


class IMailChimpPortlet(IPortletDataProvider):
    """A Mailchimp portlet"""

    name = schema.TextLine(
    title=_(u'Title'),
Esempio n. 10
0
                    # backward compatibility
                    error.args = getattr(error, 'args', tuple())
                    error.args = [_('Invalid character: ${char}',
                                    mapping=dict(char=msg))\
                                  for msg in error.args or [error.msg]]
                    errors.append(error)
                except Exception, error:
                    mapping = dict(email=email_address, list=name)
                    # backward compatibility
                    error.args = getattr(error, 'args', tuple())
                    error.args = [_(msg.replace(email_address, '${email}')\
                                    .replace(name, '${list}'),
                                    mapping=mapping)\
                                  for msg in error.args or [error.msg]]
                    errors.append(error)
            else:
                msg = _(u'The chosen list is not available anymore.')
                errors.append(greatape.MailChimpError(msg))
        return success, errors

    def cleanUpLists(self, lists):
        return [li for li in lists if li in [i['id'] for i in self.getLists()]]

    def setNewAPIKey(self, apikey):
        self.mailChimp = greatape.MailChimp(apikey, self.props.mailchimp_ssl,
                                            self.props.mailchimp_debug)
        try:
            self.isValid = self.mailChimp(method='ping')
        except:
            self.isValid = False
Esempio n. 11
0
def email_formats(context):
    return SimpleVocabulary([
        SimpleTerm(value="html", token="html", title=_(u"HTML")),
        SimpleTerm(value="text", token="text", title=_(u"Text")),
        SimpleTerm(value="mobile", token="mobile", title=_(u"Mobile"))
        ])
Esempio n. 12
0
 def getListName(self, list_id):
     list = self.mailChimp.lists.list(filters={'list_id':list_id}).get('data')
     return list[0].get('name', _(u'Error retrieving list name'))