def subscribe(self, request):
     email = self.get_email(request)
     merge_vars = self.get_merge_vars(request)
     if not (email or merge_vars):
         return
     properties = getToolByName(self, 'portal_properties')
     chimp_props = getattr(properties, 'mailchimp_properties', None)
     if chimp_props:
         api_key = getattr(chimp_props, 'mailchimp_api_key', '')
         list_id = getattr(chimp_props, 'mailchimp_list_id', '')
         try:
             chimp = Connection(api_key)
             chimp.list_subscribe(
                 id=list_id,
                 email_address=email,
                 merge_vars=merge_vars,
                 double_optin=False)
         except ChimpyException, e:
             log.warn("While trying to subscribe %s. Mailchimp replied: %s" % (
                 email, e.message))
             if 'already subscribed' in e.message:
                 pass # that's fine for our purposes
             else:
                 raise # must be a real problem
示例#2
0
def list_subscribe():
    """Subscribes an user to the mailing list

    Inspected arguments in a POST request

    If first_name and last_name is given it is used
    Else name is split into two to be used

    Then the form is inspected for `mailing_list`. If provided the value
    is used as ID for the subscription. If not the default list is used for
    subscription. you could put in this value as a hidden field in the form

    Always returns a JSON response:
    {
        'success': True or False,
        'message': A message (Not recommended to be displayed to user)
    }
    """
    if not request.nereid_website.mailchimp_api_key:
        current_app.logger.error("nereid-mailchimp No API key")
        return jsonify(
            success=False,
            message="No API Configured"
        )

    if request.method == 'POST':
        # Mailchimp requires first name and last name, but nereid probably
        # took only the name field. Check for the keys to decide what to pick
        email = request.values['email']

        merge_vars = {}
        keys = request.values.keys()

        if 'first_name' in keys and 'last_name' in keys:
            merge_vars['FNAME'], merge_vars['LNAME'] = (
                request.values['first_name'], request.values['last_name'])
        elif 'name' in keys:
            try:
                merge_vars['FNAME'], merge_vars['LNAME'] = request.values[
                    'name'].split(' ', 1)
            except ValueError:
                merge_vars['FNAME'] = merge_vars['LNAME'] = \
                    request.values['name']
        else:
            merge_vars.update({'FIRST': '', 'LAST': ''})

        chimpy_connection = Connection(
            request.nereid_website.mailchimp_api_key
        )

        mailing_list = request.values['mailing_list'] \
            if 'mailing_list' in keys else None
        if mailing_list is None:
            # If no mailing list was there in the form then use the default one
            lists = chimpy_connection.lists()
            mailing_list_name = request.nereid_website.mailchimp_default_list
            for each_list in lists:
                if each_list['name'] == mailing_list_name:
                    mailing_list = each_list['id']
                    break
            else:
                return jsonify(
                    success=False,
                    message="No mailing list specified, default one not found"
                )
        #  Call Subscribe
        merge_vars['OPTIN_IP'] = request.remote_addr
        try:
            chimpy_connection.list_subscribe(mailing_list, email, merge_vars)
        except ChimpyException, exc:
            return jsonify(success=False, message=exc[0])
        return jsonify(success=True, message="Successfuly subscribed user!")