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