Flake8 for tests + .flake8
This commit is contained in:
parent
2dd1735c3e
commit
dc863cab82
|
@ -0,0 +1,4 @@
|
||||||
|
[flake8]
|
||||||
|
ignore = E731, W503
|
||||||
|
max-line-length = 100
|
||||||
|
exclude = migrations
|
|
@ -2,7 +2,6 @@ import logging
|
||||||
from smtplib import SMTPException
|
from smtplib import SMTPException
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
from django.core.mail import send_mass_mail
|
|
||||||
from django.db import close_old_connections, transaction
|
from django.db import close_old_connections, transaction
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
@ -26,15 +25,17 @@ class SendCampaignThread(Thread):
|
||||||
try:
|
try:
|
||||||
logger.info(f"Sending {len(self.messages)} emails")
|
logger.info(f"Sending {len(self.messages)} emails")
|
||||||
send_mass_html_mail(self.messages)
|
send_mass_html_mail(self.messages)
|
||||||
logger.info(f"Emails finsihed sending")
|
logger.info("Emails finished sending")
|
||||||
with transaction.atomic():
|
with transaction.atomic():
|
||||||
Campaign.objects.filter(pk=self.campaign_pk).update(
|
Campaign.objects.filter(pk=self.campaign_pk).update(
|
||||||
status=CampaignStatus.SENT,
|
status=CampaignStatus.SENT,
|
||||||
sent_date=timezone.now(),
|
sent_date=timezone.now(),
|
||||||
)
|
)
|
||||||
fresh_contacts = Contact.objects.filter(pk__in=self.contact_pks)
|
fresh_contacts = Contact.objects.filter(
|
||||||
Campaign.objects.get(pk=self.campaign_pk).receipts.add(*fresh_contacts)
|
pk__in=self.contact_pks)
|
||||||
except SMTPException as e:
|
Campaign.objects.get(
|
||||||
|
pk=self.campaign_pk).receipts.add(*fresh_contacts)
|
||||||
|
except SMTPException:
|
||||||
logger.exception(f"Problem sending campaign: {self.campaign_pk}")
|
logger.exception(f"Problem sending campaign: {self.campaign_pk}")
|
||||||
self.campaign.status = CampaignStatus.FAILED
|
self.campaign.status = CampaignStatus.FAILED
|
||||||
finally:
|
finally:
|
||||||
|
@ -58,5 +59,6 @@ class SMTPEmailBackend(BaseEmailBackend):
|
||||||
'reply_to': [self.reply_to],
|
'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()
|
campaign_thread.start()
|
||||||
|
|
|
@ -10,5 +10,6 @@ class UnwrappedStreamBlock(blocks.StreamBlock):
|
||||||
|
|
||||||
|
|
||||||
class DefaultBlocks(UnwrappedStreamBlock):
|
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'])
|
features=['h3', 'h4', 'bold', 'italic', 'link', 'ul', 'ol', 'document-link'])
|
||||||
|
|
|
@ -1,19 +1,14 @@
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
from django.conf import settings
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from modelcluster.contrib.taggit import ClusterTaggableManager
|
from modelcluster.contrib.taggit import ClusterTaggableManager
|
||||||
from modelcluster.fields import ParentalKey
|
from modelcluster.fields import ParentalKey
|
||||||
from modelcluster.models import ClusterableModel
|
from modelcluster.models import ClusterableModel
|
||||||
from taggit.models import TaggedItemBase
|
from taggit.models import TaggedItemBase
|
||||||
from wagtail.admin.edit_handlers import FieldPanel
|
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.models import Site
|
||||||
from wagtail.core.utils import camelcase_to_underscore
|
from wagtail.core.utils import camelcase_to_underscore
|
||||||
|
|
||||||
from .backends import BaseEmailBackend
|
|
||||||
from .blocks import DefaultBlocks
|
|
||||||
|
|
||||||
|
|
||||||
class ContactTag(TaggedItemBase):
|
class ContactTag(TaggedItemBase):
|
||||||
content_object = ParentalKey(
|
content_object = ParentalKey(
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
from django.forms import modelform_factory
|
from django.forms import modelform_factory
|
||||||
from django.http.response import HttpResponseRedirect
|
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.helpers import AdminURLHelper, ButtonHelper
|
||||||
from wagtail.contrib.modeladmin.options import ModelAdmin
|
from wagtail.contrib.modeladmin.options import ModelAdmin
|
||||||
|
|
||||||
|
@ -73,6 +71,7 @@ class CampaignAdmin(ModelAdmin):
|
||||||
|
|
||||||
def get_admin_urls_for_registration(self):
|
def get_admin_urls_for_registration(self):
|
||||||
urls = super().get_admin_urls_for_registration()
|
urls = super().get_admin_urls_for_registration()
|
||||||
|
|
||||||
def gen_url(pattern, view, name=None):
|
def gen_url(pattern, view, name=None):
|
||||||
if not name:
|
if not name:
|
||||||
name = pattern
|
name = pattern
|
||||||
|
@ -97,7 +96,6 @@ class CampaignAdmin(ModelAdmin):
|
||||||
contact = self.contact_class.objects.first()
|
contact = self.contact_class.objects.first()
|
||||||
return editor_views.preview(request, campaign, contact)
|
return editor_views.preview(request, campaign, contact)
|
||||||
|
|
||||||
|
|
||||||
def confirm_send(self, request, instance_pk):
|
def confirm_send(self, request, instance_pk):
|
||||||
campaign = self.model.objects.get(pk=instance_pk)
|
campaign = self.model.objects.get(pk=instance_pk)
|
||||||
form = self.build_sending_form()
|
form = self.build_sending_form()
|
||||||
|
@ -128,7 +126,7 @@ class CampaignAdmin(ModelAdmin):
|
||||||
return actions.send_campaign(self.backend, request, campaign, contacts)
|
return actions.send_campaign(self.backend, request, campaign, contacts)
|
||||||
|
|
||||||
def create_contact_form(self, data=None):
|
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:
|
if data:
|
||||||
return ContactForm(data)
|
return ContactForm(data)
|
||||||
return ContactForm()
|
return ContactForm()
|
||||||
|
@ -153,7 +151,7 @@ class CampaignAdmin(ModelAdmin):
|
||||||
# Create fake contact, send test email
|
# Create fake contact, send test email
|
||||||
contact = form.save(commit=False)
|
contact = form.save(commit=False)
|
||||||
return actions.send_test(self.backend, request, campaign, contact)
|
return actions.send_test(self.backend, request, campaign, contact)
|
||||||
|
|
||||||
def copy(self, request, instance_pk):
|
def copy(self, request, instance_pk):
|
||||||
instance = self.model.objects.get(pk=instance_pk)
|
instance = self.model.objects.get(pk=instance_pk)
|
||||||
instance.name = instance.name + ' (Copy)'
|
instance.name = instance.name + ' (Copy)'
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.shortcuts import redirect
|
from django.shortcuts import redirect
|
||||||
from django.utils import timezone
|
|
||||||
from wagtail.contrib.modeladmin.helpers.url import AdminURLHelper
|
from wagtail.contrib.modeladmin.helpers.url import AdminURLHelper
|
||||||
|
|
||||||
from birdsong.models import CampaignStatus, Contact, Receipt
|
from birdsong.models import CampaignStatus
|
||||||
|
|
||||||
|
|
||||||
def redirect_helper(campaign):
|
def redirect_helper(campaign):
|
||||||
|
@ -24,6 +23,6 @@ def send_campaign(backend, request, campaign, contacts):
|
||||||
def send_test(backend, request, campaign, test_contact):
|
def send_test(backend, request, campaign, test_contact):
|
||||||
campaign.subject = f"[TEST] {campaign.subject}"
|
campaign.subject = f"[TEST] {campaign.subject}"
|
||||||
backend.send_campaign(request, campaign, [test_contact])
|
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)
|
return redirect_helper(campaign)
|
||||||
|
|
|
@ -3,16 +3,20 @@ from django.shortcuts import render
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
from wagtail.contrib.modeladmin.views import CreateView, EditView, InspectView
|
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):
|
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):
|
def confirm_send(request, campaign, form, send_url, index_url):
|
||||||
context = {
|
context = {
|
||||||
'self': campaign,
|
'self': campaign,
|
||||||
'form': form,
|
'form': form,
|
||||||
'request': request,
|
'request': request,
|
||||||
'send_url': send_url,
|
'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):
|
def confirm_test(request, campaign, form, send_url, index_url):
|
||||||
context = {
|
context = {
|
||||||
'self': campaign,
|
'self': campaign,
|
||||||
'form': form,
|
'form': form,
|
||||||
'request': request,
|
'request': request,
|
||||||
'send_url': send_url,
|
'send_url': send_url,
|
||||||
|
|
|
@ -16,7 +16,7 @@ def unsubscribe_user(request, user_id):
|
||||||
'BIRDSONG_UNSUBSCRIBE_TEMPLATE',
|
'BIRDSONG_UNSUBSCRIBE_TEMPLATE',
|
||||||
'unsubscribe.html'
|
'unsubscribe.html'
|
||||||
)
|
)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
request, template, context={
|
request, template, context={
|
||||||
'site': site,
|
'site': site,
|
||||||
|
|
|
@ -14,7 +14,6 @@ class SaleCampaignAdmin(CampaignAdmin):
|
||||||
contact_class = ExtendedContact
|
contact_class = ExtendedContact
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@modeladmin_register
|
@modeladmin_register
|
||||||
class ContactAdmin(ModelAdmin):
|
class ContactAdmin(ModelAdmin):
|
||||||
model = ExtendedContact
|
model = ExtendedContact
|
||||||
|
|
|
@ -37,7 +37,7 @@ class TestCampaignAdmin(WagtailTestUtils, TestCase):
|
||||||
'/admin/app/salecampaign/create/', self.post_data(), follow=True
|
'/admin/app/salecampaign/create/', self.post_data(), follow=True
|
||||||
)
|
)
|
||||||
self.assertEquals(response.status_code, 200)
|
self.assertEquals(response.status_code, 200)
|
||||||
|
|
||||||
def test_edit(self):
|
def test_edit(self):
|
||||||
response = self.client.post(
|
response = self.client.post(
|
||||||
f'/admin/app/salecampaign/edit/{self.campaign.id}/',
|
f'/admin/app/salecampaign/edit/{self.campaign.id}/',
|
||||||
|
@ -54,7 +54,6 @@ class TestCampaignAdmin(WagtailTestUtils, TestCase):
|
||||||
self.assertEquals(response.status_code, 200)
|
self.assertEquals(response.status_code, 200)
|
||||||
self.assertContains(response, '<p>The body</p>')
|
self.assertContains(response, '<p>The body</p>')
|
||||||
|
|
||||||
|
|
||||||
def test_live_preview(self):
|
def test_live_preview(self):
|
||||||
# TODO (post with ajax headers?)
|
# TODO (post with ajax headers?)
|
||||||
pass
|
pass
|
||||||
|
@ -91,14 +90,13 @@ class TestSending(WagtailTestUtils, TransactionTestCase):
|
||||||
'location': 'Moon',
|
'location': 'Moon',
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
sleep(10) # Allow time to send
|
sleep(10) # Allow time to send
|
||||||
self.assertEqual(len(mail.outbox), 1)
|
self.assertEqual(len(mail.outbox), 1)
|
||||||
self.assertTrue('Hi Find Me' in mail.outbox[0].body)
|
self.assertTrue('Hi Find Me' in mail.outbox[0].body)
|
||||||
|
|
||||||
|
|
||||||
def test_send(self):
|
def test_send(self):
|
||||||
self.client.get(f'/admin/app/salecampaign/send_campaign/{self.campaign.id}/')
|
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.assertEquals(len(mail.outbox), 2)
|
||||||
self.assertEqual(self.campaign.receipts.all().count(), 2)
|
self.assertEqual(self.campaign.receipts.all().count(), 2)
|
||||||
|
|
Loading…
Reference in New Issue