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.mailing_list])
58 class SubscribeForm(NewsletterForm):
60 agree_newsletter = None
62 email = EmailField(label=_('email address'))
64 def __init__(self, mailing_list, *args, **kwargs):
65 self.mailing_list = mailing_list
66 super(SubscribeForm, self).__init__(*args, **kwargs)
69 class UnsubscribeForm(Form):
70 email = EmailField(label=_('email address'))
73 email = self.cleaned_data.get('email')
75 subscription = Subscription.objects.get(email=email)
76 except Subscription.DoesNotExist:
77 raise ValidationError(ugettext('Email address not found.'))
78 self.cleaned_data['subscription'] = subscription
81 subscription = self.cleaned_data['subscription']
82 subscription.active = False
84 mailing.unsubscribe(subscription.email)
86 context = {'subscription': subscription}
87 # refactor to send_noreply_mail
89 ugettext('Unsubscribe from Wolne Lektury\'s newsletter.'),
90 render_to_string('newsletter/unsubscribe_email.html', context),
91 [subscription.email], fail_silently=True)