1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/actions/PostMessage.py Fri Jul 20 21:58:47 2012 +0200
1.3 @@ -0,0 +1,109 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - PostMessage Action
1.7 +
1.8 + @copyright: 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.10 +"""
1.11 +
1.12 +from MoinMoin.PageEditor import PageEditor
1.13 +from MoinSupport import *
1.14 +from email.parser import Parser
1.15 +
1.16 +try:
1.17 + from cStringIO import StringIO
1.18 +except ImportError:
1.19 + from StringIO import StringIO
1.20 +
1.21 +Dependencies = ['pages']
1.22 +
1.23 +class PostMessage:
1.24 +
1.25 + "A posted message handler."
1.26 +
1.27 + def __init__(self, pagename, request):
1.28 +
1.29 + """
1.30 + On the page with the given 'pagename', use the given 'request' when
1.31 + reading posted messages, modifying the Wiki.
1.32 + """
1.33 +
1.34 + self.pagename = pagename
1.35 + self.request = request
1.36 + self.page = Page(request, pagename)
1.37 +
1.38 + def do_action(self):
1.39 + request = self.request
1.40 + content_length = getHeader(request, "Content-Length", "HTTP")
1.41 + if content_length:
1.42 + content_length = int(content_length)
1.43 +
1.44 + # Get the message.
1.45 +
1.46 + message_text = StringIO(request.read(content_length))
1.47 + message = Parser().parse(message_text)
1.48 +
1.49 + # Handle a single part.
1.50 +
1.51 + if not message.is_multipart():
1.52 + self.handle_message_parts([message], to_replace(message))
1.53 +
1.54 + # Handle multiple parts.
1.55 +
1.56 + # This can be a collection of updates, with each update potentially being a
1.57 + # collection of alternative representations.
1.58 +
1.59 + elif is_collection(message):
1.60 + for part in message.get_payload():
1.61 + if part.is_multipart():
1.62 + self.handle_message_parts(part.get_payload(), to_replace(part))
1.63 + else:
1.64 + self.handle_message_parts([part], to_replace(part))
1.65 +
1.66 + # Or it can be a collection of alternative representations for a single
1.67 + # update.
1.68 +
1.69 + else:
1.70 + self.handle_message_parts(message.get_payload(), to_replace(message))
1.71 +
1.72 + # Default output.
1.73 +
1.74 + writeHeaders(request, "text/plain", getMetadata(self.page), "204 No Content")
1.75 +
1.76 + def handle_message_parts(self, parts, replace):
1.77 +
1.78 + """
1.79 + Handle the given message 'parts', replacing the page content if
1.80 + 'replace' is set to a true value.
1.81 + """
1.82 +
1.83 + # NOTE: Should either choose preferred content types or somehow retain them
1.84 + # NOTE: all but present one at a time.
1.85 +
1.86 + body = []
1.87 +
1.88 + for part in parts:
1.89 + mimetype, encoding = getContentTypeAndEncoding(part.get("Content-Type"))
1.90 + if mimetype == "text/moin":
1.91 + body.append(part.get_payload())
1.92 + if replace:
1.93 + break
1.94 +
1.95 + if not replace:
1.96 + body.append(self.page.get_raw_body())
1.97 +
1.98 + page_editor = PageEditor(self.request, self.pagename)
1.99 + page_editor.saveText("\n\n".join(body), 0)
1.100 +
1.101 +def is_collection(message):
1.102 + return message.get("Update-Type") == "Collection"
1.103 +
1.104 +def to_replace(message):
1.105 + return message.get("Update-Action") == "replace"
1.106 +
1.107 +# Action function.
1.108 +
1.109 +def execute(pagename, request):
1.110 + PostMessage(pagename, request).do_action()
1.111 +
1.112 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/tests/test_post.py Fri Jul 20 21:58:47 2012 +0200
2.3 @@ -0,0 +1,30 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +from email.mime.multipart import MIMEMultipart
2.7 +from email.mime.text import MIMEText
2.8 +import httplib
2.9 +import sys
2.10 +
2.11 +if __name__ == "__main__":
2.12 + host = sys.argv[1]
2.13 + path = sys.argv[2] + "?action=PostMessage"
2.14 +
2.15 + message = MIMEMultipart()
2.16 +
2.17 + text1 = MIMEText("An update to the Wiki.", "moin")
2.18 +
2.19 + text2 = MIMEText("Another update to the Wiki.", "moin")
2.20 +
2.21 + message.attach(text1)
2.22 + message.attach(text2)
2.23 +
2.24 + text = message.as_string()
2.25 + print text
2.26 + print
2.27 +
2.28 + req = httplib.HTTPConnection(host)
2.29 + req.request("PUT", path, text) # {"Content-Length" : len(text)}
2.30 + resp = req.getresponse()
2.31 + print resp.read()
2.32 +
2.33 +# vim: tabstop=4 expandtab shiftwidth=4