brack3t / Djrill

[INACTIVE/UNMAINTAINED] Djrill is an email backend and new message class for Django users that want to take advantage of the Mandrill transactional email service from MailChimp.
BSD 3-Clause "New" or "Revised" License
319 stars 64 forks source link

Key Error 'event' #108

Closed andrewfam closed 8 years ago

andrewfam commented 8 years ago
@receiver(webhook_event)
def handle_all(sender, event_type, data, **kwargs):
    print "Testing HEAD"
    if event_type == 'inbound':
        cdata = data['msg']['to'][0][0].split('@')[0].split('~')
        cuuid = cdata[0]
        hotel_id = cdata[1]     
        client = Client.objects.get(uuid=cuuid)
        connection.set_tenant(client)
        try:
            attachments = data['msg']['attachments']
            akeys = attachments.keys()
            for k in akeys:
                data = attachments[k]['content']

                ie = InEmail.objects.create(odata=data)
                import_data_to_models.delay(client.id, hotel_id, ie.id)
        except:
            pass    
    else:
        try:
            metadata = data['msg']['metadata']
            client = Client.objects.get(uuid=metadata['client'])
            connection.set_tenant(client)
            oe = OutEmail.objects.get(uuid=metadata['oe'])
            oe.data = data 
            oe.oe_type = event_type
            oe.save()
            print "Testing Done"
        except:
            pass

I'm getting a key error Event occasionally.


return view_func(_args, *_kwargs)
  File "/Users/andrewfam/.virtualenvs/snagfin/lib/python2.7/site-packages/django/utils/decorators.py", line 30, in bound_func
    return func.**get**(self, type(self))(_args2, *_kwargs2)
  File "/Users/andrewfam/.virtualenvs/snagfin/lib/python2.7/site-packages/djrill/views.py", line 67, in dispatch
    request, _args, *_kwargs)
  File "/Users/andrewfam/.virtualenvs/snagfin/lib/python2.7/site-packages/django/views/generic/base.py", line 89, in dispatch
    return handler(request, _args, *_kwargs)
  File "/Users/andrewfam/.virtualenvs/snagfin/lib/python2.7/site-packages/djrill/views.py", line 82, in post
    sender=None, event_type=event['event'], data=event)
KeyError: 'event'
medmunds commented 8 years ago

It looks like this could occur if your Mandrill webhook is configured to send whitelist/blacklist sync events.

The sync events webhook didn't exist when webhook support was added to Djrill, and it seems to not follow the format of the earlier message and inbound event webhooks that both use an 'event' key to identify their types.

We'll need to update Djrill to handle sync events. In the meantime, a workaround is to un-check the "Rejection Whitelist Changes" and "Rejection Blacklist Changes" triggers for the Djrill webhook in your Mandrill webhooks config: https://mandrillapp.com/settings/webhooks. (There's an "edit" command for an existing webhook hiding in the arrow next to "send test".)

(If you don't have the rejection blacklist/whitelist triggers set, then there's some other problem with Mandrill sending out-of-spec data. If that's the case, it would be helpful to see the event object from the line that's failing.)

andrewfam commented 8 years ago

Thanks for this i'll check it out