Exemple #1
0
def inbound_email(request):
    raw_data = request.raw_post_data
    #filename = '/tmp/raw_data.%s.json' % (time.time(),)
    #with open(filename, 'w') as f:
    #    f.write(raw_data)

    data = json.loads(raw_data)
    inbound = PostmarkInbound(json=raw_data)
    if not inbound.has_attachments():
        m = "ERROR! No attachments"
        logging.debug(m)
        return http.HttpResponse(m)
    try:
        hashkey, subject = inbound.subject().split(':', 1)
    except ValueError:
        m = "ERROR! No hashkey defined in subject line"
        logging.debug(m)
        return http.HttpResponse(m)
    try:
        post = BlogItem.get_by_inbound_hashkey(hashkey)
    except BlogItem.DoesNotExist:
        m = "ERROR! Unrecognized hashkey"
        logging.debug(m)
        return http.HttpResponse(m)

    attachments = inbound.attachments()
    attachment = attachments[0]
    blogfile = BlogFile(
        blogitem=post,
        title=subject.strip(),
    )
    content = StringIO(attachment.read())
    f = File(content, name=attachment.name())
    f.size = attachment.content_length()
    blogfile.file.save(attachment.name(), f, save=True)
    blogfile.save()
    return http.HttpResponse("OK\n")
Exemple #2
0
 def setUp(self):
     json_data = open('tests/fixtures/valid_http_post.json').read()
     self.inbound = PostmarkInbound(json=json_data)