Skip to content

Commit 7dd9428

Browse files
Merge pull request #1071 from torchbox/feature/register-your-interest
#7346 - WEB FORM | Register your interest - UK & IRE Data
2 parents c0516a3 + 1ef59ff commit 7dd9428

File tree

12 files changed

+41
-208
lines changed

12 files changed

+41
-208
lines changed

.isort.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ force_grid_wrap=0
55
use_parentheses=True
66
line_length=88
77

8-
known_third_party = bs4,captcha,dj_database_url,django,django_countries,factory,faker,freezegun,gunicorn,import_export,invoke,mailchimp_marketing,modelcluster,pattern_library,phonenumber_field,requests,rest_framework,taggit,wagtail,wagtail_factories,wagtailcaptcha,wagtailorderable
8+
known_third_party = bs4,captcha,dj_database_url,django,django_countries,factory,faker,freezegun,gunicorn,import_export,invoke,modelcluster,pattern_library,phonenumber_field,requests,rest_framework,taggit,wagtail,wagtail_factories,wagtailcaptcha,wagtailorderable

docs/integrations.md

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,5 @@
11
# Integrations
22

3-
## Mailchimp
4-
5-
We make use of Mailchimp's API to subscribe people as members when they submit a sign up form (rca.enquire_to_study.views.EnquireToStudyFormView).
6-
7-
There are two fields used to provide the enquire to study form with values we can use to better integrate with mailchimp, the `mailchimp_group_name` on programme pages and the `mailchimp_label` field within StartDate snippets.
8-
9-
More details on the MailChimp client [here](https://github.com/mailchimp/mailchimp-marketing-python).
10-
11-
### Developing
12-
13-
If wanting to update the mailchimp integration, request access to the mailchimp account and ask which audience list to use. They have a `Test` list which may be worth using on staging, providing that the mailchimp sign up forms match the list you wish to use on production.
14-
15-
#### API keys & IDs
16-
17-
You can get the `MAILCHIMP_API_KEY` from their mailchimp account or from the heroku config.
18-
For info on finding the `MAILCHIMP_LIST_ID` read this [article](https://mailchimp.com/help/find-audience-id/).
19-
To get the `MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID` value which is used to map values from the `mailchimp_group_name` field to values within a mailchimp "group", you will have to use the snippet below as group ids are currently only available via the API.
20-
21-
```python
22-
from mailchimp_marketing import Client
23-
mailchimp = Client()
24-
mailchimp.set_config(
25-
{
26-
"api_key": settings.MAILCHIMP_API_KEY,
27-
"server": settings.MAILCHIMP_API_KEY.split("-")[-1],
28-
}
29-
)
30-
mailchimp.get_list_interest_categories(MAILCHIMP_LIST_ID)
31-
```
32-
33-
You're interested in the `id` value for which `title` matches the one in mailchimp i.e. 'Programme of interest'. Set `MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID` to this value (i.e. `0da07d9429`).
34-
35-
Example response:
36-
37-
```python
38-
'categories':
39-
[
40-
{
41-
'list_id': '07bbaca3ef',
42-
'id': '0da07d9429',
43-
'title': 'Programme of interest',
44-
...
45-
```
46-
47-
You an use the endpoint below to confirm the ID works.
48-
49-
```python
50-
mailchimp.lists.list_interest_category_interests(settings.MAILCHIMP_LIST_ID, MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID)
51-
```
52-
533
## Azure AD SSO
544

555
To add SSO functionality, we used `social-auth-app-django`. All of the configurations and necessary keys can be seen in the `Azure AD (SSO)` and `Social Auth (SSO)` sections in the base settings. Logging in via SSO is optional. Users can still login via the standard Wagtail login form.

docs/support-runbook.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ The Azure environment is handled by RCA. If we need to update keys or redirect U
3232

3333
### 1. Check the data
3434

35-
The enquire to study form has 2 integrations:
36-
37-
1. Form data from user outside UK is sent to 'QS'
38-
2. Form data from user inside UK is sent to Mailchimp
39-
4035
The programme values the user select on the form have 'qs_code` values in Wagtail. As do some of the taxonomies. These 'qs_codes' and to lookup data from the QS endpoint. Usually the form throws 500s if these values change on the QS side but aren't updated in Wagtail. You can view the QS endpoint and inspect what codes a course should have - the endpoint is set as an env var in heroku.
4136

4237
If it turns out a programme/course has the wrong QS code, update it in wagtail with the right one from the endpoint.

mkdocs.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ markdown_extensions:
1313
nav:
1414
- 'Home': 'index.md'
1515
- 'Front end tooling': 'front-end-tooling.md'
16-
- 'Mailchimp integration': 'mailchimp-integration.md'
1716
- Implementation Specs:
1817
- 'Rebuild Approach': 'implementation-specs/finalised-rebuild-approach.md'
1918
- 'Programme Page': 'implementation-specs/programme-page.md'

poetry.lock

Lines changed: 3 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ coverage = "^7.6.10"
4141
freezegun = "^1.5.1"
4242
html5lib = "^1.1"
4343
lxml = "^5.3.0"
44-
mailchimp-marketing = "^3.0.80"
4544
scout-apm = "~3.2"
4645
sentry-sdk = "^2.19.2"
4746
tblib = "^3.0.0"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 4.2.17 on 2025-03-05 10:21
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("enquire_to_study", "0014_enquiretostudysettings_intro_heading_and_more"),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name="startdate",
15+
name="mailchimp_label",
16+
),
17+
]

rca/enquire_to_study/models.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,6 @@ class StartDate(models.Model):
3838
blank=True,
3939
null=True,
4040
)
41-
mailchimp_label = models.CharField(
42-
max_length=255,
43-
help_text=(
44-
"This value needs to match the options specified in the 'Intended year of study' "
45-
"field within mailchimps sign-up form , E.G 2021/22"
46-
),
47-
blank=True,
48-
null=True,
49-
)
5041

5142
class Meta:
5243
verbose_name = "Enquiry form start date"

rca/enquire_to_study/views.py

Lines changed: 3 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import logging
32
from datetime import timedelta
43

@@ -14,10 +13,7 @@
1413
from django.utils.decorators import method_decorator
1514
from django.views.decorators.csrf import csrf_exempt
1615
from django.views.generic import FormView, TemplateView
17-
from django_countries import countries
1816
from django_countries.ioc_data import IOC_TO_ISO
19-
from mailchimp_marketing import Client
20-
from mailchimp_marketing.api_client import ApiClientError
2117
from wagtail.admin import messages
2218

2319
from rca.utils.views import MetaTitleMixin
@@ -29,11 +25,7 @@
2925

3026

3127
class EnquireToStudyFormView(MetaTitleMixin, FormView):
32-
"""Custom form with integrations to Mailchimp and QS
33-
34-
On form submission, the post data is sent to different services depending
35-
on the 'country_of_residence' value. For the moment only QS is receiving
36-
post data, Mailchimp is incoming.
28+
"""Custom form with integrations to QS
3729
3830
Keys can be seen here:
3931
https://qs-enrolment-solutions.screenstepslive.com/s/Help/m/enquiryapi/l/889815-application-endpoints#student-enquiry
@@ -76,102 +68,6 @@ class EnquireToStudyFormView(MetaTitleMixin, FormView):
7668
def dispatch(self, *args, **kwargs):
7769
return super().dispatch(*args, **kwargs)
7870

79-
def get_mailchimp_interests_and_map_to_programmes(self, mailchimp, programmes):
80-
try:
81-
response = mailchimp.lists.list_interest_category_interests(
82-
settings.MAILCHIMP_LIST_ID,
83-
settings.MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID,
84-
count=500,
85-
)
86-
mailchimp_interest_ids = {}
87-
mapped_user_interests = {}
88-
try:
89-
for interest in response["interests"]:
90-
mailchimp_interest_ids.update({interest["name"]: interest["id"]})
91-
except ValueError:
92-
return {}
93-
for program in programmes:
94-
if program.mailchimp_group_name:
95-
try:
96-
interest_id = mailchimp_interest_ids[
97-
program.mailchimp_group_name
98-
]
99-
mapped_user_interests.update({interest_id: True})
100-
except KeyError:
101-
logger.warning(
102-
f"Mailchimp: Unable to map mailchimp_group_name for program page {program} - {program.id}"
103-
)
104-
pass
105-
return mapped_user_interests
106-
except ApiClientError as error:
107-
logger.exception(error.text)
108-
return {}
109-
110-
def post_mailchimp(self, form_data):
111-
mailchimp = Client()
112-
mailchimp.set_config(
113-
{
114-
"api_key": settings.MAILCHIMP_API_KEY,
115-
"server": settings.MAILCHIMP_API_KEY.split("-")[-1],
116-
}
117-
)
118-
119-
interests = {}
120-
if (
121-
form_data["programmes"]
122-
and settings.MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID
123-
):
124-
interests = self.get_mailchimp_interests_and_map_to_programmes(
125-
mailchimp, form_data["programmes"]
126-
)
127-
elif not settings.MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID:
128-
logger.warning(
129-
"Mailchimp: Set MAILCHIMP_PROGRAMMES_INTEREST_CATEGORY_ID to assign users to groups"
130-
)
131-
132-
country = dict(countries)[form_data["country_of_residence"]]
133-
134-
member_info = {
135-
"merge_fields": {
136-
"FNAME": form_data["first_name"],
137-
"LNAME": form_data["last_name"],
138-
"PHONE": str(form_data["phone_number"]),
139-
"MMERGE6": form_data["start_date"].mailchimp_label or "",
140-
"MMERGE7": form_data["country_of_citizenship"],
141-
"ADDRESS": {
142-
"addr1": "N/A",
143-
"addr2": "N/A",
144-
"city": form_data["city"],
145-
"state": "N/A",
146-
"zip": "N/A",
147-
"country": country,
148-
},
149-
"MMERGE8": form_data["country_of_residence"],
150-
"MMERGE9": form_data["city"],
151-
"MMERGE10": form_data["enquiry_reason"].reason,
152-
"MMERGE11": form_data["enquiry_questions"],
153-
},
154-
"interests": interests,
155-
"email_address": form_data["email"],
156-
"status": "subscribed",
157-
}
158-
159-
try:
160-
return mailchimp.lists.add_list_member(
161-
settings.MAILCHIMP_LIST_ID, member_info
162-
)
163-
except ApiClientError as error:
164-
try:
165-
error_json = json.loads(error.text)
166-
if error_json["title"] and error_json["title"] == "Member Exists":
167-
logger.info("Mailchimp: User tried to re-register to signup form")
168-
return
169-
except ValueError:
170-
logger.warning("Mailchimp: failed to decode error message")
171-
return
172-
logger.exception(error.text)
173-
return
174-
17571
def get_qs_data(self, query):
17672
return requests.get(
17773
f"{settings.QS_API_ENDPOINT}/{query}",
@@ -307,9 +203,8 @@ def set_session_data(self, form_data, enquiry_submission):
307203

308204
def form_valid(self, form):
309205
country_of_residence = form.cleaned_data["country_of_residence"]
310-
if country_of_residence in ["GB", "IE"] and settings.MAILCHIMP_API_KEY:
311-
self.post_mailchimp(form.cleaned_data)
312-
elif settings.QS_API_PASSWORD:
206+
207+
if settings.QS_API_PASSWORD:
313208
self.post_qs(form.cleaned_data)
314209

315210
enquiry_submission = form.save()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 4.2.17 on 2025-03-05 10:21
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("programmes", "0095_remove_programme_type_field"),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name="programmepage",
15+
name="mailchimp_group_name",
16+
),
17+
]

0 commit comments

Comments
 (0)