newsletter checkboxes in suggestions and registration
[wolnelektury.git] / src / newsletter / forms.py
1 # -*- coding: utf-8 -*-
2 from django.core.exceptions import ValidationError
3 from django.core.validators import validate_email
4 from django.forms import Form, BooleanField
5 from django.utils.translation import ugettext_lazy as _
6
7 from newsletter.models import Subscription
8
9
10 class NewsletterForm(Form):
11     email_field = 'email'
12     agree_newsletter = BooleanField(
13         required=False, initial=True, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
14
15     def save(self):
16         try:
17             # multiple inheritance mode
18             super(NewsletterForm, self).save()
19         except AttributeError:
20             pass
21         if not self.cleaned_data.get('agree_newsletter'):
22             return
23         email = self.cleaned_data[self.email_field]
24         try:
25             validate_email(email)
26         except ValidationError:
27             pass
28         else:
29             subscription, created = Subscription.objects.get_or_create(email=email)
30             if not created and not subscription.active:
31                 subscription.active = True
32                 subscription.save()
33             # Send some test email?