# HG changeset patch # User Paul Boddie # Date 1362874788 -3600 # Node ID e09466179fcba618df5461f21805316d935cd74e # Parent 959d119cf5f428888417f381a67483e460993719 Added a macro to show messages in a message store. diff -r 959d119cf5f4 -r e09466179fcb macros/ShowMessages.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/macros/ShowMessages.py Sun Mar 10 01:19:48 2013 +0100 @@ -0,0 +1,106 @@ +# -*- coding: iso-8859-1 -*- +""" + MoinMoin - ShowMessages Macro + + @copyright: 2012, 2013 by Paul Boddie + @license: GNU GPL (v2 or later), see COPYING.txt for details. +""" + +from MoinSupport import \ + ItemStore, get_form, parseMacroArguments, getParsersForContentType, formatText +from email.parser import Parser + +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + +Dependencies = ['pages'] + +# Macro functions. + +def execute(macro, args): + + """ + Execute the 'macro' with the given 'args' to produce a list of messages from + a message store associated with the current page. + """ + + request = macro.request + fmt = macro.formatter + page = fmt.page + _ = request.getText + + # Interpret the arguments. + + parsed_args = dict(parseMacroArguments(args)) + fragment = parsed_args.get("fragment") + + # Obtain any request parameters corresponding to the field. + # NOTE: Get selection of representations for display. + + form = get_form(request) + + # Show the messages. + # NOTE: Support additional access control. + + store = ItemStore(page, ("messages",), ("message-locks",)) + + output = [] + output.append(fmt.div(on=1, css_class="showmessages-messages")) + + for message_text in iter(store).reversed(): + message = Parser().parse(StringIO(message_text)) + + # Determine whether the message has several representations. + + if not message.is_multipart(): + parts = [message] + else: + parts = message.get_payload() + + # Show the parts. + + output.append(fmt.div(on=1, css_class="showmessages-message")) + first = True + + for part in parts: + + # Set the appearance of the part. + + extra_flags = first and " showmessages-shown-part" or "" + css_class = "showmessages-message-part%s" % extra_flags + + output.append(fmt.div(on=1, css_class=css_class)) + + # Find a parser for the content type. + + mimetype = part.get_content_type() + parsers = getParsersForContentType(request.cfg, mimetype) + + if parsers: + for parser_cls in parsers: + + # NOTE: Could check for output_mimetypes and use formatForOutputType. + # Emit the parsed content. + + output.append(formatText(part.get_payload(), request, fmt, parser_cls=parser_cls)) + break + else: + output.append(fmt.text(_("Update cannot be shown for content of type %s.") % mimetype)) + + # End message part. + + output.append(fmt.div(on=0)) + first = False + + # End message. + + output.append(fmt.div(on=0)) + + # End message list. + + output.append(fmt.div(on=0)) + return u"".join(output) + +# vim: tabstop=4 expandtab shiftwidth=4