Added unsubscribe ability

This commit is contained in:
Zakary Timson 2018-01-14 17:15:28 -05:00
parent 667a470f41
commit c70e07e5fc
5 changed files with 39 additions and 6 deletions

View File

@ -37,6 +37,7 @@ urlpatterns = [
url(r'^logout/', main.views.logout, name='logout'), url(r'^logout/', main.views.logout, name='logout'),
url(r'^media/secure/(?P<path>.*)$', protected_serve, {'document_root': os.path.join(settings.MEDIA_ROOT, 'secure')}, name='secure media'), url(r'^media/secure/(?P<path>.*)$', protected_serve, {'document_root': os.path.join(settings.MEDIA_ROOT, 'secure')}, name='secure media'),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
url(r'^newsletter/unsubscribe', newsletters.views.unsubscribe, name='unsubscribe'),
url(r'^newsletter/', newsletters.views.newsletters, name='newsletters'), url(r'^newsletter/', newsletters.views.newsletters, name='newsletters'),
url(r'^tinymce/', include('tinymce.urls')) url(r'^tinymce/', include('tinymce.urls'))
] ]

View File

@ -19,7 +19,7 @@ class Command(BaseCommand):
print('Found %s subscribers' % len(subscribers)) print('Found %s subscribers' % len(subscribers))
for newsletter in newsletters: for newsletter in newsletters:
print('Sending newsletter: "%s"' % newsletter.subject) print('Sending newsletter: "%s"' % newsletter.subject)
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})) 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, 'unsubscribe': True}))
newsletter.sent = True newsletter.sent = True
newsletter.save() newsletter.save()
print('Complete!') print('Complete!')

View File

@ -0,0 +1,25 @@
{% extends 'base.html' %}
{% block body %}
<div class="container-fluid bg-dark-primary">
<div class="container py-3">
<div class="row">
<div class="col-12 col-md-8 mx-auto">
<h3 class="text-white">Unsubscribe From Newsletters</h3>
<form method="post">
{% csrf_token %}
<div class="input-group pt-2">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa fa-envelope-o"></i></span>
</div>
<input name="email" class="form-control" type="email" placeholder="Email">
<div class="input-group-append">
<button class="btn btn-danger" type="submit">Unsubscribe</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -1,7 +1,5 @@
import json
from django.db.models import Q from django.db.models import Q
from django.shortcuts import render from django.shortcuts import render, redirect
from django.utils import timezone from django.utils import timezone
from .models import Newsletter, Subscriber from .models import Newsletter, Subscriber
@ -10,5 +8,14 @@ from .models import Newsletter, Subscriber
def newsletters(request): def newsletters(request):
if request.method == 'POST': if request.method == 'POST':
Subscriber.objects.create(email=request.POST.get('email')) Subscriber.objects.create(email=request.POST.get('email'))
newsletters = Newsletter.objects.filter(Q(publish__lte=timezone.now())).order_by('-publish') newsletters = Newsletter.objects.filter(Q(publish__lte=timezone.now())).order_by('-publish')
return render(request, 'newsletters.html', {'newsletters': newsletters}) return render(request, 'newsletters.html', {'newsletters': newsletters})
def unsubscribe(request):
if request.method == 'POST':
Subscriber.objects.get(email=request.POST.get('email')).delete()
return redirect('newsletters')
return render(request, 'unsubscribe.html')

View File

@ -2,7 +2,7 @@
<div style="width: calc(100% - 2px); border: solid 1px rgba(0,0,0,0.2)"> <div style="width: calc(100% - 2px); border: solid 1px rgba(0,0,0,0.2)">
<div style="width: calc(100% - 1rem); background-color: #00305b; color: #ffffff; padding: 0.5rem"> <div style="width: calc(100% - 1rem); background-color: #00305b; color: #ffffff; padding: 0.5rem">
<img src="{{ base_url }}{% static 'main/img/logo.png' %}" width="30" height="30" style="display: inline-block; vertical-align: top" alt=""> <img src="{{ base_url }}{% static 'main/img/logo.png' %}" width="30" height="30" style="display: inline-block; vertical-align: top; margin: 1em;" alt="">
<div style="display: inline-block; font-size: 1.25rem;"> <div style="display: inline-block; font-size: 1.25rem;">
ONTARIO ASSOCIATION OF ONTARIO ASSOCIATION OF
<br> <br>
@ -33,7 +33,7 @@
</div> </div>
<div style="background-color: #00305b; color: #ffffff; width:100%"> <div style="background-color: #00305b; color: #ffffff; width:100%">
<div style="text-align: center"> <div style="text-align: center">
<p style="margin-bottom: 0;">© 2017 Ontario Association of Child Protection Lawyers.</p> <p style="margin-bottom: 0;">© 2017 Ontario Association of Child Protection Lawyers.{% if unsubscribe %} Unsubscribe <a href="{% url 'unsubscribe' %}">here</a>{% endif %}</p>
</div> </div>
</div> </div>
</div> </div>