Search code examples
pythondjangodjango-modelsdjango-admindjango-signals

Django signal mystery...development server vs. remote server


After many hours of hair pulling, I look to stackoverflow to help me solve this issue.

I have create two signals to run auto-email functions. The signals are triggered via a modification of save_model in my admin.py file.

The only problem is...BOTH of the signals work when run through the Django development server (the auto-emails are sent), but only ONE of the signals work when testing on the remote server. I don't understand how this is possible, and I haven't been able to find the bug.

I know there isn't much information to work with here. Any suggestions for what might be happening here?

As for file/directory structure, these signals are saved in a signals.py file located in the directory for this specific application. The signal is triggered via a modification of the save_model definition on my admin.py file, in the same directory.

Here are the signal definitions:

notify_status_change_signal = Signal(providing_args=['status', 'restaurant', 'delivery_date', 'contact_email', 'contact_phone'])
notify_on_change_signal = Signal(providing_args=['organization', 'identifier', 'id', 'restaurant', 'delivery_date'])

@receiver(notify_on_change_signal)
def notify_on_change(sender, organization, identifier, id, restaurant, delivery_date, signal, *args, **kwargs):
    "This is the signal that only functions when run through the local development server"
    order_id = identifier + str(id)
    subject = r'A change has been made to order %s' % order_id
    body = """
    System Message: The details for order %s (%s) have been updated by the client.""" % (order_id, organization)

    send_mail(subject, body, 'System Notification <system@expressdelivery.com>', [admin_email], fail_silently=False)    


@receiver(notify_status_change_signal)
def notify_status_change(sender, status, restaurant, delivery_date, contact_email, contact_phone, signal, *args, **kwargs):
    """This is the signal that works on both servers"""
    stat_body = {
    'Confirmed':"""

Message 1 """,

    'Placed': """

Message 2 """,

    'En Route': """

Message 3 """,

    'Completed':"""

Message 4 """,
    }

    #Auto email
    from_addr = 'Order Status <status@expressdelivery.com>'
    to_addrs = [contact_email]
    subject = u'Order %s %s status changed to %s.' % (restaurant, delivery_date, status)
    body = ''
    for stat_label, description in stat_body.iteritems():
        if status == stat_label: body = description

    if status == "Confirmed" or status == "En Route" or status == "Completed":    
        send_mail(subject, body, from_addr, to_addrs, fail_silently=False)

    #Auto text message
    if status == 'En Route':
        client = TwilioRestClient(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
        message = client.sms.messages.create(to=contact_phone,
                                     from_="5555555555",
                                     body="Your order, %s %s, is now en route." % (restaurant, delivery_date))

Solution

  • Although I definitely restarted the server when I updated the code with these signals, restarting the server again did the trick.