diff --git a/.flake8 b/.flake8 new file mode 100644 index 0000000..e200917 --- /dev/null +++ b/.flake8 @@ -0,0 +1,4 @@ +[flake8] +ignore = E731, W503 +max-line-length = 100 +exclude = migrations diff --git a/birdsong/backends/smtp.py b/birdsong/backends/smtp.py index 9adb497..e7b8d5f 100644 --- a/birdsong/backends/smtp.py +++ b/birdsong/backends/smtp.py @@ -2,7 +2,6 @@ import logging from smtplib import SMTPException from threading import Thread -from django.core.mail import send_mass_mail from django.db import close_old_connections, transaction from django.template.loader import render_to_string from django.utils import timezone @@ -26,15 +25,17 @@ class SendCampaignThread(Thread): try: logger.info(f"Sending {len(self.messages)} emails") send_mass_html_mail(self.messages) - logger.info(f"Emails finsihed sending") + logger.info("Emails finished sending") with transaction.atomic(): Campaign.objects.filter(pk=self.campaign_pk).update( status=CampaignStatus.SENT, sent_date=timezone.now(), ) - fresh_contacts = Contact.objects.filter(pk__in=self.contact_pks) - Campaign.objects.get(pk=self.campaign_pk).receipts.add(*fresh_contacts) - except SMTPException as e: + fresh_contacts = Contact.objects.filter( + pk__in=self.contact_pks) + Campaign.objects.get( + pk=self.campaign_pk).receipts.add(*fresh_contacts) + except SMTPException: logger.exception(f"Problem sending campaign: {self.campaign_pk}") self.campaign.status = CampaignStatus.FAILED finally: @@ -58,5 +59,6 @@ class SMTPEmailBackend(BaseEmailBackend): 'reply_to': [self.reply_to], }) - campaign_thread = SendCampaignThread(campaign.pk, [c.pk for c in contacts], messages) + campaign_thread = SendCampaignThread( + campaign.pk, [c.pk for c in contacts], messages) campaign_thread.start() diff --git a/birdsong/blocks.py b/birdsong/blocks.py index e9032e6..b4dbfba 100644 --- a/birdsong/blocks.py +++ b/birdsong/blocks.py @@ -10,5 +10,6 @@ class UnwrappedStreamBlock(blocks.StreamBlock): class DefaultBlocks(UnwrappedStreamBlock): - rich_text = blocks.RichTextBlock(template='birdsong/mail/blocks/richtext.html', + rich_text = blocks.RichTextBlock( + template='birdsong/mail/blocks/richtext.html', features=['h3', 'h4', 'bold', 'italic', 'link', 'ul', 'ol', 'document-link']) diff --git a/birdsong/models.py b/birdsong/models.py index 8ce2a3a..b94cbcc 100644 --- a/birdsong/models.py +++ b/birdsong/models.py @@ -1,19 +1,14 @@ import uuid -from django.conf import settings from django.db import models from modelcluster.contrib.taggit import ClusterTaggableManager from modelcluster.fields import ParentalKey from modelcluster.models import ClusterableModel from taggit.models import TaggedItemBase from wagtail.admin.edit_handlers import FieldPanel -from wagtail.contrib.settings.models import BaseSetting, register_setting from wagtail.core.models import Site from wagtail.core.utils import camelcase_to_underscore -from .backends import BaseEmailBackend -from .blocks import DefaultBlocks - class ContactTag(TaggedItemBase): content_object = ParentalKey( diff --git a/birdsong/options.py b/birdsong/options.py index b4b6537..87f24ef 100644 --- a/birdsong/options.py +++ b/birdsong/options.py @@ -1,8 +1,6 @@ from django.conf.urls import url from django.forms import modelform_factory from django.http.response import HttpResponseRedirect -from django.urls import reverse -from wagtail.admin.edit_handlers import ObjectList, TabbedInterface from wagtail.contrib.modeladmin.helpers import AdminURLHelper, ButtonHelper from wagtail.contrib.modeladmin.options import ModelAdmin @@ -73,6 +71,7 @@ class CampaignAdmin(ModelAdmin): def get_admin_urls_for_registration(self): urls = super().get_admin_urls_for_registration() + def gen_url(pattern, view, name=None): if not name: name = pattern @@ -97,7 +96,6 @@ class CampaignAdmin(ModelAdmin): contact = self.contact_class.objects.first() return editor_views.preview(request, campaign, contact) - def confirm_send(self, request, instance_pk): campaign = self.model.objects.get(pk=instance_pk) form = self.build_sending_form() @@ -128,7 +126,7 @@ class CampaignAdmin(ModelAdmin): return actions.send_campaign(self.backend, request, campaign, contacts) def create_contact_form(self, data=None): - ContactForm = modelform_factory(self.contact_class, exclude=['id']) + ContactForm = modelform_factory(self.contact_class, exclude=['id']) if data: return ContactForm(data) return ContactForm() @@ -153,7 +151,7 @@ class CampaignAdmin(ModelAdmin): # Create fake contact, send test email contact = form.save(commit=False) return actions.send_test(self.backend, request, campaign, contact) - + def copy(self, request, instance_pk): instance = self.model.objects.get(pk=instance_pk) instance.name = instance.name + ' (Copy)' diff --git a/birdsong/views/actions.py b/birdsong/views/actions.py index b56f58c..592a0ff 100644 --- a/birdsong/views/actions.py +++ b/birdsong/views/actions.py @@ -1,9 +1,8 @@ from django.contrib import messages from django.shortcuts import redirect -from django.utils import timezone from wagtail.contrib.modeladmin.helpers.url import AdminURLHelper -from birdsong.models import CampaignStatus, Contact, Receipt +from birdsong.models import CampaignStatus def redirect_helper(campaign): @@ -24,6 +23,6 @@ def send_campaign(backend, request, campaign, contacts): def send_test(backend, request, campaign, test_contact): campaign.subject = f"[TEST] {campaign.subject}" backend.send_campaign(request, campaign, [test_contact]) - messages.add_message(request, messages.SUCCESS, f"Test email sent, please check your inbox") + messages.success(request, "Test email sent, please check your inbox") return redirect_helper(campaign) diff --git a/birdsong/views/editor.py b/birdsong/views/editor.py index 7ec6431..2b2a206 100644 --- a/birdsong/views/editor.py +++ b/birdsong/views/editor.py @@ -3,16 +3,20 @@ from django.shortcuts import render from django.template.loader import render_to_string from wagtail.contrib.modeladmin.views import CreateView, EditView, InspectView -from birdsong.models import Contact, Receipt +from birdsong.models import Contact def preview(request, campaign, test_contact): - return render(request, campaign.get_template(request), campaign.get_context(request, test_contact)) + return render( + request, + campaign.get_template(request), + campaign.get_context(request, test_contact) + ) def confirm_send(request, campaign, form, send_url, index_url): context = { - 'self': campaign, + 'self': campaign, 'form': form, 'request': request, 'send_url': send_url, @@ -24,7 +28,7 @@ def confirm_send(request, campaign, form, send_url, index_url): def confirm_test(request, campaign, form, send_url, index_url): context = { - 'self': campaign, + 'self': campaign, 'form': form, 'request': request, 'send_url': send_url, diff --git a/birdsong/views/unsubscribe.py b/birdsong/views/unsubscribe.py index 46b28b6..883df13 100644 --- a/birdsong/views/unsubscribe.py +++ b/birdsong/views/unsubscribe.py @@ -16,7 +16,7 @@ def unsubscribe_user(request, user_id): 'BIRDSONG_UNSUBSCRIBE_TEMPLATE', 'unsubscribe.html' ) - + return render( request, template, context={ 'site': site, diff --git a/tests/app/wagtail_hooks.py b/tests/app/wagtail_hooks.py index 2f52f71..9fcf8ab 100644 --- a/tests/app/wagtail_hooks.py +++ b/tests/app/wagtail_hooks.py @@ -14,7 +14,6 @@ class SaleCampaignAdmin(CampaignAdmin): contact_class = ExtendedContact - @modeladmin_register class ContactAdmin(ModelAdmin): model = ExtendedContact diff --git a/tests/test_admin.py b/tests/test_admin.py index 3040c95..5a39808 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -37,7 +37,7 @@ class TestCampaignAdmin(WagtailTestUtils, TestCase): '/admin/app/salecampaign/create/', self.post_data(), follow=True ) self.assertEquals(response.status_code, 200) - + def test_edit(self): response = self.client.post( f'/admin/app/salecampaign/edit/{self.campaign.id}/', @@ -54,7 +54,6 @@ class TestCampaignAdmin(WagtailTestUtils, TestCase): self.assertEquals(response.status_code, 200) self.assertContains(response, '
The body
') - def test_live_preview(self): # TODO (post with ajax headers?) pass @@ -91,14 +90,13 @@ class TestSending(WagtailTestUtils, TransactionTestCase): 'location': 'Moon', } ) - sleep(10) # Allow time to send + sleep(10) # Allow time to send self.assertEqual(len(mail.outbox), 1) self.assertTrue('Hi Find Me' in mail.outbox[0].body) - def test_send(self): self.client.get(f'/admin/app/salecampaign/send_campaign/{self.campaign.id}/') - sleep(10) # Allow time to send + sleep(10) # Allow time to send self.assertEquals(len(mail.outbox), 2) self.assertEqual(self.campaign.receipts.all().count(), 2)