Skip to content

Commit

Permalink
Merge pull request jazzband#155 from claudep/python3_syntax
Browse files Browse the repository at this point in the history
Fixed Python 3 syntax and API issues
  • Loading branch information
dokterbob committed Jan 3, 2016
2 parents 811f732 + 450a567 commit c6c3826
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions newsletter/admin_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def parse_vcard(myfile, newsletter, ignore_errors=False):

try:
myvcards = vobject.readComponents(myfile)
except vobject.VObjectError, e:
except vobject.VObjectError as e:
raise forms.ValidationError(
_(u"Error reading vCard file: %s" % e)
)
Expand Down Expand Up @@ -332,7 +332,7 @@ def handle(self, dn, entry):
try:
myparser = AddressParser(myfile)
myparser.parse()
except ValueError, e:
except ValueError as e:
if not ignore_errors:
raise forms.ValidationError(e)

Expand Down
6 changes: 3 additions & 3 deletions newsletter/admin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
except ImportError: # Django < 1.7
from django.contrib.admin.util import unquote

from django.utils.encoding import force_unicode
from django.utils.encoding import force_text


class ExtendibleModelAdminMixin(object):
Expand All @@ -29,8 +29,8 @@ def _getobj(self, request, object_id):
'%(name)s object with primary key '
'%(key)r does not exist.'
) % {
'name': force_unicode(opts.verbose_name),
'key': unicode(object_id)
'name': force_text(opts.verbose_name),
'key': force_text(object_id)
}
)

Expand Down
6 changes: 3 additions & 3 deletions newsletter/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Migration(migrations.Migration):
('title', models.CharField(max_length=200, verbose_name='title')),
('text', models.TextField(verbose_name='text')),
('url', models.URLField(null=True, verbose_name='link', blank=True)),
('image', sorl.thumbnail.fields.ImageField(upload_to=b'newsletter/images/%Y/%m/%d', null=True, verbose_name='image', blank=True)),
('image', sorl.thumbnail.fields.ImageField(upload_to='newsletter/images/%Y/%m/%d', null=True, verbose_name='image', blank=True)),
],
options={
'ordering': ('sortorder',),
Expand Down Expand Up @@ -88,8 +88,8 @@ class Migration(migrations.Migration):
name='Subscription',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('name_field', models.CharField(db_column=b'name', max_length=30, blank=True, help_text='optional', null=True, verbose_name='name')),
('email_field', models.EmailField(db_column=b'email', max_length=75, blank=True, null=True, verbose_name='e-mail', db_index=True)),
('name_field', models.CharField(db_column='name', max_length=30, blank=True, help_text='optional', null=True, verbose_name='name')),
('email_field', models.EmailField(db_column='email', max_length=75, blank=True, null=True, verbose_name='e-mail', db_index=True)),
('ip', models.IPAddressField(null=True, verbose_name='IP address', blank=True)),
('create_date', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
('activation_code', models.CharField(default=newsletter.utils.make_activation_code, max_length=40, verbose_name='activation code')),
Expand Down
6 changes: 3 additions & 3 deletions newsletter/migrations/0002_auto_20150416_1555.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='subscription',
name='email_field',
field=models.EmailField(db_column=b'email', max_length=254, blank=True, null=True, verbose_name='e-mail', db_index=True),
field=models.EmailField(db_column='email', max_length=254, blank=True, null=True, verbose_name='e-mail', db_index=True),
),
]

Expand All @@ -31,8 +31,8 @@ class Migration(migrations.Migration):
migrations.AlterModelManagers(
name='newsletter',
managers=[
(b'objects', django.db.models.manager.Manager()),
(b'on_site', django.contrib.sites.managers.CurrentSiteManager()),
('objects', django.db.models.manager.Manager()),
('on_site', django.contrib.sites.managers.CurrentSiteManager()),
],
),
migrations.AlterField(
Expand Down
18 changes: 12 additions & 6 deletions newsletter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from django.db.models import permalink
from django.template import Context
from django.template.loader import select_template
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext
from django.utils.timezone import now
Expand All @@ -31,6 +32,7 @@
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')


@python_2_unicode_compatible
class Newsletter(models.Model):
site = models.ManyToManyField(Site, default=get_default_sites)

Expand Down Expand Up @@ -99,7 +101,7 @@ def get_templates(self, action):

return (subject_template, text_template, html_template)

def __unicode__(self):
def __str__(self):
return self.title

class Meta:
Expand Down Expand Up @@ -157,6 +159,7 @@ def get_default(cls):
return None


@python_2_unicode_compatible
class Subscription(models.Model):
user = models.ForeignKey(
AUTH_USER_MODEL, blank=True, null=True, verbose_name=_('user')
Expand Down Expand Up @@ -323,7 +326,7 @@ def save(self, *args, **kwargs):
verbose_name=_("unsubscribe date"), null=True, blank=True
)

def __unicode__(self):
def __str__(self):
if self.name:
return _(u"%(name)s <%(email)s> to %(newsletter)s") % {
'name': self.name,
Expand Down Expand Up @@ -420,6 +423,7 @@ def update_activate_url(self):
})


@python_2_unicode_compatible
class Article(models.Model):
"""
An Article within a Message which will be send through a Submission.
Expand Down Expand Up @@ -470,7 +474,7 @@ class Meta:
verbose_name = _('article')
verbose_name_plural = _('articles')

def __unicode__(self):
def __str__(self):
return self.title

def save(self):
Expand All @@ -480,6 +484,7 @@ def save(self):
super(Article, self).save()


@python_2_unicode_compatible
class Message(models.Model):
""" Message as sent through a Submission. """

Expand All @@ -497,7 +502,7 @@ class Message(models.Model):
verbose_name=_('modified'), auto_now=True, editable=False
)

def __unicode__(self):
def __str__(self):
try:
return _(u"%(title)s in %(newsletter)s") % {
'title': self.title,
Expand Down Expand Up @@ -529,6 +534,7 @@ def get_default(cls):
return None


@python_2_unicode_compatible
class Submission(models.Model):
"""
Submission represents a particular Message as it is being submitted
Expand All @@ -539,7 +545,7 @@ class Meta:
verbose_name = _('submission')
verbose_name_plural = _('submissions')

def __unicode__(self):
def __str__(self):
return _(u"%(newsletter)s on %(publish_date)s") % {
'newsletter': self.message,
'publish_date': self.publish_date
Expand Down Expand Up @@ -602,7 +608,7 @@ def submit(self):

message.send()

except Exception, e:
except Exception as e:
# TODO: Test coverage for this branch.
logger.error(
ugettext(u'Message %(subscription)s failed '
Expand Down
5 changes: 3 additions & 2 deletions newsletter/tests/test_mailing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from django.core import mail

from django.utils.six.moves import range
from django.utils.timezone import now

from ..models import (
Expand Down Expand Up @@ -80,7 +81,7 @@ def test_sortorder_defaults(self):
total = 3

last = 0
for current in xrange(total):
for current in range(total):
a = self.make_article()
if last:
self.assert_(a.sortorder > last)
Expand Down Expand Up @@ -243,7 +244,7 @@ def test_subscribe_unsubscribe(self):
self.assertFalse(s.unsubscribe_date)

# Repeat this to ensure consequencentness
for x in xrange(2):
for x in range(2):
s.subscribed = True
s.save()

Expand Down
3 changes: 2 additions & 1 deletion newsletter/tests/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from django.core.urlresolvers import reverse

from django.utils import timezone
from django.utils.encoding import force_text

from django.test.utils import override_settings, patch_logger

Expand Down Expand Up @@ -483,7 +484,7 @@ def test_unsubscribe_not_subscribed_view(self):

self.assertIn(
'You are not subscribed to',
unicode(list(response.context['messages'])[0])
force_text(list(response.context['messages'])[0])
)

def test_unsubscribe_post(self):
Expand Down
5 changes: 3 additions & 2 deletions newsletter/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.utils.hashcompat import sha_constructor as sha1

from django.contrib.sites.models import Site
from django.utils.encoding import force_bytes

from datetime import datetime

Expand All @@ -34,12 +35,12 @@ def get_user_model():
def make_activation_code():
""" Generate a unique activation code. """
random_string = str(random.random())
random_digest = sha1(random_string).hexdigest()[:5]
random_digest = sha1(force_bytes(random_string)).hexdigest()[:5]
time_string = str(datetime.now().microsecond)

combined_string = random_digest + time_string

return sha1(combined_string).hexdigest()
return sha1(force_bytes(combined_string)).hexdigest()


def get_default_sites():
Expand Down
4 changes: 2 additions & 2 deletions newsletter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def get_template_names(self):
else:
try:
return [self.template_name % {'action': self.action}]
except KeyError, e:
except KeyError as e:
raise ImproperlyConfigured(
'%(class_name)s inherits from ActionMixin and can contain '
'%%(action)s in template_name to be replaced '
Expand Down Expand Up @@ -414,7 +414,7 @@ def form_valid(self, form):
try:
self.subscription.send_activation_email(action=self.action)

except (SMTPException, socket.error), e:
except (SMTPException, socket.error) as e:
logger.exception(
'Error %s while submitting email to %s.',
e, self.subscription.email
Expand Down

0 comments on commit c6c3826

Please sign in to comment.