changeset 5844:3d997b1235fe

action/rss_rc.py: added ability to show attachment-related actions in RSS.
author Eugene Syromyatnikov <evgsyr@gmail.com>
date Wed, 16 May 2012 06:47:10 +0400
parents 89fbca5396a1
children 0e1b4214318f
files MoinMoin/action/rss_rc.py MoinMoin/config/multiconfig.py
diffstat 2 files changed, 102 insertions(+), 49 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/action/rss_rc.py	Wed May 16 06:30:38 2012 +0400
+++ b/MoinMoin/action/rss_rc.py	Wed May 16 06:47:10 2012 +0400
@@ -14,11 +14,16 @@
 from MoinMoin.util import timefuncs
 from MoinMoin.Page import Page
 from MoinMoin.wikixml.util import RssGenerator
+from MoinMoin.action import AttachFile
 
 def full_url(request, page, querystr=None, anchor=None):
     url = page.url(request, anchor=anchor, querystr=querystr)
     return request.getQualifiedURL(url)
 
+def attach_url(request, pagename, filename, do):
+    url = AttachFile.getAttachUrl(pagename, filename, request, do=do)
+    return request.getQualifiedURL(url)
+
 def execute(pagename, request):
     """ Send recent changes as an RSS document
     """
@@ -38,6 +43,7 @@
     ddiffs = getattr(cfg, "rss_ddiffs", 0)
     max_lines = getattr(cfg, "rss_lines_default", 20)
     lines_limit = getattr(cfg, "rss_lines_limit", 100)
+    show_att = getattr(cfg, "rss_show_attachment_entries", 0)
 
     try:
         max_items = min(int(request.values.get('items', max_items)),
@@ -62,6 +68,10 @@
                         lines_limit)
     except ValueError:
         pass
+    try:
+        show_att = int(request.values.get('show_att', show_att))
+    except ValueError:
+        pass
 
     # get data
     log = editlog.EditLog(request)
@@ -72,7 +82,7 @@
     for line in log.reverse():
         if not request.user.may.read(line.pagename):
             continue
-        if (not line.action.startswith('SAVE') or
+        if ((not show_att and not line.action.startswith('SAVE')) or
             ((line.pagename in pages) and unique)): continue
         line.editor = line.getInterwikiEditorData(request)
         line.time = timefuncs.tmtuple(wikiutil.version2timestamp(line.ed_time_usecs)) # UTC
@@ -88,7 +98,8 @@
     del log
 
     timestamp = timefuncs.formathttpdate(lastmod)
-    etag = "%d-%d-%d-%d-%d-%d" % (lastmod, max_items, diffs, ddiffs, unique, max_lines)
+    etag = "%d-%d-%d-%d-%d-%d-%d" % (lastmod, max_items, diffs, ddiffs, unique,
+        max_lines, show_att)
 
     # for 304, we look at if-modified-since and if-none-match headers,
     # one of them must match and the other is either not there or must match.
@@ -142,10 +153,12 @@
             '    Add "lines=nnn" to change maximum number of diff/body lines \n'
             '    to show. Cannot be more than %d.\n'
             '    \n'
+            '    Add "show_att=1" to show items related to attachments.\n'
+            '    \n'
             '    Current settings: items=%i, unique=%i, diffs=%i, ddiffs=%i, \n'
-            '    lines=%i\n'
+            '    lines=%i, show_att=%i\n'
             '-->\n' % (def_max_items, items_limit, lines_limit, max_items,
-                       unique, diffs, ddiffs, max_lines)
+                       unique, diffs, ddiffs, max_lines, show_att)
             )
 
         # emit channel description
@@ -206,58 +219,94 @@
 
             show_diff = diffs
 
-            if action == 'SAVE/REVERT':
-                to_rev = int(item.extra)
-                comment = (_("Revert to revision %(rev)d.") % {
-                    'rev': to_rev}) + "<br />" \
-                    + _("Comment:") + " " + comment
-
-            elif action == 'SAVE/RENAME':
+            if action.startswith('ATT'): # Attachment
                 show_diff = 0
-                comment = (_("Renamed from '%(oldpagename)s'.") % {
-                    'oldpagename': item.extra}) + "<br />" \
-                    + _("Comment:") + " " + comment
-                if item.pagename in pagename_map:
-                    newpage = pagename_map[item.pagename]
-                    del pagename_map[item.pagename]
-                    pagename_map[item.extra] = newpage
-                else:
-                    pagename_map[item.extra] = item.pagename
-
-            elif action == 'SAVENEW':
-                comment = _("New page:\n") + comment
-
-            item_rev = int(item.rev)
+                filename = wikiutil.url_unquote(item.extra)
+                att_exists = AttachFile.exists(request, cur_pagename, filename)
 
-            # If we use diffs/ddiffs, we should calculate proper links and content
-            if ddiffs:
-                # first revision can't have older revisions to diff with
-                if item_rev == 1:
-                    handler.simpleNode('link', full_url(request, page,
-                        querystr={'action': 'recall', 'rev': str(item_rev)}))
-                else:
-                    handler.simpleNode('link', full_url(request, page,
-                        querystr={'action': 'diff',
-                                  'rev1': str(item_rev),
-                                  'rev2': str(item_rev - 1)}))
+                if action == 'ATTNEW':
+                    # Once attachment deleted this link becomes invalid but we
+                    # preserve it to prevent appearance of new RSS entries in
+                    # RSS readers.
+                    if ddiffs:
+                        handler.simpleNode('link', attach_url(request,
+                            cur_pagename, filename, do='get'))
 
-            if show_diff:
-                if item_rev == 1:
-                    lines = Page(request, cur_pagename, rev=item_rev).getlines()
-                else:
-                    lines = wikiutil.pagediff(request, cur_pagename,
-                        item_rev - 1, cur_pagename, item_rev, ignorews=1)
+                    comment = _("Upload of attachment '%(filename)s'.") % {
+                        'filename': filename}
 
-                if len(lines) > max_lines:
-                    lines = lines[:max_lines] + ['...\n']
+                elif action == 'ATTDEL':
+                    if ddiffs:
+                        handler.simpleNode('link', full_url(request, page,
+                            querystr={'action': 'AttachFile'}))
 
-                lines = '\n'.join(lines)
-                lines = wikiutil.escape(lines)
+                    comment = _("Attachment '%(filename)s' deleted.") % {
+                        'filename': filename}
 
-                comment = '%s\n<pre>\n%s\n</pre>\n' % (comment, lines)
+                elif action == 'ATTDRW':
+                    if ddiffs:
+                        handler.simpleNode('link', attach_url(request,
+                            cur_pagename, filename, do='view'))
 
-            if not ddiffs:
-                handler.simpleNode('link', full_url(request, page))
+                    comment = _("Drawing '%(filename)s' saved.") % {
+                        'filename': filename}
+
+            elif action.startswith('SAVE'):
+                if action == 'SAVE/REVERT':
+                    to_rev = int(item.extra)
+                    comment = (_("Revert to revision %(rev)d.") % {
+                        'rev': to_rev}) + "<br />" \
+                        + _("Comment:") + " " + comment
+
+                elif action == 'SAVE/RENAME':
+                    show_diff = 0
+                    comment = (_("Renamed from '%(oldpagename)s'.") % {
+                        'oldpagename': item.extra}) + "<br />" \
+                        + _("Comment:") + " " + comment
+                    if item.pagename in pagename_map:
+                        newpage = pagename_map[item.pagename]
+                        del pagename_map[item.pagename]
+                        pagename_map[item.extra] = newpage
+                    else:
+                        pagename_map[item.extra] = item.pagename
+
+                elif action == 'SAVENEW':
+                    comment = _("New page:\n") + comment
+
+                item_rev = int(item.rev)
+
+                # If we use diffs/ddiffs, we should calculate proper links and
+                # content
+                if ddiffs:
+                    # first revision can't have older revisions to diff with
+                    if item_rev == 1:
+                        handler.simpleNode('link', full_url(request, page,
+                            querystr={'action': 'recall',
+                                      'rev': str(item_rev)}))
+                    else:
+                        handler.simpleNode('link', full_url(request, page,
+                            querystr={'action': 'diff',
+                                      'rev1': str(item_rev),
+                                      'rev2': str(item_rev - 1)}))
+
+                if show_diff:
+                    if item_rev == 1:
+                        lines = Page(request, cur_pagename,
+                            rev=item_rev).getlines()
+                    else:
+                        lines = wikiutil.pagediff(request, cur_pagename,
+                            item_rev - 1, cur_pagename, item_rev, ignorews=1)
+
+                    if len(lines) > max_lines:
+                        lines = lines[:max_lines] + ['...\n']
+
+                    lines = '\n'.join(lines)
+                    lines = wikiutil.escape(lines)
+
+                    comment = '%s\n<pre>\n%s\n</pre>\n' % (comment, lines)
+
+                if not ddiffs:
+                    handler.simpleNode('link', full_url(request, page))
 
             if comment:
                 handler.simpleNode('description', comment)
--- a/MoinMoin/config/multiconfig.py	Wed May 16 06:30:38 2012 +0400
+++ b/MoinMoin/config/multiconfig.py	Wed May 16 06:47:10 2012 +0400
@@ -1307,6 +1307,10 @@
                             "lines URL query parameter of rss_rc action."),
       ('lines_limit', 100, "Limit for possible line count for diffs added as "
                            "item descriptions in RSS."),
+      ('show_attachment_entries', 0, "If set to 1, items, related to "
+                                     "attachment management, would be added to "
+                                     "RSS feed. Can be changed via show_att "
+                                     "URL query parameter of rss_rc action."),
     )),
 }