1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.core.exceptions import ValidationError
5 from django.core.validators import validate_email
6 from django.forms import Form, BooleanField, MultipleChoiceField
7 from django.forms.fields import EmailField
8 from django.forms.widgets import CheckboxSelectMultiple
9 from django.template.loader import render_to_string
10 from django.utils.safestring import mark_safe
11 from django.utils.translation import ugettext_lazy as _, ugettext
13 from contact import mailing
14 from newsletter.models import Subscription
15 from wolnelektury.utils import send_noreply_mail
18 class NewsletterForm(Form):
20 agree_newsletter = BooleanField(
21 required=False, initial=False, label=_('I want to receive Wolne Lektury\'s newsletter.'))
23 mailing_field = 'agree_newsletter'
25 data_processing_part1 = '''\
26 Administratorem danych osobowych jest Fundacja Nowoczesna Polska (ul. Marszałkowska 84/92 lok. 125, 00-514 Warszawa).
27 Podanie danych osobowych jest dobrowolne.'''
28 data_processing_part2 = '''Dane są przetwarzane w zakresie niezbędnym do wysyłania newslettera odbiorcom.'''
29 data_processing_part3 = '''\
30 Osobom, których dane są zbierane, przysługuje prawo dostępu do treści swoich danych oraz ich poprawiania.
31 Więcej informacji w <a href="">polityce prywatności.</a>'''
34 def data_processing(self):
35 return mark_safe('%s %s %s' % (self.data_processing_part1, self.data_processing_part2, self.data_processing_part3))
37 def save(self, *args, **kwargs):
39 # multiple inheritance mode
40 super(NewsletterForm, self).save(*args, **kwargs)
41 except AttributeError:
43 if not (self.mailing or self.cleaned_data.get(self.mailing_field)):
45 email = self.cleaned_data[self.email_field]
48 except ValidationError:
51 # subscription, created = Subscription.objects.get_or_create(email=email, defaults={'active': False})
53 # ugettext('Confirm your subscription to Wolne Lektury newsletter'),
54 # render_to_string('newsletter/subscribe_email.html', {'subscription': subscription}), [email])
55 mailing.subscribe(email, mailing_lists=self.cleaned_data.get('mailing_lists'))
58 class SubscribeForm(NewsletterForm):
60 agree_newsletter = None
62 email = EmailField(label=_('email address'))
63 mailing_lists = MultipleChoiceField(
64 widget=CheckboxSelectMultiple,
65 choices=(('general', _('general newsletter')), ('contest', _('about the contest'))),
66 label=_('mailing list'))
68 def __init__(self, *args, **kwargs):
69 super(SubscribeForm, self).__init__(*args, **kwargs)
72 class UnsubscribeForm(Form):
73 email = EmailField(label=_('email address'))
76 email = self.cleaned_data.get('email')
78 subscription = Subscription.objects.get(email=email)
79 except Subscription.DoesNotExist:
80 raise ValidationError(ugettext('Email address not found.'))
81 self.cleaned_data['subscription'] = subscription
84 subscription = self.cleaned_data['subscription']
85 subscription.active = False
87 mailing.unsubscribe(subscription.email)
89 context = {'subscription': subscription}
90 # refactor to send_noreply_mail
92 ugettext('Unsubscribe from Wolne Lektury\'s newsletter.'),
93 render_to_string('newsletter/unsubscribe_email.html', context),
94 [subscription.email], fail_silently=True)