From f06ebbe78002746adb62a2bce69db2c884aad31f Mon Sep 17 00:00:00 2001 From: Martin Massera Date: Thu, 23 Nov 2017 11:56:23 -0300 Subject: [PATCH 1/9] enconding sender and recipient name as ascii in django<1.9 --- newsletter/models.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/newsletter/models.py b/newsletter/models.py index 737edd0d..924c9331 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -1,5 +1,6 @@ import logging import time +import django from django.conf import settings from django.contrib.sites.models import Site @@ -15,6 +16,8 @@ from django.utils.timezone import now from sorl.thumbnail import ImageField +from distutils.version import LooseVersion + from .compat import get_context, reverse from .utils import ( @@ -138,7 +141,7 @@ def archive_url(self): ) def get_sender(self): - return u'%s <%s>' % (self.sender, self.email) + return get_address(self.sender, self.email) def get_subscriptions(self): logger.debug(u'Looking up subscribers for %s', self) @@ -242,7 +245,6 @@ def _unsubscribe(self): def save(self, *args, **kwargs): """ Perform some basic validation and state maintenance of Subscription. - TODO: Move this code to a more suitable place (i.e. `clean()`) and cleanup the code. Refer to comment below and https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.clean @@ -342,10 +344,7 @@ class Meta: unique_together = ('user', 'email_field', 'newsletter') def get_recipient(self): - if self.name: - return u'%s <%s>' % (self.name, self.email) - - return u'%s' % (self.email) + return get_address(self.name, self.email) def send_activation_email(self, action): assert action in ACTIONS, 'Unknown action: %s' % action @@ -733,3 +732,11 @@ def get_absolute_url(self): default=False, verbose_name=_('sending'), db_index=True, editable=False ) + +def get_address(name, email): + if LooseVersion(django.get_version()) < LooseVersion('1.9'): + name = name.encode('ascii', 'ignore').decode('ascii').strip() + if name: + return u'%s <%s>' % (name, email) + else: + return u'%s' % email \ No newline at end of file From 2e69dd718635fef0e9b597d7690efaf658f038e5 Mon Sep 17 00:00:00 2001 From: newearthmartin Date: Mon, 26 Feb 2018 17:25:39 +0300 Subject: [PATCH 2/9] comment for when django 1.8 support is dropped --- newsletter/models.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/newsletter/models.py b/newsletter/models.py index 924c9331..5a0c6a82 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -734,9 +734,11 @@ def get_absolute_url(self): ) def get_address(name, email): + # Converting name to ascii for compatibility with django < 1.9. + # Remove this when django 1.8 is no longer supported. if LooseVersion(django.get_version()) < LooseVersion('1.9'): name = name.encode('ascii', 'ignore').decode('ascii').strip() if name: return u'%s <%s>' % (name, email) else: - return u'%s' % email \ No newline at end of file + return u'%s' % email From 0a6f9a658fc8576e823e446c0d906cfae72c2206 Mon Sep 17 00:00:00 2001 From: Martin Massera Date: Sat, 7 Apr 2018 08:48:56 -0300 Subject: [PATCH 3/9] setting default newsletter in the field definition --- newsletter/models.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/newsletter/models.py b/newsletter/models.py index 19be8c4a..9027ed63 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -476,7 +476,7 @@ class Message(models.Model): slug = models.SlugField(verbose_name=_('slug')) newsletter = models.ForeignKey( - Newsletter, verbose_name=_('newsletter'), on_delete=models.CASCADE + Newsletter, verbose_name=_('newsletter'), on_delete=models.CASCADE, default=Newsletter.get_default ) date_create = models.DateTimeField( @@ -501,11 +501,6 @@ def __str__(self): logger.warning('No newsletter has been set for this message yet.') return self.title - def save(self, **kwargs): - if self.pk is None: - self.newsletter = Newsletter.get_default() - super(Message, self).save(**kwargs) - def get_next_article_sortorder(self): """ Get next available sortorder for Article. """ From f9091e2cef02f70acb88dc23ec88cf090f7ff52c Mon Sep 17 00:00:00 2001 From: Martin Massera Date: Sat, 7 Apr 2018 09:13:53 -0300 Subject: [PATCH 4/9] fixing tests --- tests/test_mailing.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_mailing.py b/tests/test_mailing.py index 66e76330..adf77ac5 100644 --- a/tests/test_mailing.py +++ b/tests/test_mailing.py @@ -106,8 +106,8 @@ class MessageTestCase(MailingTestCase): def test_message_str(self): m1 = Message(title='Test message', slug='test-message') with patch_logger('newsletter.models', 'warning') as warnings: - self.assertEqual(six.text_type(m1), "Test message") - self.assertEqual(len(warnings), 1) + self.assertEqual(six.text_type(m1), "Test message in Test newsletter") + self.assertEqual(len(warnings), 0) m2 = Message.objects.create( title='Test message str', From 6181492052b792539ebc458beb4958c0d7855769 Mon Sep 17 00:00:00 2001 From: Martin Massera Date: Sat, 7 Apr 2018 10:45:51 -0300 Subject: [PATCH 5/9] adding migration --- newsletter/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/newsletter/models.py b/newsletter/models.py index 9027ed63..94a3870f 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -468,6 +468,9 @@ def save(self, **kwargs): super(Article, self).save() +def get_default_newsletter(): + return Newsletter.get_default() + @python_2_unicode_compatible class Message(models.Model): """ Message as sent through a Submission. """ @@ -476,7 +479,7 @@ class Message(models.Model): slug = models.SlugField(verbose_name=_('slug')) newsletter = models.ForeignKey( - Newsletter, verbose_name=_('newsletter'), on_delete=models.CASCADE, default=Newsletter.get_default + Newsletter, verbose_name=_('newsletter'), on_delete=models.CASCADE, default=get_default_newsletter ) date_create = models.DateTimeField( From 5de7f8eef996724ff077d21b635d20df0d988df7 Mon Sep 17 00:00:00 2001 From: Martin Massera Date: Sat, 7 Apr 2018 10:46:29 -0300 Subject: [PATCH 6/9] adding migration --- .../migrations/0004_auto_20180407_1043.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 newsletter/migrations/0004_auto_20180407_1043.py diff --git a/newsletter/migrations/0004_auto_20180407_1043.py b/newsletter/migrations/0004_auto_20180407_1043.py new file mode 100644 index 00000000..fbf6cfbd --- /dev/null +++ b/newsletter/migrations/0004_auto_20180407_1043.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import newsletter.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('newsletter', '0003_auto_20160226_1518'), + ] + + operations = [ + migrations.AlterField( + model_name='message', + name='newsletter', + field=models.ForeignKey(default=newsletter.models.get_default_newsletter, verbose_name='newsletter', to='newsletter.Newsletter'), + ), + ] From 21a7a9ca87795529b0cced8f5eb86016b28574f4 Mon Sep 17 00:00:00 2001 From: Martin Massera Date: Sat, 7 Apr 2018 11:23:29 -0300 Subject: [PATCH 7/9] adding missing on_delete to migration --- newsletter/migrations/0004_auto_20180407_1043.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/newsletter/migrations/0004_auto_20180407_1043.py b/newsletter/migrations/0004_auto_20180407_1043.py index fbf6cfbd..d0319648 100644 --- a/newsletter/migrations/0004_auto_20180407_1043.py +++ b/newsletter/migrations/0004_auto_20180407_1043.py @@ -15,6 +15,9 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='message', name='newsletter', - field=models.ForeignKey(default=newsletter.models.get_default_newsletter, verbose_name='newsletter', to='newsletter.Newsletter'), + field=models.ForeignKey(default=newsletter.models.get_default_newsletter, + verbose_name='newsletter', + to='newsletter.Newsletter', + on_delete=models.CASCADE), ), ] From 6daf547c19892f42ba7cdd3245c4442c2b6e89d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marti=CC=81n=20Massera?= Date: Tue, 17 Sep 2019 17:21:16 -0300 Subject: [PATCH 8/9] removing invalid characters from address for sending --- newsletter/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/newsletter/models.py b/newsletter/models.py index 9d2a7cd4..66eaf99b 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -714,6 +714,8 @@ def get_address(name, email): if LooseVersion(django.get_version()) < LooseVersion('1.9'): name = name.encode('ascii', 'ignore').decode('ascii').strip() if name: + name = name.replace('@', '')\ + .replace(',', '') return u'%s <%s>' % (name, email) else: return u'%s' % email From 2d5602c75e7a6ab9a2ebd0b230416e35622daf2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marti=CC=81n=20Massera?= Date: Tue, 17 Sep 2019 22:05:09 -0300 Subject: [PATCH 9/9] checking for ; also --- newsletter/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/newsletter/models.py b/newsletter/models.py index 66eaf99b..a2e37ae6 100644 --- a/newsletter/models.py +++ b/newsletter/models.py @@ -715,7 +715,8 @@ def get_address(name, email): name = name.encode('ascii', 'ignore').decode('ascii').strip() if name: name = name.replace('@', '')\ - .replace(',', '') + .replace(',', '')\ + .replace(';', '') return u'%s <%s>' % (name, email) else: return u'%s' % email