Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix django ugettextlazy incompatibility #15

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 16 additions & 5 deletions postmark/core.py
Expand Up @@ -26,6 +26,18 @@
except ImportError:
raise Exception('Cannot use python-postmark library without Python 2.6 or greater, or Python 2.4 or 2.5 and the "simplejson" library')

class PMJSONEncoder(json.JSONEncoder):
def default(self, o):
try:
if hasattr(o, '_proxy____str_cast') and callable(o._proxy____str_cast):
return o._proxy____str_cast()
elif hasattr(o, '_proxy____unicode_cast'):
return unicode(o)
except:
pass

return super(PMJSONEncoder, self).default(o)

#
#
__POSTMARK_URL__ = 'http://api.postmarkapp.com/'
Expand Down Expand Up @@ -379,14 +391,13 @@ def send(self, test=None):

# If this is a test, just print the message
if test:
print 'JSON message is:\n%s' % json.dumps(json_message)
print 'JSON message is:\n%s' % json.dumps(json_message, cls=PMJSONEncoder)
return


# Set up the url Request
req = urllib2.Request(
__POSTMARK_URL__ + 'email',
json.dumps(json_message),
json.dumps(json_message, cls=PMJSONEncoder),
{
'Accept': 'application/json',
'Content-Type': 'application/json',
Expand Down Expand Up @@ -464,7 +475,7 @@ def send(self, test=None):

req = urllib2.Request(
__POSTMARK_URL__ + 'email/batch',
json.dumps(json_message),
json.dumps(json_message, cls=PMJSONEncoder),
{
'Accept': 'application/json',
'Content-Type': 'application/json',
Expand All @@ -483,7 +494,7 @@ def send(self, test=None):

# If this is a test, just print the message
if test:
print 'JSON message is:\n%s' % json.dumps(json_message)
print 'JSON message is:\n%s' % json.dumps(json_message, cls=PMJSONEncoder)
return

# Attempt send
Expand Down
3 changes: 2 additions & 1 deletion test/demo/forms.py
Expand Up @@ -2,6 +2,7 @@
from django import forms
from django.conf import settings
from django.core.mail import send_mail
from django.utils.translation import ugettext_lazy

class EmailForm(forms.Form):

Expand All @@ -14,7 +15,7 @@ def save(self, fail_silently=False):
"""
Build and send the email message.
"""
send_mail(subject=self.cleaned_data['subject'],
send_mail(subject=ugettext_lazy(self.cleaned_data['subject']),
message=self.cleaned_data['body'],
from_email=self.cleaned_data['sender'],
recipient_list=[addr.strip() for addr in self.cleaned_data['to'].split(',')],
Expand Down