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
 
  10 from contact import mailing
 
  11 from newsletter.models import Subscription
 
  12 from wolnelektury.utils import send_noreply_mail
 
  15 class NewsletterForm(Form):
 
  17     agree_newsletter = BooleanField(
 
  18         required=False, initial=False, label=_(u'I want to receive Wolne Lektury\'s newsletter.'))
 
  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>'''
 
  29     def data_processing(self):
 
  30         return mark_safe('%s %s %s' % (self.data_processing_part1, self.data_processing_part2, self.data_processing_part3))
 
  32     def save(self, *args, **kwargs):
 
  34             # multiple inheritance mode
 
  35             super(NewsletterForm, self).save(*args, **kwargs)
 
  36         except AttributeError:
 
  38         if not self.cleaned_data.get('agree_newsletter'):
 
  40         email = self.cleaned_data[self.email_field]
 
  43         except ValidationError:
 
  46             # subscription, created = Subscription.objects.get_or_create(email=email, defaults={'active': False})
 
  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)
 
  53 class SubscribeForm(NewsletterForm):
 
  54     email = EmailField(label=_('email address'))
 
  56     def __init__(self, *args, **kwargs):
 
  57         super(SubscribeForm, self).__init__(*args, **kwargs)
 
  58         self.fields['agree_newsletter'].required = True
 
  61 class UnsubscribeForm(Form):
 
  62     email = EmailField(label=_('email address'))
 
  65         email = self.cleaned_data.get('email')
 
  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
 
  73         subscription = self.cleaned_data['subscription']
 
  74         subscription.active = False
 
  77         context = {'subscription': subscription}
 
  78         # refactor to 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)