diff --git a/creator/management/commands/send_mails.py b/creator/management/commands/send_mails.py index eb1ba9d..e223540 100644 --- a/creator/management/commands/send_mails.py +++ b/creator/management/commands/send_mails.py @@ -2,6 +2,7 @@ from django.core import mail from django.core.management.base import BaseCommand +from django.core.signing import Signer from django.template.loader import render_to_string from easy_pdf.rendering import render_to_pdf @@ -15,6 +16,11 @@ class Command(BaseCommand): help = 'Looks up all mail subscriptions in the DB, generates the corresponding PDF and sends off an email' + def __init__(self): + super().__init__() + + self.signer = Signer() + def handle(self, *args, **options): logger.info("Starting to send todays email subscriptions") @@ -45,20 +51,28 @@ def handle(self, *args, **options): else: logger.info('No subscriptions found') - @classmethod - def new_email(cls, subscription, pdf): + def new_email(self, subscription, pdf): """Send email with pdf as attachment""" subject = "StundenzettelCreator - Your monthly timesheet" + unsubscribe_hash = self.signer.sign(subscription.pk) + text_content = """ Hey {first_name}, here is your monthly timesheet from StundenzettelCreator. + You can always unsubscribe with this link: http://stundenzettel-creator/unsubscribe/{hash} + Bye - """.format(first_name=subscription.first_name) - html_content = render_to_string('creator/subscription_email.html', {'context': subscription}) + """.format(first_name=subscription.first_name, hash=unsubscribe_hash) + html_content = render_to_string( + 'creator/subscription_email.html', + context={ + 'subscription': subscription, + 'hash': unsubscribe_hash + }) from_email = "subscription@stundenzettel-creator.xyz" recipient_list = [subscription.email] diff --git a/creator/templates/creator/subscription_email.html b/creator/templates/creator/subscription_email.html index 6a7b1d5..84cd55c 100644 --- a/creator/templates/creator/subscription_email.html +++ b/creator/templates/creator/subscription_email.html @@ -7,7 +7,8 @@
Hey {{ first_name }}, -here is your monthly timesheet from StundenzettelCreator.
+here is your monthly timesheet from StundenzettelCreator. + You can always unsubscribe by visiting unsubscribe
Bye
diff --git a/creator/templates/creator/subscribe.html b/creator/templates/creator/subscription_subscribe.html similarity index 100% rename from creator/templates/creator/subscribe.html rename to creator/templates/creator/subscription_subscribe.html diff --git a/creator/templates/creator/subscription_unsubscribe.html b/creator/templates/creator/subscription_unsubscribe.html new file mode 100644 index 0000000..327efdc --- /dev/null +++ b/creator/templates/creator/subscription_unsubscribe.html @@ -0,0 +1,11 @@ +{% extends "creator/base.html" %} + +{% block container %} + {% if status == 'success' %} +You successfully unsubscribed.
+ {% else %} +Please try visiting {% url 'unsubscribe' %}
+ {% endif %} +{% endblock %} \ No newline at end of file diff --git a/creator/urls.py b/creator/urls.py index 5eefc59..0c235df 100644 --- a/creator/urls.py +++ b/creator/urls.py @@ -1,10 +1,11 @@ from django.conf.urls import url -from creator.views import DetailsFormView, ResultPdfView, SubscriptionFormView, SuccessView +from creator.views import DetailsFormView, ResultPdfView, SubscriptionFormView, SuccessView, UnsubscribeView urlpatterns = [ url(r'^$', DetailsFormView.as_view(), name='index'), url(r'^result/', ResultPdfView.as_view(), name='result'), url(r'^subscribe/', SubscriptionFormView.as_view(), name='subscribe'), - url(r'^success/', SuccessView.as_view(), name='success') + url(r'^success/', SuccessView.as_view(), name='success'), + url(r'^unsubscribe/(?P