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'
24 mailing_list = 'general'
26 data_processing_part1 = '''\
27 Administratorem danych osobowych jest Fundacja Nowoczesna Polska (ul. Marszałkowska 84/92 lok. 125, 00-514 Warszawa).
28 Podanie danych osobowych jest dobrowolne.'''
29 data_processing_part2 = '''Dane są przetwarzane w zakresie niezbędnym do wysyłania newslettera odbiorcom.'''
30 data_processing_part3 = '''\
31 Osobom, których dane są zbierane, przysługuje prawo dostępu do treści swoich danych oraz ich poprawiania.
32 Więcej informacji w <a href="">polityce prywatności.</a>'''
35 def data_processing(self):
36 return mark_safe('%s %s %s' % (self.data_processing_part1, self.data_processing_part2, self.data_processing_part3))
38 def save(self, *args, **kwargs):
40 # multiple inheritance mode
41 super(NewsletterForm, self).save(*args, **kwargs)
42 except AttributeError:
44 if not (self.mailing or self.cleaned_data.get(self.mailing_field)):
46 email = self.cleaned_data[self.email_field]
49 except ValidationError:
52 # subscription, created = Subscription.objects.get_or_create(email=email, defaults={'active': False})
54 # ugettext('Confirm your subscription to Wolne Lektury newsletter'),
55 # render_to_string('newsletter/subscribe_email.html', {'subscription': subscription}), [email])
56 mailing.subscribe(email, mailing_lists=[self.mailing_list])
59 class SubscribeForm(NewsletterForm):
61 agree_newsletter = None
63 email = EmailField(label=_('email address'))
65 def __init__(self, mailing_list, *args, **kwargs):
66 self.mailing_list = mailing_list
67 super(SubscribeForm, self).__init__(*args, **kwargs)
70 class UnsubscribeForm(Form):
71 email = EmailField(label=_('email address'))
74 email = self.cleaned_data.get('email')
76 subscription = Subscription.objects.get(email=email)
77 except Subscription.DoesNotExist:
78 raise ValidationError(ugettext('Email address not found.'))
79 self.cleaned_data['subscription'] = subscription
82 subscription = self.cleaned_data['subscription']
83 subscription.active = False
85 mailing.unsubscribe(subscription.email)
87 context = {'subscription': subscription}
88 # refactor to send_noreply_mail
90 ugettext('Unsubscribe from Wolne Lektury\'s newsletter.'),
91 render_to_string('newsletter/unsubscribe_email.html', context),
92 [subscription.email], fail_silently=True)