from django.core.management.base import BaseCommand

from base.newsletter_scheduler import dispatch_due_campaigns


class Command(BaseCommand):
    help = "Dispatch due newsletter campaigns"

    def add_arguments(self, parser):
        parser.add_argument("--limit", type=int, default=20)

    def handle(self, *args, **options):
        result = dispatch_due_campaigns(limit=options["limit"])
        self.stdout.write(
            self.style.SUCCESS(
                "processed_campaigns={processed_campaigns} sent_recipients={sent_recipients} failed_campaigns={failed_campaigns}".format(
                    **result
                )
            )
        )
