Commit 19382ff7 authored by Jelle Besseling's avatar Jelle Besseling

Merge branch 'newsletter-individual-mails' into 'master'

Send the newsletter to individual members

See merge request !1400
parents 9c44eb4e 14be9ce7
"""The emails defined by the newsletters package"""
import logging
from smtplib import SMTPException
from django.conf import settings
from django.core import mail
from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template
from django.utils import translation, timezone
......@@ -10,6 +13,8 @@ from members.models import Member
from newsletters import services
from partners.models import Partner
logger = logging.getLogger(__name__)
def send_newsletter(newsletter):
"""
......@@ -33,35 +38,51 @@ def send_newsletter(newsletter):
main_partner = Partner.objects.filter(is_main_partner=True).first()
local_partner = Partner.objects.filter(is_local_partner=True).first()
for language in settings.LANGUAGES:
translation.activate(language[0])
with mail.get_connection() as connection:
for language in settings.LANGUAGES:
translation.activate(language[0])
members = Member.current_members.all().filter(
profile__receive_newsletter=True,
profile__language=language[0]
)
subject = '[THALIA] ' + newsletter.title
recipients = [member.email for member in
Member.current_members.all().filter(
profile__receive_newsletter=True,
profile__language=language[0])
if member.email]
context = {
'newsletter': newsletter,
'agenda_events': events,
'main_partner': main_partner,
'local_partner': local_partner,
'lang_code': language[0]
}
subject = '[THALIA] ' + newsletter.title
html_message = html_template.render(context)
text_message = text_template.render(context)
context = {
'newsletter': newsletter,
'agenda_events': events,
'main_partner': main_partner,
'local_partner': local_partner,
'lang_code': language[0]
}
services.write_to_file(newsletter.pk, language[0], html_message)
html_message = html_template.render(context)
text_message = text_template.render(context)
msg = EmailMultiAlternatives(
subject=subject,
body=text_message,
from_email=from_email,
connection=connection
)
msg.attach_alternative(html_message, "text/html")
msg = EmailMultiAlternatives(subject, text_message,
to=[from_email],
bcc=recipients,
from_email=from_email)
msg.attach_alternative(html_message, "text/html")
msg.send()
for member in members:
if not member.email:
return
services.write_to_file(newsletter.pk, language[0], html_message)
msg.to = [member.email]
try:
msg.send()
logger.info('Sent newsletter to %s (%s)',
member.get_full_name(),
member.email)
except SMTPException as e:
logger.error('Failed to send the newsletter to %s (%s)',
member.get_full_name(),
member.email, e)
translation.deactivate()
translation.deactivate()
{% load i18n baseurl %}{% load static from staticfiles %}THALIA {% trans "newsletter"|upper %}
{% trans "dear members"|capfirst %},
{% trans "dear members"|upper %},
{{ newsletter.description|striptags }}
......
"""Defines tests for the newsletters package"""
import doctest
from django.conf import settings
from django.contrib.auth.models import User, Permission
from django.core import mail
from django.core.exceptions import ValidationError
......@@ -33,11 +32,38 @@ class NewslettersTest(TestCase):
Profile.objects.create(user=cls.user,
address_street='street',
address_postal_code='1234AB',
address_city='city')
address_city='city',
language='nl')
Membership.objects.create(type=Membership.MEMBER,
user=cls.user,
since=timezone.now())
cls.user2 = User.objects.create_user(username='janwillem',
email='janwillem@test.com',
password='top_secret',
is_staff=False)
Profile.objects.create(user=cls.user2,
address_street='street',
address_postal_code='1234AB',
address_city='city',
language='en')
Membership.objects.create(type=Membership.MEMBER,
user=cls.user2,
since=timezone.now())
cls.user3 = User.objects.create_user(username='thijs',
email='thijs@test.com',
password='top_secret',
is_staff=False)
Profile.objects.create(user=cls.user3,
address_street='street',
address_postal_code='1234AB',
address_city='city',
language='en')
Membership.objects.create(type=Membership.MEMBER,
user=cls.user3,
since=timezone.now())
cls.user.user_permissions.set(
Permission.objects.filter(content_type__app_label="newsletters")
)
......@@ -94,7 +120,7 @@ class NewslettersTest(TestCase):
self.client.post(reverse(
'newsletters:admin-send',
args=[testletter.pk]), {'post': 'yes'})
self.assertEqual(len(mail.outbox), len(settings.LANGUAGES))
self.assertEqual(len(mail.outbox), 3)
def test_email_html_and_text(self):
testletter = Newsletter.objects.create(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment