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
11 from contact import mailing
12 from newsletter.models import Subscription
13 from wolnelektury.utils import send_noreply_mail
16 class NewsletterForm(Form):
18 agree_newsletter = BooleanField(
19 required=False, initial=False, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
21 mailing_field = 'agree_newsletter'
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>'''
32 def data_processing(self):
33 return mark_safe('%s %s %s' % (self.data_processing_part1, self.data_processing_part2, self.data_processing_part3))
35 def save(self, *args, **kwargs):
37 # multiple inheritance mode
38 super(NewsletterForm, self).save(*args, **kwargs)
39 except AttributeError:
41 if not (self.mailing or self.cleaned_data.get(self.mailing_field)):
43 email = self.cleaned_data[self.email_field]
46 except ValidationError:
49 # subscription, created = Subscription.objects.get_or_create(email=email, defaults={'active': False})
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'))
56 class SubscribeForm(NewsletterForm):
58 agree_newsletter = None
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'))
66 def __init__(self, *args, **kwargs):
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(u'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(u'Unsubscribe from Wolne Lektury\'s newsletter.'),
91 render_to_string('newsletter/unsubscribe_email.html', context),
92 [subscription.email], fail_silently=True)