newsletter
authorJan Szejko <janek37@gmail.com>
Wed, 14 Sep 2016 10:26:43 +0000 (12:26 +0200)
committerJan Szejko <janek37@gmail.com>
Wed, 14 Sep 2016 10:47:48 +0000 (12:47 +0200)
16 files changed:
src/ajaxable/templatetags/ajaxable_tags.py
src/newsletter/admin.py
src/newsletter/forms.py
src/newsletter/models.py
src/newsletter/templates/newsletter/confirm_subscription.html [new file with mode: 0644]
src/newsletter/templates/newsletter/subscribe_email.html [new file with mode: 0644]
src/newsletter/templates/newsletter/subscribe_form.html [new file with mode: 0644]
src/newsletter/templates/newsletter/subscribed.html [new file with mode: 0644]
src/newsletter/templates/newsletter/unsubscribe_email.html [new file with mode: 0644]
src/newsletter/templates/newsletter/unsubscribe_form.html [new file with mode: 0644]
src/newsletter/templates/newsletter/unsubscribed.html [new file with mode: 0644]
src/newsletter/urls.py
src/newsletter/views.py
src/suggest/forms.py
src/wolnelektury/templates/main_page.html
src/wolnelektury/utils.py

index c262a92..72b9baa 100644 (file)
@@ -26,8 +26,14 @@ def pretty_field(field, template=None):
             <li>
               <span class="error">%(errors)s</span>
               <label class="nohide"><span class="label">%(label)s: </span>%(input)s</label>
+              <span class="helptext">%(helptext)s</span>
             </li>'''
-    return mark_safe(template % {'errors': field.errors, 'input': field, 'label': field.label})
+    return mark_safe(template % {
+        'errors': field.errors,
+        'input': field,
+        'label': field.label,
+        'helptext': field.help_text,
+    })
 
 
 @register.filter
@@ -36,4 +42,5 @@ def pretty_checkbox(field):
         <li class="checkbox">
           <span class="error">%(errors)s</span>
           <label class="nohide">%(input)s<span class="label"> %(label)s</span></label>
+          <span class="helptext">%(helptext)s</span>
         </li>''')
index bcb7897..21ab434 100644 (file)
@@ -15,7 +15,9 @@ class SubscriptionAdmin(admin.ModelAdmin):
         return my_urls + urls
 
     def extract_subscribers(self, request):
-        return HttpResponse(',\n'.join(Subscription.objects.values_list('email', flat=True)), content_type='text/plain')
+        active_subscriptions = Subscription.objects.filter(active=True)
+        return HttpResponse(',\n'.join(active_subscriptions.values_list('email', flat=True)),
+                            content_type='text/plain')
 
 
 admin.site.register(Subscription, SubscriptionAdmin)
index d9f931b..5eb71e8 100644 (file)
@@ -2,15 +2,23 @@
 from django.core.exceptions import ValidationError
 from django.core.validators import validate_email
 from django.forms import Form, BooleanField
-from django.utils.translation import ugettext_lazy as _
+from django.forms.fields import EmailField
+from django.template.loader import render_to_string
+from django.utils.translation import ugettext_lazy as _, ugettext
 
 from newsletter.models import Subscription
+from wolnelektury.utils import send_noreply_mail
 
 
 class NewsletterForm(Form):
     email_field = 'email'
     agree_newsletter = BooleanField(
-        required=False, initial=True, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
+        required=False, initial=True, label=_(u'I want to receive Wolne Lektury\'s newsletter.'), help_text='''\
+Oświadczam, że wyrażam zgodę na przetwarzanie moich danych osobowych zawartych \
+w niniejszym formularzu zgłoszeniowym przez Fundację Nowoczesna Polska (administratora danych) z siedzibą \
+w Warszawie (00-514) przy ul. Marszałkowskiej 84/92 lok. 125 w celu otrzymywania newslettera Wolnych Lektur. \
+Jednocześnie oświadczam, że zostałam/em poinformowana/y o tym, że mam prawo wglądu w treść swoich danych i \
+możliwość ich poprawiania oraz że ich podanie jest dobrowolne, ale niezbędne do dokonania zgłoszenia.''')
 
     def save(self):
         try:
@@ -26,8 +34,39 @@ class NewsletterForm(Form):
         except ValidationError:
             pass
         else:
-            subscription, created = Subscription.objects.get_or_create(email=email)
-            if not created and not subscription.active:
-                subscription.active = True
-                subscription.save()
-            # Send some test email?
+            subscription, created = Subscription.objects.get_or_create(email=email, defaults={'active': False})
+            send_noreply_mail(
+                ugettext(u'Confirm your subscription to Wolne Lektury newsletter'),
+                render_to_string('newsletter/subscribe_email.html', {'subscription': subscription}), [email])
+
+
+class SubscribeForm(NewsletterForm):
+    email = EmailField(label=_('email address'))
+
+    def __init__(self, *args, **kwargs):
+        super(SubscribeForm, self).__init__(*args, **kwargs)
+        self.fields['agree_newsletter'].required = True
+
+
+class UnsubscribeForm(Form):
+    email = EmailField(label=_('email address'))
+
+    def clean(self):
+        email = self.cleaned_data.get('email')
+        try:
+            subscription = Subscription.objects.get(email=email)
+        except Subscription.DoesNotExist:
+            raise ValidationError(ugettext(u'Email address not found.'))
+        self.cleaned_data['subscription'] = subscription
+
+    def save(self):
+        subscription = self.cleaned_data['subscription']
+        subscription.active = False
+        subscription.save()
+
+        context = {'subscription': subscription}
+        # refactor to send_noreply_mail
+        send_noreply_mail(
+            ugettext(u'Unsubscribe from Wolne Lektury\'s newsletter.'),
+            render_to_string('newsletter/unsubscribe_email.html', context),
+            [subscription.email], fail_silently=True)
index 6d100a3..65fc435 100644 (file)
@@ -1,6 +1,9 @@
 # -*- coding: utf-8 -*-
+import hashlib
+
 from django.db.models import Model, EmailField, DateTimeField, BooleanField
 from django.utils.translation import ugettext_lazy as _
+from django.conf import settings
 
 
 class Subscription(Model):
@@ -14,4 +17,7 @@ class Subscription(Model):
         verbose_name_plural = _('subscriptions')
 
     def __unicode__(self):
-        return self.email
\ No newline at end of file
+        return self.email
+
+    def hashcode(self):
+        return hashlib.sha224(self.email + settings.SECRET_KEY).hexdigest()[:30]
diff --git a/src/newsletter/templates/newsletter/confirm_subscription.html b/src/newsletter/templates/newsletter/confirm_subscription.html
new file mode 100644 (file)
index 0000000..401789e
--- /dev/null
@@ -0,0 +1,7 @@
+{% extends "base/base.html" %}
+{% load i18n %}
+
+{% block body %}
+  <h1>{{ page_title }}</h1>
+  <p>{% trans "Your subscription to Wolne Lektury newsletter is confirmed. Thank you!" %}</p>
+{% endblock %}
\ No newline at end of file
diff --git a/src/newsletter/templates/newsletter/subscribe_email.html b/src/newsletter/templates/newsletter/subscribe_email.html
new file mode 100644 (file)
index 0000000..2670a48
--- /dev/null
@@ -0,0 +1,14 @@
+Dziękujemy za zapisanie się na newsletter Wolnych Lektur!
+
+W celu dokończenia rejestracji kliknij w poniższy link:
+
+https://wolnelektury.pl{% url 'confirm_subscription' subscription_id=subscription.id hashcode=subscription.hashcode %}
+
+lub skopiuj go do swojej przeglądarki.
+
+Dopóki tego nie zrobisz, nie będziesz otrzymywał od nas newsletterów.
+
+Jeżeli jednak otrzymałaś/eś tę wiadomość przez przypadek i nie chcesz otrzymywać newslettera Wolnych Lektur, zignoruj ją.
+
+Pozdrawiamy,
+zespół Wolnych Lektur
\ No newline at end of file
diff --git a/src/newsletter/templates/newsletter/subscribe_form.html b/src/newsletter/templates/newsletter/subscribe_form.html
new file mode 100644 (file)
index 0000000..67a39ce
--- /dev/null
@@ -0,0 +1,18 @@
+{% extends "base/base.html" %}
+{% load i18n %}
+{% load ssify %}
+{% load honeypot %}
+{% load ajaxable_tags %}
+
+{% block body %}
+  <h1>{{ page_title }}</h1>
+  <form id="subscribe-form" action="" method="post" accept-charset="utf-8" class="cuteform">
+    {% ssi_csrf_token %}
+    {% render_honeypot_field %}
+    <ol>
+      <li>{{ form.email|pretty_field }}</li>
+      <li>{{ form.agree_newsletter|pretty_checkbox }}</li>
+      <li><input type="submit" value="{% trans "Subscribe" %}"/></li>
+    </ol>
+  </form>
+{% endblock %}
\ No newline at end of file
diff --git a/src/newsletter/templates/newsletter/subscribed.html b/src/newsletter/templates/newsletter/subscribed.html
new file mode 100644 (file)
index 0000000..a703092
--- /dev/null
@@ -0,0 +1,9 @@
+{% extends "base/base.html" %}
+{% load i18n %}
+
+{% block body %}
+  <h1>{{ page_title }}</h1>
+  <p>
+    {% trans "You have requested subscription to Wolne Lektury newsletter. You'll receive a confirmation link by email." %}
+  </p>
+{% endblock %}
\ No newline at end of file
diff --git a/src/newsletter/templates/newsletter/unsubscribe_email.html b/src/newsletter/templates/newsletter/unsubscribe_email.html
new file mode 100644 (file)
index 0000000..e8e34f6
--- /dev/null
@@ -0,0 +1,6 @@
+Dziękujemy za dotychczasowe korzystanie z newslettera Wolnych Lektur.
+Jeśli w przyszłości zdecydujesz ponownie się na niego zapisać, wypełnij formularz
+https://wolnelektury.pl/{% url "subscribe" %}, a będziemy Cię na bieżąco informować o naszych działaniach.
+
+Pozdrawiamy,
+zespół Wolnych Lektur
\ No newline at end of file
diff --git a/src/newsletter/templates/newsletter/unsubscribe_form.html b/src/newsletter/templates/newsletter/unsubscribe_form.html
new file mode 100644 (file)
index 0000000..3372d2f
--- /dev/null
@@ -0,0 +1,16 @@
+{% extends "base/base.html" %}
+{% load i18n %}
+{% load ssify %}
+{% load honeypot %}
+
+{% block body %}
+  <h1>{{ page_title }}</h1>
+  <form id="unsubscribe-form" action="" method="post" accept-charset="utf-8" class="cuteform">
+    {% ssi_csrf_token %}
+    {% render_honeypot_field %}
+    <ol>
+      {{ form.as_ul }}
+      <li><input type="submit" value="{% trans "Unsubscribe" %}"/></li>
+    </ol>
+  </form>
+{% endblock %}
\ No newline at end of file
diff --git a/src/newsletter/templates/newsletter/unsubscribed.html b/src/newsletter/templates/newsletter/unsubscribed.html
new file mode 100644 (file)
index 0000000..f96c613
--- /dev/null
@@ -0,0 +1,7 @@
+{% extends "base/base.html" %}
+{% load i18n %}
+
+{% block body %}
+  <h1>{{ page_title }}</h1>
+  <p>{% trans "You have unsubscribed from Wolne Lektury newsletter. You'll receive a confirmation by email." %}</p>
+{% endblock %}
\ No newline at end of file
index 566f777..a34d98c 100644 (file)
@@ -3,5 +3,10 @@ from django.conf.urls import url
 from . import views
 
 urlpatterns = [
-    url(r'^wypisz-sie/$', views.unsubscribe, name='unsubscribe'),
+    url(r'^zapisz-sie/$', views.subscribe_form, name='subscribe'),
+    url(r'^zapis/$', views.subscribed, name='subscribed'),
+    url(r'^potwierdzenie/(?P<subscription_id>[0-9]+)/(?P<hashcode>[0-9a-f]+)/$',
+        views.confirm_subscription, name='confirm_subscription'),
+    url(r'^wypisz-sie/$', views.unsubscribe_form, name='unsubscribe'),
+    url(r'^wypisano/$', views.unsubscribed, name='unsubscribed'),
 ]
index 847530e..c1e4fbb 100644 (file)
@@ -1,9 +1,64 @@
 # -*- coding: utf-8 -*-
-from django.shortcuts import render
+from django.core.urlresolvers import reverse
+from django.http import Http404
+from django.http.response import HttpResponseRedirect
+from django.shortcuts import render, get_object_or_404
 from django.utils.translation import ugettext_lazy as _
 
+from newsletter.forms import UnsubscribeForm, SubscribeForm
+from newsletter.models import Subscription
 
-def unsubscribe(request):
-    return render(request, 'newsletter/unsubscribe.html', {
+
+def subscribe_form(request):
+    if request.POST:
+        form = SubscribeForm(request.POST)
+        if form.is_valid():
+            form.save()
+            return HttpResponseRedirect(reverse('subscribed'))
+    else:
+        form = SubscribeForm()
+    return render(request, 'newsletter/subscribe_form.html', {
+        'page_title': _(u'Subscribe'),
+        'form': form,
+    })
+
+
+def subscribed(request):
+    return render(request, 'newsletter/subscribed.html', {
+        'page_title': _(u'Subscribed'),
+    })
+
+
+def check_subscription(subscription, hashcode):
+    if hashcode != subscription.hashcode():
+        raise Http404
+
+
+def confirm_subscription(request, subscription_id, hashcode):
+    subscription = get_object_or_404(Subscription, id=subscription_id)
+    check_subscription(subscription, hashcode)
+    subscription.active = True
+    subscription.save()
+    return render(request, 'newsletter/confirm_subscription.html', {
+        'page_title': _(u'Subscription confirmed')
+    })
+
+
+def unsubscribe_form(request):
+    if request.POST:
+        form = UnsubscribeForm(request.POST)
+        if form.is_valid():
+            form.save()
+            return HttpResponseRedirect(reverse('unsubscribed'))
+    else:
+        form = UnsubscribeForm()
+    return render(request, 'newsletter/unsubscribe_form.html', {
         'page_title': _(u'Unsubscribe'),
+        'form': form,
+    })
+
+
+def unsubscribed(request):
+    return render(request, 'newsletter/unsubscribed.html', {
+        'page_title': _(u'Unsubscribed'),
     })
\ No newline at end of file
index 4521805..717cbc7 100644 (file)
@@ -13,6 +13,7 @@ from django.utils.translation import ugettext
 
 from newsletter.forms import NewsletterForm
 from suggest.models import PublishingSuggestion, Suggestion
+from wolnelektury.utils import send_noreply_mail
 
 
 class SuggestForm(NewsletterForm):
@@ -50,12 +51,12 @@ Kontakt: %(contact)s
         except ValidationError:
             pass
         else:
-            send_mail(u'[WolneLektury] ' + ugettext(u'Thank you for your suggestion.'),
-                      ugettext(u"""\
+            send_noreply_mail(
+                ugettext(u'Thank you for your suggestion.'),
+                ugettext(u"""\
 Thank you for your comment on WolneLektury.pl.
-The suggestion has been referred to the project coordinator.""") +
-                      u'\n\n-- \n' + ugettext(u'''Message sent automatically. Please do not reply.'''),
-                      'no-reply@wolnelektury.pl', [contact], fail_silently=True)
+The suggestion has been referred to the project coordinator."""),
+                [contact], fail_silently=True)
 
 
 class PublishingSuggestForm(NewsletterForm):
@@ -113,9 +114,8 @@ class PublishingSuggestForm(NewsletterForm):
                 pass
             else:
                 send_mail(
-                    u'[WolneLektury] ' + ugettext(u'Thank you for your suggestion.'),
+                    ugettext(u'Thank you for your suggestion.'),
                     ugettext(u"""\
 Thank you for your comment on WolneLektury.pl.
-The suggestion has been referred to the project coordinator.""") +
-                    u"\n\n-- \n" + ugettext(u'''Message sent automatically. Please do not reply.'''),
-                    'no-reply@wolnelektury.pl', [contact], fail_silently=True)
+The suggestion has been referred to the project coordinator."""),
+                    [contact], fail_silently=True)
index d2b7556..d46e192 100755 (executable)
         <li><a href="{% url 'oaipmh' %}">OAI-PMH</a></li>
         <li><a href="{% url 'lesmianator' %}" lang="pl">Leśmianator</a></li>
         <li><a href="http://polski.wolnelektury.pl" lang="pl">Materiały do nauki j. polskiego</a></li>
+        <li><a href="{% url 'subscribe' %}">{% trans "Newsletter" %}</a></li>
       </ul>
     </section>
 
index 2ce231b..72bc7d0 100644 (file)
@@ -9,11 +9,13 @@ from functools import wraps
 import pytz
 from inspect import getargspec
 
+from django.core.mail import send_mail
 from django.http import HttpResponse
 from django.template import RequestContext
 from django.template.loader import render_to_string
 from django.utils import timezone
 from django.conf import settings
+from django.utils.translation import ugettext
 
 tz = pytz.timezone(settings.TIME_ZONE)
 
@@ -106,3 +108,10 @@ def ajax(login_required=False, method=None, template=None, permission_required=N
         return ajax_view
 
     return decorator
+
+
+def send_noreply_mail(subject, message, recipient_list, **kwargs):
+    send_mail(
+        u'[WolneLektury] ' + subject,
+        message + u"\n\n-- \n" + ugettext(u'Message sent automatically. Please do not reply.'),
+        'no-reply@wolnelektury.pl', recipient_list, **kwargs)