65
69
def add_message(self, archive_uuid, message):
66
70
message_uuid = uuid.uuid4()
71
parsed = email.parser.Parser().parsestr(message)
72
date = parsed.get('date')
75
pdate = parsedate_tz(date)
76
date = datetime.datetime(
78
tzinfo=dateutil.tz.tzoffset('', pdate[9]))
67
81
self.messages.insert(
69
83
{'date_created': datetime.datetime.utcnow().isoformat() + 'Z',
70
84
'content': message,
85
'from': parsed.get('From'),
86
'subject': parsed.get('Subject'),
87
'date': date.isoformat() if date is not None else None,
88
'message_id': parsed.get('Message-ID'),
72
90
self.archive_messages.insert(
73
archive_uuid, {datetime.datetime.now(): message_uuid})
92
{date.astimezone(dateutil.tz.tzutc()): message_uuid})
94
'Imported %s into %s' % (parsed.get('Message-ID'), archive_uuid))
74
95
return message_uuid
97
def _format_message(self, message):
99
'date': message['date'],
100
'from': message['from'],
101
'subject': message['subject'],
76
104
def get_messages(self, archive_uuid):
77
105
ids = self.archive_messages.get(
78
106
archive_uuid, column_count=10, column_reversed=True).values()
79
107
messages = self.messages.multiget(ids)
80
return [messages[id]['content'] for id in ids]
108
return [self._format_message(messages[id]) for id in ids]