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 if request.user and request.user.valid: 67 update["Moin-User"] = request.user.name 68 69 self.store.append(update.as_string()) 70 71 # Update the page. 72 73 else: 74 # NOTE: Should either choose preferred content types or somehow retain them 75 # NOTE: all but present one at a time. 76 77 body = [] 78 replace = to_replace(update) 79 80 for part in parts: 81 mimetype = part.get_content_type() 82 encoding = part.get_content_charset() 83 if mimetype == "text/moin": 84 payload = part.get_payload(decode=True) 85 body.append(encoding and unicode(payload, encoding) or payload) 86 if replace: 87 break 88 89 if not replace: 90 body.append(self.page.get_raw_body()) 91 92 page_editor = PageEditor(request, self.pagename) 93 page_editor.saveText("\n\n".join(body), 0) 94 95 # Refresh the page. 96 97 self.page = Page(request, self.pagename) 98 99 # Action function. 100 101 def execute(pagename, request): 102 PostMessage(pagename, request).do_action() # instead of render 103 104 # vim: tabstop=4 expandtab shiftwidth=4