paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - PostMessage Action |
paul@0 | 4 | |
paul@16 | 5 | @copyright: 2012, 2013 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@16 | 9 | from MoinMoin.Page import Page |
paul@0 | 10 | from MoinMoin.PageEditor import PageEditor |
paul@29 | 11 | from MoinSupport import getMetadata, writeHeaders |
paul@30 | 12 | from MoinMessage import is_collection, to_replace, to_store |
paul@29 | 13 | from MoinMessageSupport import MoinMessageAction |
paul@0 | 14 | |
paul@0 | 15 | Dependencies = ['pages'] |
paul@0 | 16 | |
paul@29 | 17 | class PostMessage(MoinMessageAction): |
paul@0 | 18 | |
paul@0 | 19 | "A posted message handler." |
paul@0 | 20 | |
paul@30 | 21 | def handle_message_object(self, message): |
paul@5 | 22 | |
paul@30 | 23 | "Handle the given 'message' object." |
paul@3 | 24 | |
paul@1 | 25 | request = self.request |
paul@0 | 26 | |
paul@20 | 27 | # Handle each update. |
paul@20 | 28 | |
paul@15 | 29 | for update in message.updates: |
paul@15 | 30 | |
paul@15 | 31 | # Handle a single part. |
paul@0 | 32 | |
paul@21 | 33 | if not is_collection(update): |
paul@16 | 34 | self.handle_message_parts([update], update) |
paul@0 | 35 | |
paul@15 | 36 | # Or a collection of alternative representations for a single |
paul@15 | 37 | # update. |
paul@0 | 38 | |
paul@15 | 39 | else: |
paul@16 | 40 | self.handle_message_parts(update.get_payload(), update) |
paul@0 | 41 | |
paul@0 | 42 | # Default output. |
paul@0 | 43 | |
paul@0 | 44 | writeHeaders(request, "text/plain", getMetadata(self.page), "204 No Content") |
paul@0 | 45 | |
paul@16 | 46 | def handle_message_parts(self, parts, update): |
paul@0 | 47 | |
paul@0 | 48 | """ |
paul@16 | 49 | Handle the given message 'parts', using the original 'update' to |
paul@16 | 50 | determine whether the content is to replace or update page content, or |
paul@16 | 51 | whether it will be placed in a message store. |
paul@0 | 52 | """ |
paul@0 | 53 | |
paul@16 | 54 | # Handle the different update actions. |
paul@16 | 55 | # Update a message store for the page. |
paul@16 | 56 | |
paul@16 | 57 | if to_store(update): |
paul@20 | 58 | self.store.append(update.as_string()) |
paul@0 | 59 | |
paul@16 | 60 | # Update the page. |
paul@16 | 61 | |
paul@16 | 62 | else: |
paul@16 | 63 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@16 | 64 | # NOTE: all but present one at a time. |
paul@16 | 65 | |
paul@16 | 66 | body = [] |
paul@16 | 67 | replace = to_replace(update) |
paul@0 | 68 | |
paul@16 | 69 | for part in parts: |
paul@16 | 70 | mimetype = part.get_content_type() |
paul@16 | 71 | encoding = part.get_content_charset() |
paul@16 | 72 | if mimetype == "text/moin": |
paul@42 | 73 | payload = part.get_payload(decode=True) |
paul@42 | 74 | body.append(encoding and unicode(payload, encoding) or payload) |
paul@16 | 75 | if replace: |
paul@16 | 76 | break |
paul@0 | 77 | |
paul@16 | 78 | if not replace: |
paul@16 | 79 | body.append(self.page.get_raw_body()) |
paul@0 | 80 | |
paul@16 | 81 | page_editor = PageEditor(self.request, self.pagename) |
paul@16 | 82 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 83 | |
paul@16 | 84 | # Refresh the page. |
paul@11 | 85 | |
paul@16 | 86 | self.page = Page(self.request, self.pagename) |
paul@11 | 87 | |
paul@0 | 88 | # Action function. |
paul@0 | 89 | |
paul@0 | 90 | def execute(pagename, request): |
paul@12 | 91 | PostMessage(pagename, request).do_action() # instead of render |
paul@0 | 92 | |
paul@0 | 93 | # vim: tabstop=4 expandtab shiftwidth=4 |