from django.core.exceptions import ValidationError
from django.core.files.uploadedfile import UploadedFile
from django.core.mail import send_mail, mail_managers
-from django.core.urlresolvers import reverse
from django.core.validators import validate_email
from django import forms
from django.template.loader import render_to_string
from django.template import RequestContext
from django.utils.translation import ugettext_lazy as _
+from . import mailing
+
contact_forms = {}
admin_list_width = 0
__metaclass__ = ContactFormMeta
form_tag = None
+ old_form_tags = []
form_title = _('Contact form')
submit_label = _('Submit')
admin_list = None
result_page = False
+ mailing_field = None
required_css_class = 'required'
contact = NotImplemented
'contact/mail_body.txt',
], dictionary, context)
send_mail(mail_subject, mail_body, 'no-reply@%s' % site.domain, [contact.contact], fail_silently=True)
+ if self.mailing_field and self.cleaned_data[self.mailing_field]:
+ email = self.cleaned_data['contact']
+ mailing.subscribe(email)
return contact
--- /dev/null
+# -*- coding: utf-8 -*-
+
+from hashlib import md5
+
+import requests
+from django.conf import settings
+from mailchimp3 import MailChimp
+from mailchimp3.mailchimpclient import MailChimpError
+
+INTERESTS = {settings.MAILCHIMP_GROUP_ID: True}
+
+
+def get_client():
+ headers = requests.utils.default_headers()
+ headers['User-Agent'] = '%s (%s)' % settings.MANAGERS[0]
+
+
+def subscriber_hash(email):
+ return md5(email).hexdigest()
+
+
+def remove_from_groups(email, client):
+ group_ids = []
+ categories = client.lists.interest_categories.all(list_id=settings.MAILCHIMP_LIST_ID)['categories']
+ for category in categories:
+ groups = client.lists.interest_categories.interests.all(
+ list_id=settings.MAILCHIMP_LIST_ID, category_id=category['id'])['interests']
+ group_ids += [group['id'] for group in groups]
+ interests = {group_id: False for group_id in group_ids}
+ client.lists.members.update(
+ settings.MAILCHIMP_LIST_ID, subscriber_hash(email),
+ data={'interests': interests})
+
+
+def subscribe(email):
+ client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY, timeout=10.0)
+ try:
+ member = client.lists.members.get(settings.MAILCHIMP_LIST_ID, subscriber_hash(email))
+ except MailChimpError:
+ pass
+ else:
+ if member['status'] != 'subscribed':
+ remove_from_groups(email, client)
+ client.lists.members.create_or_update(
+ settings.MAILCHIMP_LIST_ID, subscriber_hash(email),
+ data={
+ 'email_address': email,
+ 'status_if_new': 'subscribed',
+ 'status': 'subscribed',
+ 'interests': INTERESTS,
+ }
+ )
--- /dev/null
+# -*- coding: utf-8 -*-
+# This file is part of EduMed, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
+from django.core.management.base import BaseCommand
+
+from contact.models import Contact
+from edumed import contact_forms
+
+FORMS = (
+ contact_forms.CooperateForm,
+ contact_forms.ContestForm,
+ contact_forms.WTEMForm,
+ contact_forms.TEMForm,
+ contact_forms.CybernauciForm,
+ contact_forms.WLEMForm,
+)
+
+
+class Command(BaseCommand):
+ help = 'Export contacts for newsletter.'
+
+ def handle(self, **options):
+ addresses = set(self.get_addresses())
+ for address in addresses:
+ print address
+
+ def get_addresses(self):
+ for form in FORMS:
+ tags = [form.form_tag] + form.old_form_tags
+ contacts = Contact.objects.filter(form_tag__in=tags)
+ for contact in contacts:
+ if contact.body.get(form.mailing_field):
+ yield contact.contact
form_tag = 'wspolpraca'
form_title = u"Bądź z nami w kontakcie"
admin_list = ['podpis', 'contact']
+ mailing_field = 'zgoda_dane'
podpis = forms.CharField(label=u'Imię i nazwisko', max_length=128)
contact = forms.EmailField(label=u'E-mail', max_length=128)
form_tag = 'konkurs'
form_title = u"Zgłoś się do konkursu"
admin_list = ['nazwisko', 'instytucja', 'tytul']
+ mailing_field = 'zgoda_informacje'
nazwisko = forms.CharField(label=u'Imię i nazwisko', max_length=128)
contact = forms.EmailField(label=u'Adres e-mail', max_length=128)
disabled = True
disabled_template = 'wtem/disabled_contact_form.html'
form_tag = "wtem"
+ old_form_tags = ["wtem2013", "wtem2014"]
form_title = u"WTEM - rejestracja uczestników"
submit_label = u"Wyślij zgłoszenie"
admin_list = ['imie', 'nazwisko', 'institution']
'student': forms.formsets.formset_factory(
WTEMStudentForm, formset=NonEmptyBaseFormSet, max_num=5, validate_max=True, extra=5),
}
+ mailing_field = 'zgoda_informacje'
contact = forms.EmailField(label=u'Adres e-mail opiekuna/opiekunki', max_length=128)
imie = forms.CharField(label=u'Imię', max_length=128)
disabled = True
disabled_template = 'wtem/disabled_contact_form.html'
form_tag = "olimpiada"
+ old_form_tags = ["olimpiada-2016"]
form_title = u"Olimpiada Cyfrowa - Elektroniczny System Zgłoszeń"
submit_label = u"Wyślij zgłoszenie"
admin_list = ['nazwisko', 'school']
form_tag = 'tem'
form_title = u"TEM - szkolenie dla trenerów edukacji medialnej"
admin_list = ['imie', 'nazwisko', 'instytucja', 'contact']
+ mailing_field = 'zgoda_informacje'
imie = forms.CharField(label=u'Imię', max_length=128)
nazwisko = forms.CharField(label=u'Nazwisko', max_length=128)
disabled = True
disabled_template = 'contact/disabled_contact_form.html'
form_tag = 'trenerzy-cybernauci2017'
+ old_form_tags = ['trenerzy-cybernauci']
form_title = u"Cybernauci – szkolenie dla trenerów"
admin_list = ['nazwisko', 'instytucja', 'contact']
submit_label = u'Wyślij'
+ mailing_field = 'zgoda_newsletter'
nazwisko = forms.CharField(label=u'Imię i nazwisko', max_length=1024)
adres = forms.CharField(label=u'Adres zamieszkania')
form_title = u"WLEM - szkolenie dla warszawskich liderów edukacji medialnej"
admin_list = ['nazwisko', 'instytucja', 'contact']
submit_label = u'Wyślij'
+ mailing_field = 'zgoda_newsletter'
nazwisko = forms.CharField(label=u'Imię i nazwisko', max_length=128)
contact = forms.CharField(label=u'Adres e-mail')
self.label_suffix = ''
form_tag = 'cybernauci-ankieta-trenera-2017'
+ old_form_tags = ['cybernauci-ankieta-trenera']
form_title = u"Cybernauci – ankieta trenerska"
nazwisko = forms.CharField(label=u'Imię i nazwisko', max_length=128)
contact = forms.CharField(label=u'Adres e-mail')
django-subdomains>=2.0.4,<2.1
-markdown2
\ No newline at end of file
+markdown2
+
+mailchimp3
\ No newline at end of file