paul@19 | 1 | # -*- coding: iso-8859-1 -*- |
paul@19 | 2 | """ |
paul@19 | 3 | MoinMoin - ShowMessages Macro |
paul@19 | 4 | |
paul@19 | 5 | @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@19 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@19 | 7 | """ |
paul@19 | 8 | |
paul@19 | 9 | from MoinSupport import \ |
paul@19 | 10 | ItemStore, get_form, parseMacroArguments, getParsersForContentType, formatText |
paul@19 | 11 | from email.parser import Parser |
paul@19 | 12 | |
paul@19 | 13 | try: |
paul@19 | 14 | from cStringIO import StringIO |
paul@19 | 15 | except ImportError: |
paul@19 | 16 | from StringIO import StringIO |
paul@19 | 17 | |
paul@19 | 18 | Dependencies = ['pages'] |
paul@19 | 19 | |
paul@19 | 20 | # Macro functions. |
paul@19 | 21 | |
paul@19 | 22 | def execute(macro, args): |
paul@19 | 23 | |
paul@19 | 24 | """ |
paul@19 | 25 | Execute the 'macro' with the given 'args' to produce a list of messages from |
paul@19 | 26 | a message store associated with the current page. |
paul@19 | 27 | """ |
paul@19 | 28 | |
paul@19 | 29 | request = macro.request |
paul@19 | 30 | fmt = macro.formatter |
paul@19 | 31 | page = fmt.page |
paul@19 | 32 | _ = request.getText |
paul@19 | 33 | |
paul@19 | 34 | # Interpret the arguments. |
paul@19 | 35 | |
paul@19 | 36 | parsed_args = dict(parseMacroArguments(args)) |
paul@19 | 37 | fragment = parsed_args.get("fragment") |
paul@19 | 38 | |
paul@19 | 39 | # Obtain any request parameters corresponding to the field. |
paul@19 | 40 | # NOTE: Get selection of representations for display. |
paul@19 | 41 | |
paul@19 | 42 | form = get_form(request) |
paul@19 | 43 | |
paul@19 | 44 | # Show the messages. |
paul@19 | 45 | # NOTE: Support additional access control. |
paul@19 | 46 | |
paul@19 | 47 | store = ItemStore(page, ("messages",), ("message-locks",)) |
paul@19 | 48 | |
paul@19 | 49 | output = [] |
paul@19 | 50 | output.append(fmt.div(on=1, css_class="showmessages-messages")) |
paul@19 | 51 | |
paul@19 | 52 | for message_text in iter(store).reversed(): |
paul@19 | 53 | message = Parser().parse(StringIO(message_text)) |
paul@19 | 54 | |
paul@19 | 55 | # Determine whether the message has several representations. |
paul@19 | 56 | |
paul@19 | 57 | if not message.is_multipart(): |
paul@19 | 58 | parts = [message] |
paul@19 | 59 | else: |
paul@19 | 60 | parts = message.get_payload() |
paul@19 | 61 | |
paul@19 | 62 | # Show the parts. |
paul@19 | 63 | |
paul@19 | 64 | output.append(fmt.div(on=1, css_class="showmessages-message")) |
paul@19 | 65 | first = True |
paul@19 | 66 | |
paul@19 | 67 | for part in parts: |
paul@19 | 68 | |
paul@19 | 69 | # Set the appearance of the part. |
paul@19 | 70 | |
paul@19 | 71 | extra_flags = first and " showmessages-shown-part" or "" |
paul@19 | 72 | css_class = "showmessages-message-part%s" % extra_flags |
paul@19 | 73 | |
paul@19 | 74 | output.append(fmt.div(on=1, css_class=css_class)) |
paul@19 | 75 | |
paul@19 | 76 | # Find a parser for the content type. |
paul@19 | 77 | |
paul@19 | 78 | mimetype = part.get_content_type() |
paul@19 | 79 | parsers = getParsersForContentType(request.cfg, mimetype) |
paul@19 | 80 | |
paul@19 | 81 | if parsers: |
paul@19 | 82 | for parser_cls in parsers: |
paul@19 | 83 | |
paul@19 | 84 | # NOTE: Could check for output_mimetypes and use formatForOutputType. |
paul@19 | 85 | # Emit the parsed content. |
paul@19 | 86 | |
paul@19 | 87 | output.append(formatText(part.get_payload(), request, fmt, parser_cls=parser_cls)) |
paul@19 | 88 | break |
paul@19 | 89 | else: |
paul@19 | 90 | output.append(fmt.text(_("Update cannot be shown for content of type %s.") % mimetype)) |
paul@19 | 91 | |
paul@19 | 92 | # End message part. |
paul@19 | 93 | |
paul@19 | 94 | output.append(fmt.div(on=0)) |
paul@19 | 95 | first = False |
paul@19 | 96 | |
paul@19 | 97 | # End message. |
paul@19 | 98 | |
paul@19 | 99 | output.append(fmt.div(on=0)) |
paul@19 | 100 | |
paul@19 | 101 | # End message list. |
paul@19 | 102 | |
paul@19 | 103 | output.append(fmt.div(on=0)) |
paul@19 | 104 | return u"".join(output) |
paul@19 | 105 | |
paul@19 | 106 | # vim: tabstop=4 expandtab shiftwidth=4 |