X-Git-Url: https://git.mdrn.pl/wolnelektury.git/blobdiff_plain/de08100bad0d82b2823453ecccaa66fd7521cd41..86ece3be121fe57d58e76bffcd52713d413cacc1:/src/newsletter/forms.py diff --git a/src/newsletter/forms.py b/src/newsletter/forms.py index f8927e5b8..d9f931b3d 100644 --- a/src/newsletter/forms.py +++ b/src/newsletter/forms.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- - +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 _ @@ -8,7 +9,8 @@ from newsletter.models import Subscription 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=True, label=_(u'I want to receive Wolne Lektury\'s newsletter.')) def save(self): try: @@ -16,9 +18,16 @@ class NewsletterForm(Form): super(NewsletterForm, self).save() except AttributeError: pass + if not self.cleaned_data.get('agree_newsletter'): + return 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? + try: + validate_email(email) + 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?