From 2d5e46793f9b1f3f87f972114a4448da8efe740e Mon Sep 17 00:00:00 2001 From: Jan Szejko Date: Thu, 29 Mar 2018 16:04:14 +0200 Subject: [PATCH] hook mailchimp to contact forms (cherry picked from commit 965a0be) --- contact/forms.py | 28 +++++++--- contact/mailing.py | 52 +++++++++++++++++++ contact/management/__init__.py | 0 contact/management/commands/__init__.py | 0 .../management/commands/export_newsletter.py | 29 +++++++++++ edumed/contact_forms.py | 2 + requirements.txt | 6 ++- 7 files changed, 110 insertions(+), 7 deletions(-) create mode 100644 contact/mailing.py create mode 100644 contact/management/__init__.py create mode 100644 contact/management/commands/__init__.py create mode 100644 contact/management/commands/export_newsletter.py diff --git a/contact/forms.py b/contact/forms.py index 2cea310..97fc027 100644 --- a/contact/forms.py +++ b/contact/forms.py @@ -9,6 +9,8 @@ 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 @@ -30,12 +32,15 @@ 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 = forms.CharField(max_length=128) + contact = NotImplemented def save(self, request, formsets=None): from .models import Attachment, Contact @@ -52,7 +57,7 @@ class ContactForm(forms.Form): sub_body[name] = value if sub_body: body.setdefault(f.form_tag, []).append(sub_body) - + contact = Contact.objects.create( body=body, ip=request.META['REMOTE_ADDR'], @@ -91,10 +96,21 @@ class ContactForm(forms.Form): 'contact/%s/mail_subject.txt' % self.form_tag, 'contact/mail_subject.txt', ], dictionary, context).strip() - mail_body = render_to_string([ - 'contact/%s/mail_body.txt' % self.form_tag, - 'contact/mail_body.txt', - ], dictionary, context) + if self.result_page: + mail_body = render_to_string( + 'contact/%s/results_email.txt' % contact.form_tag, + { + 'contact': contact, + 'results': self.results(contact), + }, context) + else: + mail_body = render_to_string([ + 'contact/%s/mail_body.txt' % self.form_tag, + '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..43b35ee --- /dev/null +++ b/contact/management/commands/export_newsletter.py @@ -0,0 +1,29 @@ +# -*- 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.OlimpiadaForm, +) + + +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 e5ceff2..022b02d 100644 --- a/edumed/contact_forms.py +++ b/edumed/contact_forms.py @@ -54,6 +54,7 @@ class OlimpiadaForm(ContactForm): ends_on = (2017, 11, 17, 0, 5) 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'] @@ -61,6 +62,7 @@ class OlimpiadaForm(ContactForm): 'student': forms.formsets.formset_factory(WTEMStudentForm, formset=NonEmptyBaseFormSet), 'commission': forms.formsets.formset_factory(CommissionForm), } + mailing_field = 'zgoda_newsletter' contact = forms.EmailField(label=u'Adres e-mail Przewodniczącego/Przewodniczącej', max_length=128) przewodniczacy = forms.CharField(label=u'Imię i nazwisko Przewodniczącego/Przewodniczącej', max_length=128) diff --git a/requirements.txt b/requirements.txt index ae876a5..2be5181 100644 --- a/requirements.txt +++ b/requirements.txt @@ -40,4 +40,8 @@ django-subdomains>=2.0.4,<2.1 unidecode -pytz \ No newline at end of file +pytz + +markdown2 + +mailchimp3 \ No newline at end of file -- 2.20.1