Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
import logging
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils.importlib import import_module

log = logging.getLogger(__name__)


class BaseMailingList(object):
    """ Defines Interface for mailing list client

    Most of the design was explained here:
        http://charlesleifer.com/blog/django-patterns-pluggable-backends/

    """

    def __init__(self):
        self.backend_class = self._get_backend()
        self.backend = self.backend_class()

    def _get_backend(self):
        """
        Get and return the backend configured in settings We do this here so
        that we're checking the settings at the time the object is created.
        """
        # TODO: repackage this correctly
        self.mailing_list_backend = getattr(
            settings, 'MAILING_LIST_BACKEND',
            'mailinglist_client.backends.dummy_backend.DummyMailingList')
        log.debug("Set mailing list backend as: %s", self.mailing_list_backend)

        # grab the classname off of the backend string
        package, klass = self.mailing_list_backend.rsplit('.', 1)

        # dynamically import the module
        module = import_module(package)

        # pull the class off the module and return
        return getattr(module, klass)

    def _improperly_configured(self, missing_setting):
        raise ImproperlyConfigured("%s does not appear "
                                   "to be set.", missing_setting)

    def get_backend(self):
        """ Returns the string value of the backend used to instantiate the
        encoder
        """
        return self.mailing_list_backend

    def is_subscribed(self, mailing_list, email):
        """
        Check to see if the email address is on the mailing list specified

        :param mailing_list: integer id for the mailing list
        :param email: email address
        :return: True if the email is on the mailing list, False otherwise.
        """
        log.debug("Checking mailing list %s for email %s...", mailing_list,
                  email)
        result = self.backend.is_subscribed(mailing_list, email)
        if result:
            log.debug("Email %s is on mailinglist %s", email, mailing_list)
        else:
            log.debug("Email %s is not on mailinglist %s", email, mailing_list)
        return result

    def subscribe(self, mailing_list, email):
        """
        Subscribes the user associated to the email address to the specified
        mailing list

        :param mailing_list: list to subscribe to
        :param email: email address for the user
        :return: True if successful, false otherwise
        """
        log.debug("Subscribing %s to mailing list %s...", email, mailing_list)
        result = self.backend.subscribe(mailing_list, email)
        if result:
            log.info("Successfully subscribed %s to mailing list %s.", email,
                     mailing_list)
        else:
            log.info("Unable to subscribe %s to mailing list %s.", email,
                     mailing_list)
        return result

    def unsubscribe(self, mailing_list, email):
        """
        Unsubscribes the user associated to the email address to the specified
        mailing list

        :param mailing_list: list to unsubscribe from
        :param email: email address for the user
        :return: returns True if successful, false otherwise
        """
        log.debug("Unsubscribing %s to mailing list %s...", email, mailing_list)
        result = self.backend.unsubscribe(mailing_list, email)
        if result:
            log.info("Successfully unsubscribed %s to mailing list %s.", email,
                     mailing_list)
        else:
            log.info("Unable to unsubscribe %s from mailing list %s.", email,
                     mailing_list)
        return result

    def create_lead(self, email, extra_values, cookie=None):
        """
        Returns the lead ids for the specified email address, creating a lead
        if it doesn't exist.

        :param email: email address
        :param extra_values: dict of additional values
        :return: an array of lead ids associated to the email
        """
        log.debug("Creating lead for %s with additional detail %s", email,
                  extra_values)
        result = self.backend.create_lead(email, extra_values, cookie)
        if result is not None:
            log.info("Successfully created lead for %s", email)
        else:
            log.info("Unable to create lead for %s", email)
        return result

    def associate_lead(self, lead_id, cookie_id):
        """
        Associates a lead by id to a cookie id
        :param lead_id: integer lead id
        :param cookie_id: cookie id
        :return:
        """
        log.debug("Associating lead %s with cookie %s", lead_id, cookie_id)
        result = self.backend.associate_lead(lead_id, cookie_id)
        if result is not None:
            log.info("Successfully associated lead %s", lead_id)
        else:
            log.info("Unable to associate lead %s", lead_id)
        return result