update email with key + small fix
[edumed.git] / edumed / contact_forms.py
index 2cb7dd9..196e5e4 100644 (file)
@@ -10,8 +10,6 @@ from django.core.validators import validate_email
 from django.template.loader import render_to_string
 from django.utils.translation import ugettext_lazy as _
 
-from wtem.models import Confirmation
-
 WOJEWODZTWA = (
     u'dolnośląskie',
     u'kujawsko-pomorskie',
@@ -272,6 +270,8 @@ class OlimpiadaForm(ContactForm):
               u'oświadczam, że zostałam/em poinformowana/y o tym, że mam prawo wglądu w treść swoich danych '
               u'i możliwość ich poprawiania oraz że ich podanie jest dobrowolne, ale niezbędne do dokonania '
               u'zgłoszenia.')
+    zgoda_newsletter = forms.BooleanField(
+        label=u'Chcę otrzymywać newsletter: Edukacja medialna', required=False)
 
     extract_types = (dict(slug='extended', label=_('extended')),)
 
@@ -306,30 +306,23 @@ class OlimpiadaForm(ContactForm):
         return toret
 
     def save(self, request, formsets=None):
+        from wtem.models import Confirmation
         contact = super(OlimpiadaForm, self).save(request, formsets)
 
-        mail_subject = render_to_string('contact/olimpiada/student_mail_subject.html').strip()
         for formset in formsets or []:
             if formset.prefix == 'student':
                 for f in formset.forms:
                     email = f.cleaned_data.get('email', None)
-                    try:
-                        confirmation = Confirmation.objects.get(email=email)
-                    except Confirmation.DoesNotExist:
-                        first_name = f.cleaned_data.get('first_name', None)
-                        last_name = f.cleaned_data.get('last_name', None)
-                        confirmation = Confirmation.create(
-                            first_name=first_name, last_name=last_name, email=email, contact=contact)
-                    mail_body = render_to_string(
-                        'contact/olimpiada/student_mail_body.html', {'confirmation': confirmation})
-                    try:
-                        validate_email(email)
-                    except ValidationError:
-                        pass
-                    else:
-                        send_mail(mail_subject, mail_body, 'olimpiada@nowoczesnapolska.org.pl', [email],
-                                  fail_silently=True)
-
+                    if email:
+                        try:
+                            Confirmation.objects.get(email=email)
+                        except Confirmation.DoesNotExist:
+                            first_name = f.cleaned_data.get('first_name', None)
+                            last_name = f.cleaned_data.get('last_name', None)
+                            if first_name and last_name:
+                                confirmation = Confirmation.create(
+                                    first_name=first_name, last_name=last_name, email=email, contact=contact)
+                                confirmation.send_mail()
         return contact