diff --git a/mailchimp/chimp.py b/mailchimp/chimp.py index 1270cbf..743ad7b 100644 --- a/mailchimp/chimp.py +++ b/mailchimp/chimp.py @@ -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): diff --git a/mailchimp/chimpy/__init__.py b/mailchimp/chimpy/__init__.py index ba997a6..7a9a94a 100644 --- a/mailchimp/chimpy/__init__.py +++ b/mailchimp/chimpy/__init__.py @@ -1 +1 @@ -from chimpy import Connection +from .chimpy import Connection diff --git a/mailchimp/chimpy/chimpy.py b/mailchimp/chimpy/chimpy.py index be48db3..57ce237 100644 --- a/mailchimp/chimpy/chimpy.py +++ b/mailchimp/chimpy/chimpy.py @@ -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 @@ -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) @@ -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) diff --git a/mailchimp/models.py b/mailchimp/models.py index 92ce38a..2bb11be 100644 --- a/mailchimp/models.py +++ b/mailchimp/models.py @@ -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 @@ -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) @@ -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 @@ -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: @@ -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): """ @@ -147,14 +147,14 @@ 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 @@ -162,8 +162,8 @@ def can_dequeue(self, user): 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, @@ -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'' @@ -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): """ @@ -234,7 +234,7 @@ def object(self): except model.DoesNotExist: return None return None - + @property def mc(self): try: @@ -247,4 +247,4 @@ def mc(self): class Reciever(models.Model): campaign = models.ForeignKey(Campaign, related_name='recievers') - email = models.EmailField() \ No newline at end of file + email = models.EmailField() diff --git a/mailchimp/utils.py b/mailchimp/utils.py index 82e77de..547afaf 100644 --- a/mailchimp/utils.py +++ b/mailchimp/utils.py @@ -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 ( @@ -436,4 +436,4 @@ def is_queued_or_sent(object): def get_connection(): if not CONNECTION.is_connected: CONNECTION.connect(API_KEY) - return CONNECTION \ No newline at end of file + return CONNECTION