X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/e7d557155e2cd195f975e4559c353be5f28d7d66..909fd9ccd6f636cc512328dea596902d42d0f55c:/src/newsletter/forms.py diff --git a/src/newsletter/forms.py b/src/newsletter/forms.py index d9f931b3d..5eb71e898 100644 --- a/src/newsletter/forms.py +++ b/src/newsletter/forms.py @@ -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)