Last active
October 11, 2019 12:17
-
-
Save bencleary/b269a25f46eaa8d5e682fcee7cc254d1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from crunchy_waffles_app.errors import * | |
import requests | |
class EmailService: | |
subject = None | |
body = None | |
to = None | |
def __init__(self, subject, body, to): | |
EmailService.subject = subject | |
EmailService.body = body | |
EmailService.to = to | |
@staticmethod | |
def send_email(): | |
try: | |
task = requests.post( | |
"https://api.mailgun.net/v2/########/messages", | |
auth=("api", "key-#########"), | |
data={"from": "Bid Alert <[email protected]>", | |
"to": EmailService.to, | |
"subject": EmailService.subject, | |
"text": EmailService.body, | |
"html": EmailService.body | |
}) | |
task.raise_for_status() | |
return task.json() | |
except requests.exceptions.HTTPError as e: | |
print(EmailSendError("Error Sending Email: {0}".format(e))) | |
return "Error Sending Email: {0}".format(e) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.dispatch import receiver | |
from django.db.models.signals import post_save | |
from .tasks import * | |
@receiver(post_save, sender=Bid) | |
def update_auction_totals_for_bid(sender, instance, created, **kwargs): | |
if created: | |
send_bid_alert(instance.id) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import, unicode_literals | |
from celery import shared_task | |
from celery.exceptions import * | |
from django.utils import timezone | |
from .errors import * | |
from .models import * | |
from crunchy_waffles_shared.crunchy_emails import EmailService | |
@shared_task | |
def send_bid_alert(id): | |
bid = Bid.objects.get(pk=id) | |
email = EmailService("Bid Alert", "Someone has bid XXX on your XXX", bid.auction.owner.email).send_email() | |
if not isinstance(email, str): | |
bid.email_sent = True | |
bid.email_sent_time = timezone.now() | |
bid.save() | |
else: | |
raise EmailSendError(email) | |
return email | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment