Pulled out url fix into a util

This commit is contained in:
Zakary Timson 2017-09-30 15:25:06 -04:00
parent fd58ba1695
commit faf1a9d0ed
3 changed files with 12 additions and 3 deletions

8
OACPL/utils.py Normal file
View File

@ -0,0 +1,8 @@
from django.template.loader import render_to_string
from OACPL import settings
def url_fix_render_to_string(template, context):
temp = render_to_string(template, context)
return temp.replace('src="/', 'src="%s/' % settings.BASE_URL).replace('href="/', 'href="%s/' % settings.BASE_URL)

View File

@ -6,6 +6,7 @@ from django.db.models import Q
from django.shortcuts import render, redirect
from django.template.loader import render_to_string
from OACPL.utils import url_fix_render_to_string
from charter_members.models import Attorney
from newsletters.models import Subscriber
from OACPL import settings
@ -25,7 +26,7 @@ def contact(request):
result = False
if name is not None and email is not None and subject is not None and body is not None:
result = mail.send_mail('OACPL CONTACT: %(subject)s' % locals(), body, settings.EMAIL_HOST_USER, [settings.EMAIL_CONTACT],
html_message=render_to_string('email.html', {'content': '<strong>Someone has messaged you via the website contact form!<br><br>Subject:</strong> %(subject)s<br><strong>From:</strong> %(name)s (%(email)s)<br><br>%(body)s' % locals(), 'signature': ' ', 'base_url': settings.BASE_URL}))
html_message=url_fix_render_to_string('email.html', {'content': '<strong>Someone has messaged you via the website contact form!<br><br>Subject:</strong> %(subject)s<br><strong>From:</strong> %(name)s (%(email)s)<br><br>%(body)s' % locals(), 'signature': ' '}))
return JsonResponse({'success': True if result else False})

View File

@ -3,9 +3,9 @@ from django.core import mail
from django.core.management.base import BaseCommand
from django.utils import timezone
from django.utils.html import strip_tags
from django.template.loader import render_to_string
from OACPL import settings
from OACPL.utils import url_fix_render_to_string
from newsletters.models import Newsletter, Subscriber
@ -19,7 +19,7 @@ class Command(BaseCommand):
print('Found %s subscribers' % len(subscribers))
for newsletter in newsletters:
print('Sending newsletter: "%s"' % newsletter.subject)
mail.send_mail(newsletter.subject, strip_tags(newsletter.body), settings.EMAIL_HOST_USER, subscribers, html_message=render_to_string('email.html', {'content': newsletter.body, 'base_url': settings.BASE_URL}))
mail.send_mail(newsletter.subject, strip_tags(newsletter.body), settings.EMAIL_HOST_USER, subscribers, html_message=url_fix_render_to_string('email.html', {'content': newsletter.body}))
newsletter.sent = True
newsletter.save()
print('Complete!')