From c7ff3149aa25aeda0a53ffa05761947a2e806484 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Thu, 6 Oct 2022 12:23:48 +0200 Subject: [PATCH] Support civicrm newsletters. --- src/club/civicrm.py | 59 +++++++++++-------- .../migrations/0004_auto_20221006_1212.py | 23 ++++++++ src/newsletter/models.py | 3 +- src/newsletter/subscribe.py | 12 +++- 4 files changed, 70 insertions(+), 27 deletions(-) create mode 100644 src/newsletter/migrations/0004_auto_20221006_1212.py diff --git a/src/club/civicrm.py b/src/club/civicrm.py index b770fbd0f..20b287a28 100644 --- a/src/club/civicrm.py +++ b/src/club/civicrm.py @@ -27,11 +27,11 @@ class CiviCRM: d = response.json() return d - def create_or_update_contact(self, email, key): + def create_or_update_contact(self, email, key=None): contact_id = self.get_contact_id(email) if contact_id is None: contact_id = self.create_contact(email, key) - else: + elif key: self.update_contact(contact_id, key) return contact_id @@ -49,29 +49,26 @@ class CiviCRM: if result: return result[0]['id'] - def create_contact(self, email, key): - result = self.request( - 'Contact', - 'create', - { - 'values': { - 'WL.TPWL_key': key, - }, - 'chain': { - 'email': [ - 'Email', - 'create', - { - 'values': { - 'email': email, - 'contact_id': '$id' - } + def create_contact(self, email, key=None): + data = { + 'values': {}, + 'chain': { + 'email': [ + 'Email', + 'create', + { + 'values': { + 'email': email, + 'contact_id': '$id' } - ] - } + } + ] } - ) - return result[0]['id'] + } + if key: + data['values']['WL.TPWL_key'] = key + result = self.request('Contact', 'create', data) + return result['values'][0]['id'] def update_contact(self, contact_id, key): return self.request( @@ -163,7 +160,21 @@ class CiviCRM: } ) - #do we create a civicontribution? + def add_email_to_group(self, email, group_id): + contact_id = self.create_or_update_contact(email) + self.add_contact_to_group(contact_id, group_id) + + def add_contact_to_group(self, contact_id, group_id): + self.request( + 'GroupContact', + 'create', + { + "values": { + "group_id": group_id, + "contact_id": contact_id, + } + } + ) civicrm = CiviCRM( diff --git a/src/newsletter/migrations/0004_auto_20221006_1212.py b/src/newsletter/migrations/0004_auto_20221006_1212.py new file mode 100644 index 000000000..69ee3eaef --- /dev/null +++ b/src/newsletter/migrations/0004_auto_20221006_1212.py @@ -0,0 +1,23 @@ +# Generated by Django 2.2.28 on 2022-10-06 10:12 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('newsletter', '0003_newsletter'), + ] + + operations = [ + migrations.AddField( + model_name='newsletter', + name='crm_id', + field=models.IntegerField(blank=True, null=True), + ), + migrations.AlterField( + model_name='newsletter', + name='phplist_id', + field=models.IntegerField(blank=True, null=True), + ), + ] diff --git a/src/newsletter/models.py b/src/newsletter/models.py index af57d381c..dc28e3d10 100644 --- a/src/newsletter/models.py +++ b/src/newsletter/models.py @@ -28,4 +28,5 @@ class Subscription(models.Model): class Newsletter(models.Model): slug = models.SlugField(blank=True) page_title = models.CharField(max_length=255, blank=True) - phplist_id = models.IntegerField() + phplist_id = models.IntegerField(null=True, blank=True) + crm_id = models.IntegerField(null=True, blank=True) diff --git a/src/newsletter/subscribe.py b/src/newsletter/subscribe.py index 16a27a793..6d84e3a37 100644 --- a/src/newsletter/subscribe.py +++ b/src/newsletter/subscribe.py @@ -3,10 +3,19 @@ # import requests from django.conf import settings +from club.civicrm import civicrm def subscribe(email, newsletter): - list_id = newsletter.phplist_id + if newsletter.crm_id: + subscribe_crm(email, newsletter.crm_id) + if newsletter.phplist_id: + subscribe_phplist(email, newsletter.phplist_id) + +def subscribe_crm(email, group_id): + civicrm.add_email_to_group(email, group_id) + +def subscribe_phplist(email, list_id): data = { "email": email, "emailconfirm": email, @@ -25,4 +34,3 @@ def subscribe(email, newsletter): "NEWSLETTER_PHPLIST_SUBSCRIBE_URL not set. " f"Trying to subscribe email: {email} to newsletter: {list_id}." ) - -- 2.20.1