Flake8 for tests + .flake8

This commit is contained in:
Seb 2020-06-15 15:47:52 +10:00
parent 2dd1735c3e
commit dc863cab82
10 changed files with 31 additions and 31 deletions

4
.flake8 Normal file
View File

@ -0,0 +1,4 @@
[flake8]
ignore = E731, W503
max-line-length = 100
exclude = migrations

View File

@ -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()

View File

@ -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'])

View File

@ -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(

View File

@ -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()

View File

@ -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)

View File

@ -3,11 +3,15 @@ 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):

View File

@ -14,7 +14,6 @@ class SaleCampaignAdmin(CampaignAdmin):
contact_class = ExtendedContact
@modeladmin_register
class ContactAdmin(ModelAdmin):
model = ExtendedContact

View File

@ -54,7 +54,6 @@ class TestCampaignAdmin(WagtailTestUtils, TestCase):
self.assertEquals(response.status_code, 200)
self.assertContains(response, '<p>The body</p>')
def test_live_preview(self):
# TODO (post with ajax headers?)
pass
@ -95,7 +94,6 @@ class TestSending(WagtailTestUtils, TransactionTestCase):
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}/')