Club prepared.
[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, MultipleChoiceField
5 from django.forms.fields import EmailField
6 from django.forms.widgets import CheckboxSelectMultiple
7 from django.template.loader import render_to_string
8 from django.utils.safestring import mark_safe
9 from django.utils.translation import ugettext_lazy as _, ugettext
10
11 from contact import mailing
12 from newsletter.models import Subscription
13 from wolnelektury.utils import send_noreply_mail
14
15
16 class NewsletterForm(Form):
17     email_field = 'email'
18     agree_newsletter = BooleanField(
19         required=False, initial=False, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
20     mailing = False
21     mailing_field = 'agree_newsletter'
22
23     data_processing_part1 = u'''\
24 Administratorem danych osobowych jest Fundacja Nowoczesna Polska (ul. Marszałkowska 84/92 lok. 125, 00-514 Warszawa).
25 Podanie danych osobowych jest dobrowolne.'''
26     data_processing_part2 = u'''Dane są przetwarzane w zakresie niezbędnym do wysyłania newslettera odbiorcom.'''
27     data_processing_part3 = u'''\
28 Osobom, których dane są zbierane, przysługuje prawo dostępu do treści swoich danych oraz ich poprawiania.
29 Więcej informacji w <a href="">polityce prywatności.</a>'''
30
31     @property
32     def data_processing(self):
33         return mark_safe('%s %s %s' % (self.data_processing_part1, self.data_processing_part2, self.data_processing_part3))
34
35     def save(self, *args, **kwargs):
36         try:
37             # multiple inheritance mode
38             super(NewsletterForm, self).save(*args, **kwargs)
39         except AttributeError:
40             pass
41         if not (self.mailing or self.cleaned_data.get(self.mailing_field)):
42             return
43         email = self.cleaned_data[self.email_field]
44         try:
45             validate_email(email)
46         except ValidationError:
47             pass
48         else:
49             # subscription, created = Subscription.objects.get_or_create(email=email, defaults={'active': False})
50             # send_noreply_mail(
51             #     ugettext(u'Confirm your subscription to Wolne Lektury newsletter'),
52             #     render_to_string('newsletter/subscribe_email.html', {'subscription': subscription}), [email])
53             mailing.subscribe(email, mailing_lists=self.cleaned_data.get('mailing_lists'))
54
55
56 class SubscribeForm(NewsletterForm):
57     mailing = True
58     agree_newsletter = None
59
60     email = EmailField(label=_('email address'))
61     mailing_lists = MultipleChoiceField(
62         widget=CheckboxSelectMultiple,
63         choices=(('general', _(u'general newsletter')), ('contest', _(u'about the contest'))),
64         label=_(u'mailing list'))
65
66     def __init__(self, *args, **kwargs):
67         super(SubscribeForm, self).__init__(*args, **kwargs)
68
69
70 class UnsubscribeForm(Form):
71     email = EmailField(label=_('email address'))
72
73     def clean(self):
74         email = self.cleaned_data.get('email')
75         try:
76             subscription = Subscription.objects.get(email=email)
77         except Subscription.DoesNotExist:
78             raise ValidationError(ugettext(u'Email address not found.'))
79         self.cleaned_data['subscription'] = subscription
80
81     def save(self):
82         subscription = self.cleaned_data['subscription']
83         subscription.active = False
84         subscription.save()
85         mailing.unsubscribe(subscription.email)
86
87         context = {'subscription': subscription}
88         # refactor to send_noreply_mail
89         send_noreply_mail(
90             ugettext(u'Unsubscribe from Wolne Lektury\'s newsletter.'),
91             render_to_string('newsletter/unsubscribe_email.html', context),
92             [subscription.email], fail_silently=True)