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 request = self.request 55 56 # Handle the different update actions. 57 # Update a message store for the page. 58 59 if to_store(update): 60 61 # Add any authenticated user. 62 # Note that where messages are signed by the real author, encrypted, 63 # and then signed for sending, the authenticated user here is not 64 # the real author. 65 66 update["Moin-User"] = request.user and request.user.valid and request.user.name or None 67 68 self.store.append(update.as_string()) 69 70 # Update the page. 71 72 else: 73 # NOTE: Should either choose preferred content types or somehow retain them 74 # NOTE: all but present one at a time. 75 76 body = [] 77 replace = to_replace(update) 78 79 for part in parts: 80 mimetype = part.get_content_type() 81 encoding = part.get_content_charset() 82 if mimetype == "text/moin": 83 payload = part.get_payload(decode=True) 84 body.append(encoding and unicode(payload, encoding) or payload) 85 if replace: 86 break 87 88 if not replace: 89 body.append(self.page.get_raw_body()) 90 91 page_editor = PageEditor(request, self.pagename) 92 page_editor.saveText("\n\n".join(body), 0) 93 94 # Refresh the page. 95 96 self.page = Page(request, self.pagename) 97 98 # Action function. 99 100 def execute(pagename, request): 101 PostMessage(pagename, request).do_action() # instead of render 102 103 # vim: tabstop=4 expandtab shiftwidth=4