Attachment 'CollectListsCategory.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 u"""
3 MoinMoin - CollectListsCategory Version 0.2 ALPHA
4 macro to collect data from definition lists from CategoryFoo
5 into a databrowser widget table
6
7 Examples:
8 * <<CollectLists>>
9
10 == Description ==
11 will create a table column for every subpage of the current page and fills in
12 the data from each key value pair of a definition list.
13 Optional you can give the template page the definition list page depends on
14 or the column_heading. In the latter case the order is used.
15 Also it can optionally use definition list from another pagename.
16 By setting optional a parser one can use e.g. wikimarkup from a different
17 parser than moinmoin.
18 By filter_selection you can optional use the filter method of the databrowser
19 widget (this needs javascript enabled).
20 By using a different filter_column_value than the default, eg. name:: Cohen you
21 get only rows shown where that name was found.
22 By using the keyword transpose the table is shown transposed. Until transpose is not part
23 of the databrowser widget itselfs we don't support filter_selection by transpose=True.
24
25 Dependencies:
26 * SortableTables (JavaScript) http://moinmo.in/FeatureRequests/SortableTables
27
28 ToDo:
29 * CleaningUp & check Code for unwanted subpages features
30 * hope that the gui will oneday suppport dicts in moinmoin markup "field:: data"
31
32 @copyright: 2006 by michael cohen <scudette@users.sourceforge.net> (PageDicts)
33 @copyright: 2008-2009 by MoinMoin:ReimarBauer (completly rewritten)
34 @copyright: 2009 by MoinMoin:MarcelHäfner (minor rewrite to use only category pages)
35 @license: GNU GPL, see COPYING for details.
36
37 """
38 import re
39 from MoinMoin import wikiutil
40 from MoinMoin.Page import Page
41 from MoinMoin.util.dataset import TupleDataset, Column
42 from MoinMoin.wikidicts import Dict
43 from MoinMoin.widget.browser import DataBrowserWidget
44 from MoinMoin.search import searchPages
45
46 Dependencies = ["pages"]
47
48 def macro_CollectListsCategory(macro, pagename=unicode,
49 align=("left", "center", "right"),
50 column_heading=u'', template=u'',
51 transpose=False,
52 filter_column_value=u'',
53 parser=u'text_moin_wiki',
54 filter_selection=u'NeverExistingDefaultFilter'):
55
56 """
57 currently we don't support transpose together with filter_selection
58 """
59 request = macro.request
60 formatter = macro.formatter
61 _ = request.getText
62
63 try:
64 WikiParser = wikiutil.importPlugin(request.cfg, 'parser', parser, function="Parser")
65 except wikiutil.PluginMissingError:
66 WikiParser = None
67
68 if not pagename:
69 pagename = formatter.page.page_name
70
71 if filter_column_value and ':: ' in filter_column_value:
72 filter_key, filter_word = filter_column_value.split('::')
73 filter_key = filter_key.strip()
74 filter_word = filter_word.strip()
75 else:
76 # Don't filter if syntax was wrong
77 filter_column_value = u''
78
79 #needle = '^%s/(.*)' % pagename
80 needle = '^%s(.*)' % pagename
81 filterfn = re.compile(needle).search
82 pages = request.rootpage.getPageList(exists=1, filter=filterfn)
83 if not pages:
84 return _("Page '%(new_pagename)s' does not exist or you don't have enough rights.") % {"new_pagename": pagename}
85
86 # ignore Template pages
87 filterfn = request.cfg.cache.page_template_regexact.search
88 templates = request.rootpage.getPageList(filter=filterfn)
89
90 needle = "cat:%s" % pagename
91 searchresult = searchPages(request, needle)
92 subpages = []
93 for title in searchresult.hits:
94 if not wikiutil.isSystemPage(request, title.page_name) or not title.page.getPageStatus()[0]:
95 subpages.append(title.page_name)
96
97 if not subpages:
98 return _("Subpage of '%(pagename)s' does not exist or you don't have enough rights.") % {"pagename": pagename}
99 subpages.sort()
100
101 # use selection and order
102 if column_heading:
103 column_heading_keys = [key.strip() for key in column_heading.split(',')]
104 # use keys from template page
105 elif Page(request, template).exists():
106 page = Page(request, template)
107 page_dict = Dict(request, template)
108 column_heading_keys = page_dict.keys()
109 else:
110 # fallback use the keys of the first subpage
111 page = Page(request, subpages[0])
112 page_dict = Dict(request, subpages[0])
113 column_heading_keys = page_dict.keys()
114
115 data = TupleDataset()
116 data.columns = []
117 data.columns.extend([Column(pagename.strip('/'), label=pagename.strip('/'), 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 child = name
142 link = page.link_to(request, text="%s" % child)
143 data.addRow([link] + row)
144 if transpose:
145 data.columns.extend([Column(link, label=link, align=align)])
146
147 if transpose:
148 data.data = map(None, zip(*data.data))
149 data.data = data.data[1:]
150 else:
151 # transpose does not support filter_selection
152 if filter_selection:
153 filtercols = filter_selection.split(',')
154 for key in column_heading_keys:
155 key = key.strip()
156 if filter_selection != u'NeverExistingDefaultFilter' and key in filtercols:
157 data.columns.append(Column(key, autofilter=(key in filtercols)))
158 else:
159 data.columns.extend([Column(key, label=key, align=align)])
160
161 table = DataBrowserWidget(request)
162 table.setData(data)
163 html = ''.join(table.format(method='GET'))
164 # seems to work together with
165 # http://moinmo.in/FeatureRequests/SortableTables?action=AttachFile&do=view&target=common.js.patch
166 html = html.replace('id="dbw.table"', 'class="sortable" id="dbw.table"')
167 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.You are not allowed to attach a file to this page.