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