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@20 | 11 | from MoinMoin import wikiutil |
paul@29 | 12 | from MoinSupport import getMetadata, writeHeaders |
paul@29 | 13 | from MoinMessage import Message, is_collection, to_replace, to_store |
paul@29 | 14 | from MoinMessageSupport import MoinMessageAction |
paul@20 | 15 | import time |
paul@0 | 16 | |
paul@0 | 17 | Dependencies = ['pages'] |
paul@0 | 18 | |
paul@29 | 19 | class PostMessage(MoinMessageAction): |
paul@0 | 20 | |
paul@0 | 21 | "A posted message handler." |
paul@0 | 22 | |
paul@15 | 23 | def handle_message_content(self, content): |
paul@5 | 24 | |
paul@15 | 25 | "Handle the given message 'content'." |
paul@3 | 26 | |
paul@1 | 27 | request = self.request |
paul@0 | 28 | |
paul@15 | 29 | # Interpret the content as one or more updates. |
paul@0 | 30 | |
paul@15 | 31 | message = Message() |
paul@15 | 32 | message.handle_message(content) |
paul@0 | 33 | |
paul@20 | 34 | # Test any date against the page or message store. |
paul@20 | 35 | |
paul@20 | 36 | if message.date: |
paul@20 | 37 | store_date = time.gmtime(self.store.mtime()) |
paul@20 | 38 | page_date = time.gmtime(wikiutil.version2timestamp(self.page.mtime_usecs())) |
paul@20 | 39 | last_date = max(store_date, page_date) |
paul@20 | 40 | |
paul@20 | 41 | # Reject messages older than the page date. |
paul@20 | 42 | |
paul@20 | 43 | if message.date < last_date: |
paul@20 | 44 | writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden") |
paul@20 | 45 | request.write("The message is too old: %s versus %s." % (message.date, last_date)) |
paul@20 | 46 | return |
paul@20 | 47 | |
paul@20 | 48 | # Reject messages without dates if so configured. |
paul@20 | 49 | |
paul@20 | 50 | elif getattr(request.cfg, "moinmessage_reject_messages_without_dates", True): |
paul@20 | 51 | writeHeaders(request, "text/plain", getMetadata(self.page), "403 Forbidden") |
paul@20 | 52 | request.write("The message has no date information.") |
paul@20 | 53 | return |
paul@20 | 54 | |
paul@20 | 55 | # Handle each update. |
paul@20 | 56 | |
paul@15 | 57 | for update in message.updates: |
paul@15 | 58 | |
paul@15 | 59 | # Handle a single part. |
paul@0 | 60 | |
paul@21 | 61 | if not is_collection(update): |
paul@16 | 62 | self.handle_message_parts([update], update) |
paul@0 | 63 | |
paul@15 | 64 | # Or a collection of alternative representations for a single |
paul@15 | 65 | # update. |
paul@0 | 66 | |
paul@15 | 67 | else: |
paul@16 | 68 | self.handle_message_parts(update.get_payload(), update) |
paul@0 | 69 | |
paul@0 | 70 | # Default output. |
paul@0 | 71 | |
paul@0 | 72 | writeHeaders(request, "text/plain", getMetadata(self.page), "204 No Content") |
paul@0 | 73 | |
paul@16 | 74 | def handle_message_parts(self, parts, update): |
paul@0 | 75 | |
paul@0 | 76 | """ |
paul@16 | 77 | Handle the given message 'parts', using the original 'update' to |
paul@16 | 78 | determine whether the content is to replace or update page content, or |
paul@16 | 79 | whether it will be placed in a message store. |
paul@0 | 80 | """ |
paul@0 | 81 | |
paul@16 | 82 | # Handle the different update actions. |
paul@16 | 83 | # Update a message store for the page. |
paul@16 | 84 | |
paul@16 | 85 | if to_store(update): |
paul@20 | 86 | self.store.append(update.as_string()) |
paul@0 | 87 | |
paul@16 | 88 | # Update the page. |
paul@16 | 89 | |
paul@16 | 90 | else: |
paul@16 | 91 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@16 | 92 | # NOTE: all but present one at a time. |
paul@16 | 93 | |
paul@16 | 94 | body = [] |
paul@16 | 95 | replace = to_replace(update) |
paul@0 | 96 | |
paul@16 | 97 | for part in parts: |
paul@16 | 98 | mimetype = part.get_content_type() |
paul@16 | 99 | encoding = part.get_content_charset() |
paul@16 | 100 | if mimetype == "text/moin": |
paul@16 | 101 | body.append(part.get_payload()) |
paul@16 | 102 | if replace: |
paul@16 | 103 | break |
paul@0 | 104 | |
paul@16 | 105 | if not replace: |
paul@16 | 106 | body.append(self.page.get_raw_body()) |
paul@0 | 107 | |
paul@16 | 108 | page_editor = PageEditor(self.request, self.pagename) |
paul@16 | 109 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 110 | |
paul@16 | 111 | # Refresh the page. |
paul@11 | 112 | |
paul@16 | 113 | self.page = Page(self.request, self.pagename) |
paul@11 | 114 | |
paul@0 | 115 | # Action function. |
paul@0 | 116 | |
paul@0 | 117 | def execute(pagename, request): |
paul@12 | 118 | PostMessage(pagename, request).do_action() # instead of render |
paul@0 | 119 | |
paul@0 | 120 | # vim: tabstop=4 expandtab shiftwidth=4 |