def connect(self, api_key): self._api_key = api_key self.cache = Cache(api_key) self.warnings = WarningLogger() self.con = self.warnings.proxy(BaseConnection(self._api_key, self._secure)) if self._check and not self.ping(): raise MCConnectionFailed() self.is_connected = True
def connect(self, api_key): self._api_key = api_key self.cache = Cache(api_key) self.warnings = WarningLogger() self.con = self.warnings.proxy( BaseConnection(self._api_key, self._secure)) if self._check: status = self.ping() if status != STATUS_OK: raise MCConnectionFailed(status) self.is_connected = True
def connect(self, api_key): self._api_key = api_key self.cache = Cache(api_key) self.warnings = WarningLogger() self.con = self.warnings.proxy(BaseConnection(self._api_key, self._secure)) if self._check: status = self.ping() if status != STATUS_OK: raise MCConnectionFailed(status) self.is_connected = True
class Connection(object): REGULAR = REGULAR_CAMPAIGN PLAINTEXT = PLAINTEXT_CAMPAIGN ABSPLIT = ABSPLIT_CAMPAIGN RSS = RSS_CAMPAIGN TRANS = TRANS_CAMPAIGN AUTO = AUTO_CAMPAIGN DOES_NOT_EXIST = { 'templates': MCTemplateDoesNotExist, 'campaigns': MCCampaignDoesNotExist, 'lists': MCListDoesNotExist, 'folders': MCFolderDoesNotExist, } def __init__(self, api_key=None, secure=False, check=True): self._secure = secure self._check = check self._api_key = None self.con = None self.is_connected = False if api_key is not None: self.connect(api_key) def connect(self, api_key): self._api_key = api_key self.cache = Cache(api_key) self.warnings = WarningLogger() self.con = self.warnings.proxy(BaseConnection(self._api_key, self._secure)) if self._check: status = self.ping() if status != STATUS_OK: raise MCConnectionFailed(status) self.is_connected = True def ping(self): return self.con.ping() @property def campaigns(self): return self.get_campaigns() def get_campaigns(self): return self.cache.get('campaigns', self._get_categories) @property def lists(self): return self.get_lists() def get_lists(self): return self.cache.get('lists', self._get_lists) @property def templates(self): return self.get_templates() def get_templates(self): return self.cache.get('templates', self._get_templates) def _get_categories(self): return build_dict(self, Campaign, self.con.campaigns()['data']) def _get_lists(self): return build_dict(self, List, self.con.lists()) def _get_templates(self): templates = self.con.campaign_templates() for t in templates: t.update(self.con.template_info(template_id=t['id'])) return build_dict(self, Template, templates) @property def folders(self): return self.get_folders() def get_folders(self): return self.cache.get('folders', self._get_folders) def _get_folders(self): return build_dict(self, Folder, self.con.folders(), key='folder_id') def get_list_by_id(self, id): return self._get_by_id('lists', id) def get_campaign_by_id(self, id): return self._get_by_id('campaigns', id) def get_template_by_id(self, id): return self._get_by_id('templates', id) def get_template_by_name(self, name): return self._get_by_key('templates', 'name', name) def get_folder_by_id(self, id): return self._get_by_id('folders', id) def get_folder_by_name(self, name): return self._get_by_key('folders', 'name', name) def _get_by_id(self, thing, id): try: return getattr(self, thing)[id] except KeyError: self.cache.flush(thing) try: return getattr(self, thing)[id] except KeyError: raise self.DOES_NOT_EXIST[thing](id) def _get_by_key(self, thing, name, key): for id, obj in getattr(self, thing).items(): if getattr(obj, name) == key: return obj raise self.DOES_NOT_EXIST[thing]('%s=%s' % (name, key)) def create_campaign(self, campaign_type, campaign_list, template, subject, from_email, from_name, to_name, folder_id=None, tracking=None, title='', authenticate=False, analytics=None, auto_footer=False, generate_text=False, auto_tweet=False, segment_opts=None, type_opts=None): """ Creates a new campaign and returns it for the arguments given. """ tracking = tracking or {'opens':True, 'html_clicks': True} type_opts = type_opts or {} segment_opts = segment_opts or {} analytics = analytics or {} options = {} if title: options['title'] = title else: options['title'] = subject options['list_id'] = campaign_list.id options['template_id'] = template.id options['subject'] = subject options['from_email'] = from_email options['from_name'] = from_name options['to_name'] = to_name if folder_id: options['folder_id'] = folder_id options['tracking'] = tracking options['authenticate'] = bool(authenticate) if analytics: options['analytics'] = analytics options['auto_footer'] = bool(auto_footer) options['generate_text'] = bool(generate_text) options['auto_tweet'] = bool(auto_tweet) content = dict(template) kwargs = {} if segment_opts.get('conditions', None): kwargs['segment_opts'] = segment_opts if type_opts: kwargs['type_opts'] = type_opts cid = self.con.campaign_create(campaign_type, options, content, **kwargs) camp = self.get_campaign_by_id(cid) camp.template_object = template return camp def queue(self, campaign_type, contents, list_id, template_id, subject, from_email, from_name, to_name, folder_id=None, tracking_opens=True, tracking_html_clicks=True, tracking_text_clicks=False, title=None, authenticate=False, google_analytics=None, auto_footer=False, auto_tweet=False, segment_options=False, segment_options_all=True, segment_options_conditions=None, type_opts=None, obj=None): from mailchimp.models import Queue segment_options_conditions = segment_options_conditions or [] type_opts = type_opts or {} kwargs = locals().copy() del kwargs['Queue'] del kwargs['self'] return Queue.objects.queue(**kwargs)
class Connection(object): REGULAR = REGULAR_CAMPAIGN PLAINTEXT = PLAINTEXT_CAMPAIGN ABSPLIT = ABSPLIT_CAMPAIGN RSS = RSS_CAMPAIGN TRANS = TRANS_CAMPAIGN AUTO = AUTO_CAMPAIGN DOES_NOT_EXIST = { 'templates': MCTemplateDoesNotExist, 'campaigns': MCCampaignDoesNotExist, 'lists': MCListDoesNotExist, 'folders': MCFolderDoesNotExist, } def __init__(self, api_key=None, secure=False, check=True): self._secure = secure self._check = check self._api_key = None self.con = None self.is_connected = False if api_key is not None: self.connect(api_key) def connect(self, api_key): self._api_key = api_key self.cache = Cache(api_key) self.warnings = WarningLogger() self.con = self.warnings.proxy( BaseConnection(self._api_key, self._secure)) if self._check: status = self.ping() if status != STATUS_OK: raise MCConnectionFailed(status) self.is_connected = True def ping(self): return self.con.ping() @property def campaigns(self): return self.get_campaigns() def get_campaigns(self): return self.cache.get('campaigns', self._get_categories) @property def lists(self): return self.get_lists() def get_lists(self): return self.cache.get('lists', self._get_lists) @property def templates(self): return self.get_templates() def get_templates(self): return self.cache.get('templates', self._get_templates) def _get_categories(self): return build_dict(self, Campaign, self.con.campaigns()['data']) def _get_lists(self): return build_dict(self, List, self.con.lists()) def _get_templates(self): templates = self.con.campaign_templates() for t in templates: t.update(self.con.template_info(template_id=t['id'])) return build_dict(self, Template, templates) @property def folders(self): return self.get_folders() def get_folders(self): return self.cache.get('folders', self._get_folders) def _get_folders(self): return build_dict(self, Folder, self.con.folders(), key='folder_id') def get_list_by_id(self, id): return self._get_by_id('lists', id) def get_campaign_by_id(self, id): return self._get_by_id('campaigns', id) def get_template_by_id(self, id): return self._get_by_id('templates', id) def get_template_by_name(self, name): return self._get_by_key('templates', 'name', name) def get_folder_by_id(self, id): return self._get_by_id('folders', id) def get_folder_by_name(self, name): return self._get_by_key('folders', 'name', name) def _get_by_id(self, thing, id): try: return getattr(self, thing)[id] except KeyError: self.cache.flush(thing) try: return getattr(self, thing)[id] except KeyError: raise self.DOES_NOT_EXIST[thing](id) def _get_by_key(self, thing, name, key): for id, obj in getattr(self, thing).items(): if getattr(obj, name) == key: return obj raise self.DOES_NOT_EXIST[thing]('%s=%s' % (name, key)) def create_campaign(self, campaign_type, campaign_list, template, subject, from_email, from_name, to_name, folder_id=None, tracking=None, title='', authenticate=False, analytics=None, auto_footer=False, generate_text=False, auto_tweet=False, segment_opts=None, type_opts=None): """ Creates a new campaign and returns it for the arguments given. """ tracking = tracking or {'opens': True, 'html_clicks': True} type_opts = type_opts or {} segment_opts = segment_opts or {} analytics = analytics or {} options = {} if title: options['title'] = title else: options['title'] = subject options['list_id'] = campaign_list.id options['template_id'] = template.id options['subject'] = subject options['from_email'] = from_email options['from_name'] = from_name options['to_name'] = to_name if folder_id: options['folder_id'] = folder_id options['tracking'] = tracking options['authenticate'] = bool(authenticate) if analytics: options['analytics'] = analytics options['auto_footer'] = bool(auto_footer) options['generate_text'] = bool(generate_text) options['auto_tweet'] = bool(auto_tweet) content = dict(template) kwargs = {} if segment_opts.get('conditions', None): kwargs['segment_opts'] = segment_opts if type_opts: kwargs['type_opts'] = type_opts cid = self.con.campaign_create(campaign_type, options, content, **kwargs) camp = self.get_campaign_by_id(cid) camp.template_object = template return camp def queue(self, campaign_type, contents, list_id, template_id, subject, from_email, from_name, to_name, folder_id=None, tracking_opens=True, tracking_html_clicks=True, tracking_text_clicks=False, title=None, authenticate=False, google_analytics=None, auto_footer=False, auto_tweet=False, segment_options=False, segment_options_all=True, segment_options_conditions=None, type_opts=None, obj=None): from mailchimp.models import Queue segment_options_conditions = segment_options_conditions or [] type_opts = type_opts or {} kwargs = locals().copy() del kwargs['Queue'] del kwargs['self'] return Queue.objects.queue(**kwargs)
class Connection(object): REGULAR = REGULAR_CAMPAIGN PLAINTEXT = PLAINTEXT_CAMPAIGN ABSPLIT = ABSPLIT_CAMPAIGN RSS = RSS_CAMPAIGN TRANS = TRANS_CAMPAIGN AUTO = AUTO_CAMPAIGN DOES_NOT_EXIST = { "templates": MCTemplateDoesNotExist, "campaigns": MCCampaignDoesNotExist, "lists": MCListDoesNotExist, } def __init__(self, api_key=None, secure=False, check=True): self._secure = secure self._check = check self._api_key = None self.con = None self.is_connected = False if api_key is not None: self.connect(api_key) def connect(self, api_key): self._api_key = api_key self.cache = Cache(api_key) self.warnings = WarningLogger() self.con = self.warnings.proxy(BaseConnection(self._api_key, self._secure)) if self._check: status = self.ping() if status != STATUS_OK: raise MCConnectionFailed(status) self.is_connected = True def ping(self): return self.con.ping() @property def campaigns(self): return self.get_campaigns() def get_campaigns(self): return self.cache.get("campaigns", self._get_categories) @property def lists(self): return self.get_lists() def get_lists(self): return self.cache.get("lists", self._get_lists) @property def templates(self): return self.get_templates() def get_templates(self): return self.cache.get("templates", self._get_templates) def _get_categories(self): return build_dict(self, Campaign, self.con.campaigns()) def _get_lists(self): return build_dict(self, List, self.con.lists()) def _get_templates(self): return build_dict(self, Template, self.con.campaign_templates()) def get_list_by_id(self, id): return self._get_by_id("lists", id) def get_campaign_by_id(self, id): return self._get_by_id("campaigns", id) def get_template_by_id(self, id): return self._get_by_id("templates", id) def get_template_by_name(self, name): return self._get_by_key("templates", "name", name) def _get_by_id(self, thing, id): try: return getattr(self, thing)[id] except KeyError: self.cache.flush(thing) try: return getattr(self, thing)[id] except KeyError: raise self.DOES_NOT_EXIST[thing](id) def _get_by_key(self, thing, name, key): for id, obj in getattr(self, thing).items(): if getattr(obj, name) == key: return obj raise self.DOES_NOT_EXIST[thing]("%s=%s" % (name, key)) def create_campaign( self, campaign_type, campaign_list, template, subject, from_email, from_name, to_email, folder_id=None, tracking={"opens": True, "html_clicks": True}, title="", authenticate=False, analytics={}, auto_footer=False, generate_text=False, auto_tweet=False, segment_opts={}, type_opts={}, ): """ Creates a new campaign and returns it for the arguments given. """ options = {} if title: options["title"] = title else: options["title"] = subject options["list_id"] = campaign_list.id options["template_id"] = template.id options["subject"] = subject options["from_email"] = from_email options["from_name"] = from_name options["to_email"] = to_email if folder_id: options["folder_id"] = folder_id options["tracking"] = tracking options["authenticate"] = bool(authenticate) if analytics: options["analytics"] = analytics options["auto_footer"] = bool(auto_footer) options["generate_text"] = bool(generate_text) options["auto_tweet"] = bool(auto_tweet) content = dict(template) kwargs = {} if segment_opts.get("conditions", None): kwargs["segment_opts"] = segment_opts if type_opts: kwargs["type_opts"] = type_opts cid = self.con.campaign_create(campaign_type, options, content, **kwargs) camp = self.get_campaign_by_id(cid) camp.template_object = template return camp def queue( self, campaign_type, contents, list_id, template_id, subject, from_email, from_name, to_email, folder_id=None, tracking_opens=True, tracking_html_clicks=True, tracking_text_clicks=False, title=None, authenticate=False, google_analytics=None, auto_footer=False, auto_tweet=False, segment_options=False, segment_options_all=True, segment_options_conditions=[], type_opts={}, obj=None, ): from mailchimp.models import Queue kwargs = locals().copy() del kwargs["Queue"] del kwargs["self"] return Queue.objects.queue(**kwargs)