Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First draft for importing Mailchimp campaigns #445

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ wagtailaltgenerator = "~4.1.2" # Compatible with Wagtail <= 5.0 as far as I can
wagtailmedia = "~0.13"
whitenoise = "~6.3"
django-redis = "~5.2"
mailchimp3 = "~3.0.21"

[tool.poetry.group.dev.dependencies]
black = "23.1.0"
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from django.conf import settings
from django.core.management.base import BaseCommand

from mailchimp3 import MailChimp

from wagtailio.newsletter.models import NewsletterIndexPage, NewsletterPage


class Command(BaseCommand):
def add_arguments(self, parser):
parser.add_argument(
"--index-page-id",
nargs=1,
type=int,
dest="index_page_id",
help="The ID of the NewsletterIndexPage to add the new pages to",
)

def handle(self, **options):
client = MailChimp(mc_api=settings.MAILCHIMP_API_KEY)

campaigns = client.campaigns.all(
get_all=True,
fields="campaigns.id,campaigns.settings.title,campaigns.send_time",
)

if options["index_page_id"]:
try:
index_page = NewsletterIndexPage.objects.get(
id=options["index_page_id"][0]
)
except NewsletterIndexPage.DoesNotExist:
print(
"NewsletterIndexPage with ID {} does not exist".format(
options["index_page_id"]
)
)
index_page = None
else:
index_page = NewsletterIndexPage.objects.live().public().first()

if index_page:
for campaign in campaigns["campaigns"]:
content = client.campaigns.content.get(campaign["id"])
existing_page = NewsletterPage.objects.filter(
mailchimp_campaign_id=campaign["id"]
).last()

if not existing_page:
# check if the campaign has a title, content, send time
if (
"html" in content.keys()
and "send_time" in campaign.keys()
and campaign["send_time"] != ""
and "title" in campaign["settings"].keys()
and campaign["settings"]["title"]
):
newsletter_page = NewsletterPage(
title=campaign["settings"]["title"],
mailchimp_campaign_id=campaign["id"],
mailchimp_campaign_content=content["html"],
first_published_at=campaign["send_time"],
date=campaign["send_time"].split("T")[0],
)
index_page.add_child(instance=newsletter_page)
newsletter_page.save()

print(
"Created new page for {}".format(
campaign["settings"]["title"]
)
)
29 changes: 29 additions & 0 deletions wagtailio/newsletter/migrations/0004_auto_20231215_2136.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 3.2.18 on 2023-12-15 21:36

from django.db import migrations, models

import wagtail.fields


class Migration(migrations.Migration):
dependencies = [
("newsletter", "0003_newsletteremailaddress_signed_up_at"),
]

operations = [
migrations.AddField(
model_name="newsletterpage",
name="mailchimp_campaign_content",
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name="newsletterpage",
name="mailchimp_campaign_id",
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name="newsletterpage",
name="body",
field=wagtail.fields.RichTextField(blank=True, null=True),
),
]
6 changes: 5 additions & 1 deletion wagtailio/newsletter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@
class NewsletterPage(Page):
date = models.DateField("Newsletter date")
intro = RichTextField(blank=True)
body = RichTextField()
body = RichTextField(null=True, blank=True)
mailchimp_campaign_id = models.CharField(max_length=255, null=True, blank=True)
mailchimp_campaign_content = models.TextField(null=True, blank=True)

content_panels = Page.content_panels + [
FieldPanel("date"),
FieldPanel("intro"),
FieldPanel("body"),
FieldPanel("mailchimp_campaign_content"),
]

search_fields = Page.search_fields + [
index.SearchField("intro"),
index.SearchField("body"),
index.SearchField("mailchimp_campaign_content"),
]

def get_context(self, request, *args, **kwargs):
Expand Down
Loading
Loading