Attachment 'CollectLists.py.neu.mod'

Download

   1 # -*- coding: utf-8 -*-
   2 """
   3     MoinMoin - macro to collect data from definition lists from subpages
   4     into a databrowser widget table
   5 
   6     <<CollectLists>>
   7     will create a table column for every subpage of the current page and fills in
   8     the data from each key value pair of a definition list.
   9     Optional you can give the template page the definition list page depends on
  10     or the column_heading. In the latter case the order is used.
  11     Also it can optionally use definition list from another pagename.
  12     By setting optional a parser one can use e.g. wikimarkup from a different
  13     parser than moinmoin.
  14     By filter_selection you can optional use the filter method of the databrowser
  15     widget (this needs javascript enabled).
  16     By using a different filter_column_value than the default, eg. name:: Cohen  you
  17     get only rows shown where that name was found.
  18     By using the keyword transpose the table is shown transposed. Until transpose is not part
  19     of the databrowser widget itselfs we don't support filter_selection by transpose=True.
  20     * adapted by JV on basis of modification suggested by MarcelHaefner to accept the variable category_name instead of
  21       deriving that from the pagename where the macro is called (too inflexible)*
  22     @copyright: 2006 by michael cohen <scudette@users.sourceforge.net> (PageDicts)
  23     @copyright: 2008-2009 by MoinMoin:ReimarBauer (completly rewritten)
  24     @license: GNU GPL, see COPYING for details.
  25 """
  26 import re
  27 from MoinMoin import wikiutil
  28 from MoinMoin.Page import Page
  29 from MoinMoin.util.dataset import TupleDataset, Column
  30 from MoinMoin.wikidicts import Dict
  31 from MoinMoin.widget.browser import DataBrowserWidget
  32 from MoinMoin.search import searchPages
  33 
  34 Dependencies = ["pages"]
  35 
  36 def macro_CollectLists(macro, pagename=unicode,
  37                       align=("left", "center", "right"),
  38                       column_heading=u'', template=u'',
  39                       transpose=False,
  40                       filter_column_value=u'',
  41                       parser=u'text_moin_wiki',
  42                       filter_selection=u'NeverExistingDefaultFilter',
  43                       category_name=u''):
  44 
  45     """
  46     currently we don't support transpose together with filter_selection
  47     """
  48     request = macro.request
  49     formatter = macro.formatter
  50     _ = request.getText
  51 
  52     try:
  53         WikiParser = wikiutil.importPlugin(request.cfg, 'parser', parser, function="Parser")
  54     except wikiutil.PluginMissingError:
  55         WikiParser = None
  56 
  57     if not pagename:
  58         pagename = formatter.page.page_name
  59 
  60     if filter_column_value and ':: ' in filter_column_value:
  61         filter_key, filter_word = filter_column_value.split('::')
  62         filter_key = filter_key.strip()
  63         filter_word = filter_word.strip()
  64     else:
  65         # Don't filter if syntax was wrong
  66         filter_column_value = u''
  67 
  68     #needle = '^%s/(.*)' % pagename
  69     #Marcel: because there is no child or subpage under the pagename exist, so no / to search for
  70     # needle = '^%s(.*)' % pagename
  71     # filterfn = re.compile(needle).search
  72     # pages = request.rootpage.getPageList(exists=1, filter=filterfn)
  73     # if not pages:
  74     #    return _("Page '%(new_pagename)s' does not exist or you don't have enough rights.") % {"new_pagename": pagename}
  75 
  76     # ignore Template pages ; lower two lines superfluous JV
  77     # filterfn = request.cfg.cache.page_template_regexact.search
  78     # templates = request.rootpage.getPageList(filter=filterfn)
  79     #subpages = [page for page in pages if page not in templates]
  80     #Marcel: subpages are not generated from the parent/main pages
  81     #JV I put below in if then clause to allow to search for category_name
  82     #Marcel:s earching now for the category pages = cat:CategogoryName
  83     if category_name==u'':
  84         needle = "cat:%s" % pagename
  85     else:
  86         needle = "cat:%s" % category_name
  87     searchresult =  searchPages(request, needle)
  88     subpages = []
  89     for title in searchresult.hits:
  90         # if not wikiutil.isSystemPage(request, title.page_name) or not title.page.getPageStatus()[0]: 
  91 	# upper line replaced by JV
  92 	if (not wikiutil.isSystemPage(request, title.page_name)) and (not title.page.getPageStatus()[0]):
  93             subpages.append(title.page_name)
  94             
  95     if not subpages:
  96         return _("Subpage of '%(pagename)s' does not exist or you don't have enough rights.") % {"pagename": pagename}
  97     subpages.sort()
  98 
  99     # use selection and order
 100     if column_heading:
 101         column_heading_keys = [key.strip() for key in column_heading.split(',')]
 102     # use keys from template page
 103     elif Page(request, template).exists():
 104         page = Page(request, template)
 105         page_dict = Dict(request, template)
 106         column_heading_keys = page_dict.keys()
 107     else:
 108         # fallback use the keys of the first subpage
 109         page = Page(request, subpages[0])
 110         page_dict = Dict(request, subpages[0])
 111         column_heading_keys = page_dict.keys()
 112 
 113     data = TupleDataset()
 114     data.columns = []
 115     #JV trying here to get rid of the column heading being name after the page
 116     # data.columns.extend([Column(pagename.strip('/'), label=pagename.strip('/'), align=align)])
 117     data.columns.extend([Column('Page', label='Page', align=align)])
 118     # may be transpose should be moved into the databrowser widget
 119     if transpose:
 120         data.addRow([pagename.strip('/')] + column_heading_keys)
 121 
 122     for name in subpages:
 123         page = Page(request, name)
 124         page_dict = Dict(request, name)
 125         if filter_column_value and page_dict.get(filter_key, '') != filter_word:
 126             continue
 127         row = []
 128         keep = False
 129         for key in column_heading_keys:
 130             if key in page_dict.keys():
 131                 value = page_dict.get(key, '')
 132                 if WikiParser:
 133                     # xxx check how our brand new Image class solves this
 134                     if parser == u'text_moin_wiki':
 135                         value = value.replace('attachment:', 'attachment:%s/' % name)
 136                     row.append((wikiutil.renderText(request, WikiParser, value), wikiutil.escape(value, 1)))
 137                 else:
 138                     row.append((wikiutil.escape(value, 1), wikiutil.escape(value, 1)))
 139             else:
 140                 row.append('')
 141         #parent, child = name.split('/', 1)
 142         #Marcel: because the categorypage is not the parent page of the wanted page
 143         parent = pagename #there is no need for this string... but for to stay compatible let it be
 144 	# JV don't want this parent as column heading
 145 	# parent = u'Page' doesn't work
 146         child = name
 147         
 148         #link = page.link_to(request, text="/%s" % child)
 149         #Marcel: don't want the / before every pagelink
 150         link = page.link_to(request, text="%s" % child)
 151         data.addRow([link] + row)
 152         if transpose:
 153             data.columns.extend([Column(link, label=link, align=align)])
 154 
 155     if transpose:
 156         data.data = map(None, zip(*data.data))
 157         data.data = data.data[1:]
 158     else:
 159         # transpose does not support filter_selection
 160         if filter_selection:
 161             filtercols = filter_selection.split(',')
 162             for key in column_heading_keys:
 163                 key = key.strip()
 164                 if filter_selection != u'NeverExistingDefaultFilter' and key in filtercols:
 165                     data.columns.append(Column(key, autofilter=(key in filtercols)))
 166                 else:
 167                     data.columns.extend([Column(key, label=key, align=align)])
 168 
 169     table = DataBrowserWidget(request)
 170     table.setData(data)
 171     html = ''.join(table.format(method='GET'))
 172     # seems to work together with
 173     # http://moinmo.in/FeatureRequests/SortableTables?action=AttachFile&do=view&target=common.js.patch
 174     # html = html.replace('id="dbw.table', 'class="sortable" id="dbw.table')
 175     return html

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-08-12 03:20:27, 6.9 KB) [[attachment:CollectLists.py.neu]]
  • [get | view] (2009-08-12 03:30:30, 7.7 KB) [[attachment:CollectLists.py.neu.mod]]
  • [get | view] (2009-02-11 07:33:36, 56.0 KB) [[attachment:problem_iex.jpg]]
  • [get | view] (2009-02-04 17:41:12, 3.0 KB) [[attachment:table2dict.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.