Skip to content
This repository has been archived by the owner on Jan 31, 2019. It is now read-only.

1.7 support is needed. #41

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mailchimp/chimp.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def __unicode__(self):
def __getattr__(self, attr):
if attr in self._extended_attrs:
return self.info[attr]
raise AttributeError, attr
raise AttributeError(attr)

@property
def interests(self):
Expand Down
2 changes: 1 addition & 1 deletion mailchimp/chimpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from chimpy import Connection
from .chimpy import Connection
31 changes: 16 additions & 15 deletions mailchimp/chimpy/chimpy.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import urllib
import urllib2
import urllib.request
import urllib.parse
import pprint
from utils import transform_datetime
from utils import flatten
from .utils import transform_datetime
from .utils import flatten
from warnings import warn
from django.utils import simplejson
import json as simplejson
_debug = 1


Expand Down Expand Up @@ -34,25 +34,26 @@ def __init__(self, apikey=None, secure=False):
api_host = dc + '.' + api_host

self.url = '%s://%s/%s/' % (proto, api_host, self.version)
self.opener = urllib2.build_opener()
self.opener = urllib.request.build_opener()
self.opener.addheaders = [('Content-Type', 'application/x-www-form-urlencoded')]

def _rpc(self, method, **params):
"""make an rpc call to the server"""

params = urllib.urlencode(params, doseq=True)
params = urllib.parse.urlencode(params, doseq=True).encode('utf8')

if _debug > 1:
print __name__, "making request with parameters"
print (__name__, "making request with parameters")
pprint.pprint(params)
print __name__, "encoded parameters:", params
print (__name__, "encoded parameters:", params)

response = self.opener.open("%s?method=%s" %(self.url, method), params)
data = response.read()
data = response.read().decode('utf8')
response.close()

if _debug > 1:
print __name__, "rpc call received", data
print (__name__, "rpc call received", data)


result = simplejson.loads(data)

Expand Down Expand Up @@ -232,14 +233,14 @@ def list_merge_var_add(self, id, tag, name, req=False):

def list_merge_var_del(self, id, tag):
return self._api_call(method='listMergeVarDel', id=id, tag=tag)

def list_webhooks(self, id):
return self._api_call(method='listWebhooks', id=id)

# public static listWebhookAdd(string apikey, string id, string url, array actions, array sources)
def list_webhook_add(self, id, url, actions, sources):
return self._api_call(method='listWebhookAdd', id=id, url=url, actions=actions, sources=sources)

def list_webhook_del(self, id, url):
return self._api_call(method='listWebhookDel', id=id, url=url)

Expand Down
54 changes: 27 additions & 27 deletions mailchimp/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from django.utils import simplejson
import json as simplejson
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from django.core.urlresolvers import reverse
Expand Down Expand Up @@ -33,15 +33,15 @@ def queue(self, campaign_type, contents, list_id, template_id, subject,
kwargs['object_id'] = obj.pk
kwargs['content_type'] = ContentType.objects.get_for_model(obj)
return self.create(**kwargs)

def dequeue(self, limit=None):
if limit:
qs = self.filter(locked=False)[:limit]
else:
qs = self.filter(locked=False)
for obj in qs:
yield obj.send()
yield obj.send()

def get_or_404(self, *args, **kwargs):
return get_object_or_404(self.model, *args, **kwargs)

Expand Down Expand Up @@ -78,9 +78,9 @@ class Queue(models.Model):
content_object = generic.GenericForeignKey('content_type', 'object_id')
extra_info = models.TextField(null=True)
locked = models.BooleanField(default=False)

objects = QueueManager()

def send(self):
"""
send (schedule) this queued object
Expand All @@ -91,12 +91,12 @@ def send(self):
# aquire lock
self.locked = True
self.save()
# get connection and send the mails
# get connection and send the mails
c = get_connection()
tpl = c.get_template_by_id(self.template_id)
content_data = dict([(str(k), v) for k,v in simplejson.loads(self.contents).items()])
built_template = tpl.build(**content_data)
tracking = {'opens': self.tracking_opens,
tracking = {'opens': self.tracking_opens,
'html_clicks': self.tracking_html_clicks,
'text_clicks': self.tracking_text_clicks}
if self.google_analytics:
Expand Down Expand Up @@ -125,16 +125,16 @@ def send(self):
self.locked = False
self.save()
return False

def get_dequeue_url(self):
return reverse('mailchimp_dequeue', kwargs={'id': self.id})

def get_cancel_url(self):
return reverse('mailchimp_cancel', kwargs={'id': self.id})

def get_list(self):
return get_connection().lists[self.list_id]

@property
def object(self):
"""
Expand All @@ -147,23 +147,23 @@ def object(self):
except model.DoesNotExist:
return None
return None

def get_object_admin_url(self):
if not self.object:
return ''
name = 'admin:%s_%s_change' % (self.object._meta.app_label,
self.object._meta.module_name)
return reverse(name, args=(self.object.pk,))

def can_dequeue(self, user):
if user.is_superuser:
return True
if not user.is_staff:
return False
if callable(getattr(self.object, 'mailchimp_can_dequeue', None)):
return self.object.mailchimp_can_dequeue(user)
return user.has_perm('mailchimp.can_send') and user.has_perm('mailchimp.can_dequeue')
return user.has_perm('mailchimp.can_send') and user.has_perm('mailchimp.can_dequeue')


class CampaignManager(models.Manager):
def create(self, campaign_id, segment_opts, content_type=None, object_id=None,
Expand All @@ -179,11 +179,11 @@ def create(self, campaign_id, segment_opts, content_type=None, object_id=None,
for email in camp.list.filter_members(segment_opts):
Reciever.objects.create(campaign=obj, email=email)
return obj

def get_or_404(self, *args, **kwargs):
return get_object_or_404(self.model, *args, **kwargs)


class DeletedCampaign(object):
subject = u'<deleted from mailchimp>'

Expand All @@ -197,31 +197,31 @@ class Campaign(models.Model):
object_id = models.PositiveIntegerField(null=True, blank=True)
content_object = generic.GenericForeignKey('content_type', 'object_id')
extra_info = models.TextField(null=True)

objects = CampaignManager()

class Meta:
ordering = ['-sent_date']
permissions = [('can_view', 'Can view Mailchimp information'),
('can_send', 'Can send Mailchimp newsletters')]
verbose_name = _('Mailchimp Log')
verbose_name_plural = _('Mailchimp Logs')

def get_absolute_url(self):
return reverse('mailchimp_campaign_info', kwargs={'campaign_id': self.campaign_id})

def get_object_admin_url(self):
if not self.object:
return ''
name = 'admin:%s_%s_change' % (self.object._meta.app_label,
self.object._meta.module_name)
return reverse(name, args=(self.object.pk,))

def get_extra_info(self):
if self.extra_info:
return simplejson.loads(self.extra_info)
return []

@property
def object(self):
"""
Expand All @@ -234,7 +234,7 @@ def object(self):
except model.DoesNotExist:
return None
return None

@property
def mc(self):
try:
Expand All @@ -247,4 +247,4 @@ def mc(self):

class Reciever(models.Model):
campaign = models.ForeignKey(Campaign, related_name='recievers')
email = models.EmailField()
email = models.EmailField()
4 changes: 2 additions & 2 deletions mailchimp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.contrib.contenttypes.models import ContentType
from django.utils import simplejson
import json as simplejson
from django.contrib.auth import logout
from django.contrib.messages import debug, info, success, warning, error, add_message
from django.http import (
Expand Down Expand Up @@ -436,4 +436,4 @@ def is_queued_or_sent(object):
def get_connection():
if not CONNECTION.is_connected:
CONNECTION.connect(API_KEY)
return CONNECTION
return CONNECTION