forked from mmmaly/chcemvediet
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsignals.py
96 lines (82 loc) · 3.34 KB
/
signals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# vim: expandtab
# -*- coding: utf-8 -*-
from base64 import b64decode
from django.core.files.base import ContentFile
from django.dispatch import Signal, receiver
from poleno.attachments.models import Attachment
from poleno.utils.mail import full_decode_header
from ...models import Message, Recipient
webhook_event = Signal(providing_args=['event_type', 'data'])
@receiver(webhook_event)
def message_status_webhook_event(sender, event_type, data, **kwargs):
if u'_id' not in data:
return
if event_type == u'deferral':
status = Recipient.STATUSES.QUEUED
elif event_type in [u'soft_bounce', u'hard_bounce', u'spam', u'reject']:
status = Recipient.STATUSES.REJECTED
elif event_type == u'send':
status = Recipient.STATUSES.SENT
elif event_type in [u'open', u'click']:
status = Recipient.STATUSES.OPENED
else:
return
try:
recipient = Recipient.objects.get(remote_id=data[u'_id'])
recipient.status = status
recipient.status_details = event_type
recipient.save(update_fields=[u'status', u'status_details'])
except (Recipient.DoesNotExist, Recipient.MultipleObjectsReturned):
pass
@receiver(webhook_event)
def inbound_email_webhook_event(sender, event_type, data, **kwargs):
if event_type == u'inbound' and u'msg' in data:
msg = data[u'msg']
headers = msg.get(u'headers') or ()
from_name = msg.get(u'from_name') or u''
from_mail = msg.get(u'from_email') or u''
received_for = msg.get(u'email') or u''
subject = msg.get(u'subject') or u''
text = msg.get(u'text') or u''
html = msg.get(u'html') or u''
recipients = []
for header_name, type in ((u'to', Recipient.TYPES.TO),
(u'cc', Recipient.TYPES.CC),
(u'bcc', Recipient.TYPES.BCC)):
for rcp_mail, rcp_name in msg.get(header_name) or []:
if rcp_mail:
recipients.append(Recipient(
name=rcp_name or u'',
mail=rcp_mail,
type=type,
status=Recipient.STATUSES.INBOUND,
))
attachments = []
for attch in (msg.get(u'attachments') or {}).values():
filename = attch.get(u'name') or u''
filename = full_decode_header(filename) # Mandrill does not decode '=?utf-8?...?=' in attachment names.
content = attch.get(u'content') or u''
if attch.get(u'base64', False):
content = b64decode(content)
attachments.append(Attachment(
file=ContentFile(content),
name=filename,
))
message = Message(
type=Message.TYPES.INBOUND,
processed=None,
from_name=from_name,
from_mail=from_mail,
received_for=received_for,
subject=subject,
text=text,
html=html,
headers=headers,
)
message.save()
for recipient in recipients:
recipient.message = message
recipient.save()
for attachment in attachments:
attachment.generic_object = message
attachment.save()