Created
April 22, 2018 18:52
-
-
Save bencleary/d252e986eaea112f1aaab7aa4b44d2f2 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.db.models.signals import post_save | |
from django.dispatch import receiver | |
from .models import * | |
import pika | |
import json | |
@receiver(post_save, sender=Customer) | |
def send_welcome_email(sender, instance, created, *args, **kwargs): | |
if created: | |
full_name = instance.full_name() | |
data = { | |
'full_name': full_name, | |
'email': instance.email | |
} | |
payload = json.dumps(data) | |
connection = pika.BlockingConnection(pika.URLParameters("amqp://username:[email protected]/")) | |
channel = connection.channel() | |
channel.exchange_declare(exchange='welcome', | |
exchange_type='fanout') | |
channel.basic_publish(exchange='welcome', | |
routing_key='welcome_emails', | |
body=payload, | |
properties=pika.BasicProperties( | |
delivery_mode=2, # make message persistent | |
)) | |
connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment