Skip to content

Instantly share code, notes, and snippets.

@bencleary
Last active December 15, 2017 21:06
Show Gist options
  • Save bencleary/581189c2267adb8986ea3c121b77e817 to your computer and use it in GitHub Desktop.
Save bencleary/581189c2267adb8986ea3c121b77e817 to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
from crunchy_waffles_app.models import *
from crunchy_waffles_ops.models import ManagementLog
from crunchy_waffles_ops.crunchy_classes_ops.management_log import Management
import time
class Command(BaseCommand):
title = "Process Emails"
help = 'Runs through email queue and sends emails'
log = Management(title)
def add_arguments(self, parser):
parser.add_argument('--id')
def handle(self, *args, **options):
self.log.in_progress()
if options['id']:
email_id = options['id']
try:
email = EmailQueue.objects.get(pk=email_id)
except:
raise CommandError("Email with that ID doesn't exists")
self.send_email(email)
email.delete()
self.log.update_count(1)
else:
emails = EmailQueue.EmailManager.emails()
for index, email in enumerate(emails):
self.send_email(email)
email.delete()
self.log.update_count(index + 1)
time.sleep(15)
self.log.completed()
def send_email(self, email):
try:
# send logic would allow for success or fail result, for this example just print out to console
print("Email sent to - {0}".format(email.auction.owner.email))
self.update_bid_row(email.bid)
except:
raise CommandError("Email could not send for reason XYZ")
def update_bid_row(self, bid):
bid.email_sent = True
bid.email_sent_time = timezone.now()
bid.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment