X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/9e05900b7692f2d2a484edb33df69e8519e52131..1c9e2865ce8c6ab51b2fa3e43d3661089e7b7eb8:/src/newsletter/forms.py diff --git a/src/newsletter/forms.py b/src/newsletter/forms.py index f8927e5b8..edfaab6f7 100644 --- a/src/newsletter/forms.py +++ b/src/newsletter/forms.py @@ -1,24 +1,68 @@ -# -*- coding: utf-8 -*- - +# This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later. +# Copyright © Fundacja Nowoczesna Polska. 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.utils.translation import ugettext_lazy as _ - -from newsletter.models import Subscription +from django.forms.fields import EmailField +from django.utils.safestring import mark_safe +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, label=_(u'I want to receive Wolne Lektury\'s newsletter.')) + agree_newsletter = BooleanField( + required=False, initial=False, label=_('I want to receive Wolne Lektury\'s newsletter.'), label_suffix=False) + mailing = False + mailing_field = 'agree_newsletter' + newsletter = None + + 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 polityce prywatności.''' - def save(self): + 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): + 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: - # multiple inheritance mode - super(NewsletterForm, self).save() - except AttributeError: + validate_email(email) + except ValidationError: pass - email = self.cleaned_data[self.email_field] - 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? + else: + subscribe(email, newsletter=newsletter) + + +class SubscribeForm(NewsletterForm): + mailing = True + agree_newsletter = None + required_css_class = 'required' + + email = EmailField(label=_('email address')) + + def __init__(self, newsletter, *args, **kwargs): + self.newsletter = newsletter + super(SubscribeForm, self).__init__(*args, **kwargs) +