paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - PostMessage Action |
paul@0 | 4 | |
paul@0 | 5 | @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@0 | 9 | from MoinMoin.PageEditor import PageEditor |
paul@0 | 10 | from MoinSupport import * |
paul@0 | 11 | from email.parser import Parser |
paul@0 | 12 | |
paul@0 | 13 | try: |
paul@0 | 14 | from cStringIO import StringIO |
paul@0 | 15 | except ImportError: |
paul@0 | 16 | from StringIO import StringIO |
paul@0 | 17 | |
paul@0 | 18 | Dependencies = ['pages'] |
paul@0 | 19 | |
paul@0 | 20 | class PostMessage: |
paul@0 | 21 | |
paul@0 | 22 | "A posted message handler." |
paul@0 | 23 | |
paul@0 | 24 | def __init__(self, pagename, request): |
paul@0 | 25 | |
paul@0 | 26 | """ |
paul@0 | 27 | On the page with the given 'pagename', use the given 'request' when |
paul@0 | 28 | reading posted messages, modifying the Wiki. |
paul@0 | 29 | """ |
paul@0 | 30 | |
paul@0 | 31 | self.pagename = pagename |
paul@0 | 32 | self.request = request |
paul@0 | 33 | self.page = Page(request, pagename) |
paul@0 | 34 | |
paul@0 | 35 | def do_action(self): |
paul@0 | 36 | request = self.request |
paul@0 | 37 | content_length = getHeader(request, "Content-Length", "HTTP") |
paul@0 | 38 | if content_length: |
paul@0 | 39 | content_length = int(content_length) |
paul@0 | 40 | |
paul@0 | 41 | # Get the message. |
paul@0 | 42 | |
paul@0 | 43 | message_text = StringIO(request.read(content_length)) |
paul@0 | 44 | message = Parser().parse(message_text) |
paul@0 | 45 | |
paul@0 | 46 | # Handle a single part. |
paul@0 | 47 | |
paul@0 | 48 | if not message.is_multipart(): |
paul@0 | 49 | self.handle_message_parts([message], to_replace(message)) |
paul@0 | 50 | |
paul@0 | 51 | # Handle multiple parts. |
paul@0 | 52 | |
paul@0 | 53 | # This can be a collection of updates, with each update potentially being a |
paul@0 | 54 | # collection of alternative representations. |
paul@0 | 55 | |
paul@0 | 56 | elif is_collection(message): |
paul@0 | 57 | for part in message.get_payload(): |
paul@0 | 58 | if part.is_multipart(): |
paul@0 | 59 | self.handle_message_parts(part.get_payload(), to_replace(part)) |
paul@0 | 60 | else: |
paul@0 | 61 | self.handle_message_parts([part], to_replace(part)) |
paul@0 | 62 | |
paul@0 | 63 | # Or it can be a collection of alternative representations for a single |
paul@0 | 64 | # update. |
paul@0 | 65 | |
paul@0 | 66 | else: |
paul@0 | 67 | self.handle_message_parts(message.get_payload(), to_replace(message)) |
paul@0 | 68 | |
paul@0 | 69 | # Default output. |
paul@0 | 70 | |
paul@0 | 71 | writeHeaders(request, "text/plain", getMetadata(self.page), "204 No Content") |
paul@0 | 72 | |
paul@0 | 73 | def handle_message_parts(self, parts, replace): |
paul@0 | 74 | |
paul@0 | 75 | """ |
paul@0 | 76 | Handle the given message 'parts', replacing the page content if |
paul@0 | 77 | 'replace' is set to a true value. |
paul@0 | 78 | """ |
paul@0 | 79 | |
paul@0 | 80 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@0 | 81 | # NOTE: all but present one at a time. |
paul@0 | 82 | |
paul@0 | 83 | body = [] |
paul@0 | 84 | |
paul@0 | 85 | for part in parts: |
paul@0 | 86 | mimetype, encoding = getContentTypeAndEncoding(part.get("Content-Type")) |
paul@0 | 87 | if mimetype == "text/moin": |
paul@0 | 88 | body.append(part.get_payload()) |
paul@0 | 89 | if replace: |
paul@0 | 90 | break |
paul@0 | 91 | |
paul@0 | 92 | if not replace: |
paul@0 | 93 | body.append(self.page.get_raw_body()) |
paul@0 | 94 | |
paul@0 | 95 | page_editor = PageEditor(self.request, self.pagename) |
paul@0 | 96 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 97 | |
paul@0 | 98 | def is_collection(message): |
paul@0 | 99 | return message.get("Update-Type") == "Collection" |
paul@0 | 100 | |
paul@0 | 101 | def to_replace(message): |
paul@0 | 102 | return message.get("Update-Action") == "replace" |
paul@0 | 103 | |
paul@0 | 104 | # Action function. |
paul@0 | 105 | |
paul@0 | 106 | def execute(pagename, request): |
paul@0 | 107 | PostMessage(pagename, request).do_action() |
paul@0 | 108 | |
paul@0 | 109 | # vim: tabstop=4 expandtab shiftwidth=4 |