Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
saevarom committed Dec 15, 2023
1 parent 33fc25e commit f1c9c4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,42 +1,60 @@
from django.conf import settings
from django.core.management.base import BaseCommand

from wagtailio.newsletter.models import NewsletterIndexPage, NewsletterPage

from mailchimp3 import MailChimp

from wagtailio.newsletter.models import NewsletterIndexPage, NewsletterPage

class Command(BaseCommand):

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')
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")
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])
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"]))
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()
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"]:

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"],
Expand All @@ -47,4 +65,8 @@ def handle(self, **options):
index_page.add_child(instance=newsletter_page)
newsletter_page.save()

print("Created new page for {}".format(campaign["settings"]["title"]))
print(
"Created new page for {}".format(
campaign["settings"]["title"]
)
)
16 changes: 8 additions & 8 deletions wagtailio/newsletter/migrations/0004_auto_20231215_2136.py
Original file line number Diff line number Diff line change
@@ -1,29 +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'),
("newsletter", "0003_newsletteremailaddress_signed_up_at"),
]

operations = [
migrations.AddField(
model_name='newsletterpage',
name='mailchimp_campaign_content',
model_name="newsletterpage",
name="mailchimp_campaign_content",
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name='newsletterpage',
name='mailchimp_campaign_id',
model_name="newsletterpage",
name="mailchimp_campaign_id",
field=models.CharField(blank=True, max_length=255, null=True),
),
migrations.AlterField(
model_name='newsletterpage',
name='body',
model_name="newsletterpage",
name="body",
field=wagtail.fields.RichTextField(blank=True, null=True),
),
]

0 comments on commit f1c9c4b

Please sign in to comment.