hook mailchimp to contact forms
[edumed.git] / edumed / contact_forms.py
1 # -*- coding: utf-8 -*-
2 from django import forms
3 from django.forms.formsets import BaseFormSet
4
5 from contact.forms import ContactForm
6 from django.utils.translation import ugettext_lazy as _
7
8 WOJEWODZTWA = (
9     u'dolnośląskie',
10     u'kujawsko-pomorskie',
11     u'lubelskie',
12     u'lubuskie',
13     u'łódzkie',
14     u'małopolskie',
15     u'mazowieckie',
16     u'opolskie',
17     u'podkarpackie',
18     u'podlaskie',
19     u'pomorskie',
20     u'śląskie',
21     u'świętokrzyskie',
22     u'warmińsko-mazurskie',
23     u'wielkopolskie',
24     u'zachodniopomorskie',
25 )
26
27 WOJEWODZTWO_CHOICES = [(u'', u'(wybierz)')] + [(w, w) for w in WOJEWODZTWA]
28
29
30 class WTEMStudentForm(forms.Form):
31     first_name = forms.CharField(label=u'Imię', max_length=128)
32     last_name = forms.CharField(label=u'Nazwisko', max_length=128)
33     email = forms.EmailField(label=u'Adres e-mail', max_length=128)
34     form_tag = "student"
35
36
37 class NonEmptyBaseFormSet(BaseFormSet):
38     """
39     Won't allow formset_factory to be submitted with no forms
40     """
41     def clean(self):
42         for form in self.forms:
43             if form.cleaned_data:
44                 return
45         forms.ValidationError(u"Proszę podać dane przynajmniej jednej osoby.")
46
47
48 class CommissionForm(forms.Form):
49     name = forms.CharField(label=u'Imię i nazwisko Członka Komisji', max_length=128)
50     form_tag = "commission"
51
52
53 class OlimpiadaForm(ContactForm):
54     ends_on = (2017, 11, 17, 0, 5)
55     disabled_template = 'wtem/disabled_contact_form.html'
56     form_tag = "olimpiada"
57     old_form_tags = ["olimpiada-2016"]
58     form_title = u"Olimpiada Cyfrowa - Elektroniczny System Zgłoszeń"
59     submit_label = u"Wyślij zgłoszenie"
60     admin_list = ['nazwisko', 'school']
61     form_formsets = {
62         'student': forms.formsets.formset_factory(WTEMStudentForm, formset=NonEmptyBaseFormSet),
63         'commission': forms.formsets.formset_factory(CommissionForm),
64     }
65     mailing_field = 'zgoda_newsletter'
66
67     contact = forms.EmailField(label=u'Adres e-mail Przewodniczącego/Przewodniczącej', max_length=128)
68     przewodniczacy = forms.CharField(label=u'Imię i nazwisko Przewodniczącego/Przewodniczącej', max_length=128)
69     school = forms.CharField(label=u'Nazwa szkoły', max_length=255)
70     school_address = forms.CharField(label=u'Adres szkoły', widget=forms.Textarea, max_length=1000)
71     school_wojewodztwo = forms.ChoiceField(label=u'Województwo', choices=WOJEWODZTWO_CHOICES)
72     school_email = forms.EmailField(label=u'Adres e-mail szkoły', max_length=128)
73     school_phone = forms.CharField(label=u'Numer telefonu szkoły', max_length=32)
74     school_www = forms.URLField(label=u'Strona WWW szkoły', max_length=255, required=False)
75
76     zgoda_regulamin = forms.BooleanField(
77         label=u'Znam i akceptuję Regulamin Olimpiady Cyfrowej.',
78         help_text=u'Zobacz <a href="https://olimpiadacyfrowa.pl/regulamin/" target="_blank">'
79                   u'regulamin Olimpiady Cyfrowej</a>.'
80     )
81     zgoda_dane = forms.BooleanField(
82         label=u'Oświadczam, że wyrażam zgodę na przetwarzanie danych osobowych zawartych w niniejszym formularzu '
83               u'zgłoszeniowym przez Fundację Nowoczesna Polska (administratora danych) z siedzibą w Warszawie (00-514) '
84               u'przy ul. Marszałkowskiej 84/92 lok. 125 na potrzeby organizacji Olimpiady Cyfrowej. Jednocześnie '
85               u'oświadczam, że zostałam/em poinformowana/y o tym, że mam prawo wglądu w treść swoich danych '
86               u'i możliwość ich poprawiania oraz że ich podanie jest dobrowolne, ale niezbędne do dokonania '
87               u'zgłoszenia.')
88     zgoda_newsletter = forms.BooleanField(
89         label=u'Chcę otrzymywać newsletter: Edukacja medialna', required=False)
90
91     extract_types = (dict(slug='extended', label=_('extended')),)
92
93     @staticmethod
94     def get_extract_fields(contact, extract_type_slug):
95         fields = contact.body.keys()
96         if 'student' in fields:
97             fields.remove('student')
98         fields.extend(['contact', 'student_first_name', 'student_last_name', 'student_email'])
99         return fields
100
101     @staticmethod
102     def get_extract_records(keys, contact, extract_type_slug):
103         toret = [{}]
104         for field_name in keys:
105             if field_name.startswith('student_'):
106                 continue
107             if field_name == 'contact':
108                 val = contact.contact
109             else:
110                 val = contact.body[field_name]
111             toret[0][field_name] = val
112
113         current = toret[0]
114         if 'student' in contact.body:
115             for student in contact.body['student']:
116                 for attr in ('first_name', 'last_name', 'email'):
117                     current['student_' + attr] = student[attr]
118                 if current not in toret:
119                     toret.append(current)
120                 current = {}
121         return toret
122
123     def save(self, request, formsets=None):
124         from wtem.models import Confirmation
125         contact = super(OlimpiadaForm, self).save(request, formsets)
126
127         for formset in formsets or []:
128             if formset.prefix == 'student':
129                 for f in formset.forms:
130                     email = f.cleaned_data.get('email', None)
131                     if email:
132                         try:
133                             Confirmation.objects.get(email=email)
134                         except Confirmation.DoesNotExist:
135                             first_name = f.cleaned_data.get('first_name', None)
136                             last_name = f.cleaned_data.get('last_name', None)
137                             if first_name and last_name:
138                                 confirmation = Confirmation.create(
139                                     first_name=first_name, last_name=last_name, email=email, contact=contact)
140                                 confirmation.send_mail()
141         return contact