paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinShare library |
paul@0 | 4 | |
paul@17 | 5 | @copyright: 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@17 | 9 | from ContentTypeSupport import getContentPreferences |
paul@17 | 10 | from DateSupport import getCurrentTime |
paul@0 | 11 | from MoinSupport import * |
paul@15 | 12 | from MoinMoin import wikiutil |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@0 | 15 | escape = wikiutil.escape |
paul@16 | 16 | _getFragments = getFragments |
paul@0 | 17 | |
paul@0 | 18 | __version__ = "0.1" |
paul@0 | 19 | |
paul@0 | 20 | # More Moin 1.9 compatibility functions. |
paul@0 | 21 | |
paul@0 | 22 | def has_member(request, groupname, username): |
paul@0 | 23 | if hasattr(request.dicts, "has_member"): |
paul@0 | 24 | return request.dicts.has_member(groupname, username) |
paul@0 | 25 | else: |
paul@0 | 26 | return username in request.dicts.get(groupname, []) |
paul@0 | 27 | |
paul@3 | 28 | # Fragments employ a "moinshare" attribute. |
paul@3 | 29 | |
paul@3 | 30 | fragment_attribute = "moinshare" |
paul@2 | 31 | |
paul@16 | 32 | def getFragments(s): |
paul@2 | 33 | fragments = [] |
paul@16 | 34 | for format, attributes, body in _getFragments(s): |
paul@16 | 35 | if attributes.has_key(fragment_attribute): |
paul@16 | 36 | fragments.append((format, attributes, body)) |
paul@2 | 37 | return fragments |
paul@2 | 38 | |
paul@9 | 39 | def getOutputTypes(request, format): |
paul@9 | 40 | |
paul@9 | 41 | """ |
paul@9 | 42 | Using the 'request' and the 'format' of a fragment, return the media types |
paul@9 | 43 | available for the fragment. |
paul@9 | 44 | """ |
paul@9 | 45 | |
paul@9 | 46 | # This uses an extended parser API method if available. |
paul@9 | 47 | |
paul@9 | 48 | parser = getParserClass(request, format) |
paul@9 | 49 | if hasattr(parser, "getOutputTypes"): |
paul@9 | 50 | return parser.getOutputTypes() |
paul@9 | 51 | else: |
paul@9 | 52 | return ["text/html"] |
paul@9 | 53 | |
paul@9 | 54 | def getPreferredOutputTypes(request, mimetypes): |
paul@9 | 55 | |
paul@9 | 56 | """ |
paul@9 | 57 | Using the 'request', perform content negotiation, obtaining mimetypes common |
paul@9 | 58 | to the fragment (given by 'mimetypes') and the client (found in the Accept |
paul@9 | 59 | header). |
paul@9 | 60 | """ |
paul@9 | 61 | |
paul@9 | 62 | accept = getHeader(request, "Accept", "HTTP") |
paul@12 | 63 | if accept: |
paul@12 | 64 | prefs = getContentPreferences(accept) |
paul@12 | 65 | return prefs.get_preferred_types(mimetypes) |
paul@12 | 66 | else: |
paul@12 | 67 | return mimetypes |
paul@9 | 68 | |
paul@9 | 69 | def getUpdatedTime(metadata): |
paul@9 | 70 | |
paul@9 | 71 | """ |
paul@9 | 72 | Return the last updated time based on the given 'metadata', using the |
paul@9 | 73 | current time if no explicit last modified time is specified. |
paul@9 | 74 | """ |
paul@9 | 75 | |
paul@9 | 76 | # NOTE: We could attempt to get the last edit time of a fragment. |
paul@9 | 77 | |
paul@9 | 78 | latest_timestamp = metadata.get("last-modified") |
paul@9 | 79 | if latest_timestamp: |
paul@9 | 80 | return latest_timestamp.as_ISO8601_datetime_string() |
paul@9 | 81 | else: |
paul@9 | 82 | return getCurrentTime().as_ISO8601_datetime_string() |
paul@9 | 83 | |
paul@0 | 84 | # vim: tabstop=4 expandtab shiftwidth=4 |