From: Jan Szejko Date: Thu, 29 Mar 2018 14:04:14 +0000 (+0200) Subject: hook mailchimp to contact forms X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/965a0bef5bcde07b7df2ac1b07f73ce67105b8a6 hook mailchimp to contact forms --- diff --git a/contact/forms.py b/contact/forms.py index 36d793f..97fc027 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -3,13 +3,14 @@ from django.contrib.sites.models import Site 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 @@ -31,10 +32,12 @@ class ContactForm(forms.Form): __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 @@ -106,5 +109,8 @@ class ContactForm(forms.Form): '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 diff --git a/contact/mailing.py b/contact/mailing.py new file mode 100644 index 0000000..bfd4209 --- /dev/null +++ b/contact/mailing.py @@ -0,0 +1,52 @@ +# -*- 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, + } + ) diff --git a/contact/management/__init__.py b/contact/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contact/management/commands/__init__.py b/contact/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/contact/management/commands/export_newsletter.py b/contact/management/commands/export_newsletter.py new file mode 100644 index 0000000..70ef2b4 --- /dev/null +++ b/contact/management/commands/export_newsletter.py @@ -0,0 +1,34 @@ +# -*- 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 diff --git a/edumed/contact_forms.py b/edumed/contact_forms.py index 29b0333..91ea7b9 100644 --- a/edumed/contact_forms.py +++ b/edumed/contact_forms.py @@ -60,6 +60,7 @@ class CooperateForm(ContactForm): 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) @@ -90,6 +91,7 @@ class ContestForm(ContactForm): 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) @@ -157,6 +159,7 @@ class WTEMForm(ContactForm): 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'] @@ -164,6 +167,7 @@ class WTEMForm(ContactForm): '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) @@ -254,6 +258,7 @@ class OlimpiadaForm(ContactForm): 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'] @@ -376,6 +381,7 @@ class TEMForm(ContactForm): 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) @@ -485,9 +491,11 @@ class CybernauciForm(ContactForm): 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') @@ -558,6 +566,7 @@ class WLEMForm(ContactForm): 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') @@ -608,6 +617,7 @@ class CybernauciAnkietaForm(ContactForm): 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') diff --git a/requirements.txt b/requirements.txt index c9c4032..4d8d85a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -39,4 +39,6 @@ pyyaml django-subdomains>=2.0.4,<2.1 -markdown2 \ No newline at end of file +markdown2 + +mailchimp3 \ No newline at end of file