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@85 | 12 | from MoinMessage import is_collection, to_replace, to_store, get_update_action |
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@85 | 29 | all_successful = True |
paul@85 | 30 | |
paul@15 | 31 | for update in message.updates: |
paul@15 | 32 | |
paul@15 | 33 | # Handle a single part. |
paul@0 | 34 | |
paul@21 | 35 | if not is_collection(update): |
paul@85 | 36 | all_successful = all_successful and self.handle_message_parts([update], update) |
paul@0 | 37 | |
paul@15 | 38 | # Or a collection of alternative representations for a single |
paul@15 | 39 | # update. |
paul@0 | 40 | |
paul@15 | 41 | else: |
paul@85 | 42 | all_successful = all_successful and self.handle_message_parts(update.get_payload(), update) |
paul@0 | 43 | |
paul@0 | 44 | # Default output. |
paul@0 | 45 | |
paul@85 | 46 | writeHeaders(request, "text/plain", getMetadata(self.page), "200 OK") |
paul@85 | 47 | if all_successful: |
paul@85 | 48 | request.write("All updates were successful.") |
paul@85 | 49 | else: |
paul@85 | 50 | request.write("Some updates were unsuccessful.") |
paul@0 | 51 | |
paul@16 | 52 | def handle_message_parts(self, parts, update): |
paul@0 | 53 | |
paul@0 | 54 | """ |
paul@16 | 55 | Handle the given message 'parts', using the original 'update' to |
paul@16 | 56 | determine whether the content is to replace or update page content, or |
paul@16 | 57 | whether it will be placed in a message store. |
paul@0 | 58 | """ |
paul@0 | 59 | |
paul@80 | 60 | request = self.request |
paul@80 | 61 | |
paul@85 | 62 | # Test for privileges to change the page or message store. |
paul@85 | 63 | |
paul@85 | 64 | update_action = get_update_action(update) |
paul@85 | 65 | |
paul@85 | 66 | if not self.can_perform_action(update_action): |
paul@85 | 67 | return False |
paul@85 | 68 | |
paul@16 | 69 | # Handle the different update actions. |
paul@16 | 70 | # Update a message store for the page. |
paul@16 | 71 | |
paul@16 | 72 | if to_store(update): |
paul@80 | 73 | |
paul@80 | 74 | # Add any authenticated user. |
paul@80 | 75 | # Note that where messages are signed by the real author, encrypted, |
paul@80 | 76 | # and then signed for sending, the authenticated user here is not |
paul@80 | 77 | # the real author. |
paul@80 | 78 | |
paul@81 | 79 | update["Moin-User"] = request.user and request.user.valid and request.user.name or None |
paul@80 | 80 | |
paul@20 | 81 | self.store.append(update.as_string()) |
paul@0 | 82 | |
paul@16 | 83 | # Update the page. |
paul@16 | 84 | |
paul@16 | 85 | else: |
paul@16 | 86 | # NOTE: Should either choose preferred content types or somehow retain them |
paul@16 | 87 | # NOTE: all but present one at a time. |
paul@16 | 88 | |
paul@16 | 89 | body = [] |
paul@16 | 90 | replace = to_replace(update) |
paul@0 | 91 | |
paul@16 | 92 | for part in parts: |
paul@16 | 93 | mimetype = part.get_content_type() |
paul@16 | 94 | encoding = part.get_content_charset() |
paul@16 | 95 | if mimetype == "text/moin": |
paul@42 | 96 | payload = part.get_payload(decode=True) |
paul@42 | 97 | body.append(encoding and unicode(payload, encoding) or payload) |
paul@16 | 98 | if replace: |
paul@16 | 99 | break |
paul@0 | 100 | |
paul@16 | 101 | if not replace: |
paul@16 | 102 | body.append(self.page.get_raw_body()) |
paul@0 | 103 | |
paul@80 | 104 | page_editor = PageEditor(request, self.pagename) |
paul@16 | 105 | page_editor.saveText("\n\n".join(body), 0) |
paul@0 | 106 | |
paul@16 | 107 | # Refresh the page. |
paul@11 | 108 | |
paul@80 | 109 | self.page = Page(request, self.pagename) |
paul@11 | 110 | |
paul@85 | 111 | return True |
paul@85 | 112 | |
paul@0 | 113 | # Action function. |
paul@0 | 114 | |
paul@0 | 115 | def execute(pagename, request): |
paul@12 | 116 | PostMessage(pagename, request).do_action() # instead of render |
paul@0 | 117 | |
paul@0 | 118 | # vim: tabstop=4 expandtab shiftwidth=4 |