Created
July 3, 2010 22:58
-
-
Save staydecent/462907 to your computer and use it in GitHub Desktop.
This file contains 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
class EntryHandler(InboundMailHandler): | |
""" | |
Process inbound emails as entries into the database. | |
Every email is a new post. User must login to web interface | |
to edit any post details. | |
message object: | |
- subject | |
- sender | |
- to | |
- cc | |
- bodies | |
- date | |
- attachments | |
optional fields within message.bodies: | |
- slug (default message.subject) | |
- published (default message.date) | |
- tags | |
- template | |
""" | |
def receive(self, message): | |
# Combine message bodies and decode html | |
bodies = message.bodies('text/plain') | |
for content_type, body in bodies: | |
decoded_body = body.decode() | |
# Split body from any optional fields | |
body_list = decoded_body.split('\n\n', 1) | |
body_len = len(body_list) | |
fields = body_list[0] | |
body = body_list[1] | |
# Grab any data from fields | |
slug = message.subject.replace(' ', '-').lower() | |
template = 'None' | |
tags = 'None' | |
draft = False | |
if fields: | |
for config in yaml.load_all(fields): | |
if type(config) is dict: | |
# Probably should do this a better way | |
if 'slug' in config: | |
slug = config['slug'] | |
if 'template' in config: | |
template = config['template'] | |
if 'tags' in config: | |
tags = config['tags'].split(', ') | |
if 'draft' in config: | |
draft = True | |
date = message.date.split(' -'); | |
published = datetime.datetime.strptime( date[0], "%a, %d %b %Y %H:%M:%S" ) | |
# save | |
# Like: | |
# entry = Entry([...]) | |
# entry.put() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment