Fundraising in PDF.
[wolnelektury.git] / src / newsletter / forms.py
index eb7afa5..4f4ced3 100644 (file)
@@ -1,41 +1,52 @@
-# -*- coding: utf-8 -*-
+# This file is part of Wolne Lektury, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Wolne Lektury. See NOTICE for more information.
+#
 from django.core.exceptions import ValidationError
 from django.core.validators import validate_email
 from django.forms import Form, BooleanField
 from django.forms.fields import EmailField
-from django.template.loader import render_to_string
 from django.utils.safestring import mark_safe
-from django.utils.translation import ugettext_lazy as _, ugettext
-
-from contact import mailing
-from newsletter.models import Subscription
-from wolnelektury.utils import send_noreply_mail
+from django.utils.translation import gettext_lazy as _
+from newsletter.subscribe import subscribe
+from .models import Newsletter
 
 
 class NewsletterForm(Form):
     email_field = 'email'
     agree_newsletter = BooleanField(
-        required=False, initial=False, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
+        required=False, initial=False, label=_('Chcę otrzymywać newsletter Wolnych Lektur'), label_suffix=False)
+    mailing = False
+    mailing_field = 'agree_newsletter'
+    newsletter = None
 
-    data_processing_part1 = u'''\
-Administratorem danych osobowych jest Fundacja Nowoczesna Polska (ul. Marszałkowska 84/92 lok. 125, 00-514 Warszawa).
-Podanie danych osobowych jest dobrowolne.'''
-    data_processing_part2 = u'''Dane są przetwarzane w zakresie niezbędnym do wysyłania newslettera odbiorcom.'''
-    data_processing_part3 = u'''\
+    data_processing_part1 = _('''\
+Administratorem danych osobowych jest Fundacja Wolne Lektury (ul. Marszałkowska 84/92 lok. 125, 00-514 Warszawa).
+Podanie danych osobowych jest dobrowolne.''')
+    data_processing_part2 = _('''Dane są przetwarzane w zakresie niezbędnym do wysyłania newslettera odbiorcom.''')
+    data_processing_part3 = _('''\
 Osobom, których dane są zbierane, przysługuje prawo dostępu do treści swoich danych oraz ich poprawiania.
-Więcej informacji w <a href="">polityce prywatności.</a>'''
+Więcej informacji w <a href="%(url)s">polityce prywatności.</a>''') % {
+    'url': 'https://fundacja.wolnelektury.pl/prywatnosc/'
+    }
 
+    def __init__(self, *args, **kwargs):
+        super().__init__(*args, **kwargs)
+        # Move the newsletter field to the end.
+        if 'agree_newsletter' in self.fields:
+            f = self.fields['agree_newsletter']
+            del self.fields['agree_newsletter']
+            self.fields['agree_newsletter'] = f
+    
     @property
     def data_processing(self):
         return mark_safe('%s %s %s' % (self.data_processing_part1, self.data_processing_part2, self.data_processing_part3))
 
     def save(self, *args, **kwargs):
-        try:
-            # multiple inheritance mode
-            super(NewsletterForm, self).save(*args, **kwargs)
-        except AttributeError:
-            pass
-        if not self.cleaned_data.get('agree_newsletter'):
+        newsletter = self.newsletter or Newsletter.objects.filter(slug='').first()
+        if not newsletter:
+            return
+
+        if not (self.mailing or self.cleaned_data.get(self.mailing_field)):
             return
         email = self.cleaned_data[self.email_field]
         try:
@@ -43,40 +54,17 @@ Więcej informacji w <a href="">polityce prywatności.</a>'''
         except ValidationError:
             pass
         else:
-            # 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])
-            mailing.subscribe(email)
+            subscribe(email, newsletter=newsletter)
 
 
 class SubscribeForm(NewsletterForm):
-    email = EmailField(label=_('email address'))
+    mailing = True
+    agree_newsletter = None
+    required_css_class = 'required'
 
-    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'))
+    email = EmailField(label=_('adres e-mail'))
 
-    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()
+    def __init__(self, newsletter, *args, **kwargs):
+        self.newsletter = newsletter
+        super(SubscribeForm, self).__init__(*args, **kwargs)
 
-        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)