def construct_table(request, pagelist, metakeys, legend='', checkAccess=True, styles=dict(), options=dict(), parser=None): request.page.formatter = request.formatter formatter = request.formatter _ = request.getText pagename = request.page.page_name row = 0 formatopts = {'tableclass': 'metatable'} # Limit the maximum number of pages displayed pagepathstrip = options.get('pathstrip', 0) try: pagepathstrip = int(pagepathstrip) except ValueError: pagepathstrip = 0 pass if pagepathstrip < 0: pagepathstrip = 0 ## Properties # Default and override properties propdefault = options.get('propdefault', '') propoverride = options.get('propoverride', '') if propoverride: propoverride = get_properties(request, propoverride) if propdefault: propdefault = get_properties(request, propdefault) # Properties dict per key properties = dict() emptyprop = dict().fromkeys(PROPERTIES, '') for key in metakeys: if propoverride: properties[key] = propoverride else: properties[key] = get_properties(request, key) if properties[key] == emptyprop: properties[key] = propdefault # To include only a [link] to page instead of pagename pagelinkonly = options.get('pagelinkonly', 0) # Transpose table, i.e. make table lanscape instead of portrait transpose = options.get('transpose', 0) # Limit the maximum number of pages displayed maxpages = len(pagelist) limit = options.get('limit', 0) try: limit = int(limit) except ValueError: limit = 0 pass if limit > maxpages or limit < 0: limit = 0 if limit: pagelist = pagelist[:limit] if 'width' in options: formatopts = {'tableclass': 'metatable wrap'} formatopts['tablewidth'] = options['width'] # Start table out = list() out.append(formatter.linebreak() + formatter.table(1, attrs=formatopts)) # If the first column is -, do not send page data send_pages = True if metakeys and metakeys[0] == '-': send_pages = False metakeys = metakeys[1:] if metakeys: # Give a class to headers to make it customisable out.append(formatter.table_row(1, {'rowclass': 'meta_header'})) if send_pages: # Upper left cell contains table size or has the desired legend if legend: out.extend(t_cell(request, pagename, [legend], parser=parser)) elif limit: message = ["Showing (%s/%s) pages" % (len(pagelist), maxpages)] out.extend(t_cell(request, pagename, message, parser=parser)) else: out.extend(t_cell(request, pagename, [legend], parser=parser)) def key_cell(request, metas, key, pageobj, styles, properties): out = list() style = styles.get(key, dict()) if key == 'gwikipagename': out.extend(t_cell(request, pageobj, [pageobj.page_name], head=1, style=style, parser=parser)) return out colors = [x.strip() for x in properties if x.startswith('color ')] colormatch = None # Get first color match for color in colors: colorval = properties.get(color) # See that color is valid (either in the colorlist # or a valid hex color) if colorval not in COLORS: if not re.match('#[0-9a-fA-F]{6}', colorval): continue color = color.split()[-1] try: color_p = re.compile(color) except: continue for val in metas[key]: if color_p.match(val): colormatch = colorval if colormatch: break if colormatch: style['bgcolor'] = colormatch out.extend(t_cell(request, pageobj, metas[key], style=style, key=key, parser=parser)) if colormatch: del style['bgcolor'] return out def page_rev_metas(request, page, metakeys, checkAccess): if '-gwikirevision-' in page: metas = get_metas(request, page, metakeys, checkAccess=checkAccess) page, revision = page.split('-gwikirevision-') else: metas = get_metas(request, page, metakeys, checkAccess=checkAccess) revision = '' return metas, page, revision def page_cell(request, pageobj, revision, row, send_pages, pagelinkonly, pagepathstrip): out = list() if row: if row % 2: out.append(formatter.table_row(1, {'rowclass': 'metatable-odd-row'})) else: out.append(formatter.table_row(1, {'rowclass': 'metatable-even-row'})) if send_pages: linktext = '' if pagelinkonly: linktext = _('[link]') out.extend(t_cell(request, pageobj, [pageobj.page_name], head=1, rev=revision, pathstrip=pagepathstrip, linkoverride=linktext, parser=parser)) return out tmp_page = request.page if transpose: for page in pagelist: metas, page, revision = page_rev_metas(request, page, metakeys, checkAccess) pageobj = Page(request, page) request.page = pageobj request.formatter.page = pageobj out.extend(page_cell(request, pageobj, revision, 0, send_pages, pagelinkonly, pagepathstrip)) else: for key in metakeys: style = styles.get(key, dict()) # Styles can modify key naming name = style.get('gwikiname', '').strip('"') if not name and legend and key == 'gwikipagename': name = [legend] # We don't want stuff like bullet lists in out header headerstyle = dict() for st in style: if not st.startswith('gwiki'): headerstyle[st] = style[st] if name: out.extend(t_cell(request, request.page, [name], style=headerstyle, key=key, parser=parser)) else: out.extend(t_cell(request, request.page, [key], style=headerstyle, key=key, parser=parser)) if metakeys: out.append(formatter.table_row(0)) if transpose: for key in metakeys: style = styles.get(key, dict()) # Styles can modify key naming name = style.get('gwikiname', '').strip('"') if not name and legend and key == 'gwikipagename': name = [legend] # We don't want stuff like bullet lists in out header headerstyle = dict() for st in style: if not st.startswith('gwiki'): headerstyle[st] = style[st] if name: out.extend(t_cell(request, tmp_page, [name], style=headerstyle, key=key, parser=parser)) else: out.extend(t_cell(request, tmp_page, [key], style=headerstyle, key=key, parser=parser)) row = row + 1 for page in pagelist: metas, page, revision = page_rev_metas(request, page, [key], checkAccess) pageobj = Page(request, page) request.page = pageobj request.formatter.page = pageobj out.extend(key_cell(request, metas, key, pageobj, styles, properties[key])) out.append(formatter.table_row(0)) else: for page in pagelist: metas, page, revision = page_rev_metas(request, page, metakeys, checkAccess) row = row + 1 pageobj = Page(request, page) request.page = pageobj request.formatter.page = pageobj out.extend(page_cell(request, pageobj, revision, row, send_pages, pagelinkonly, pagepathstrip)) for key in metakeys: out.extend(key_cell(request, metas, key, pageobj, styles, properties[key])) out.append(formatter.table_row(0)) request.page = tmp_page request.formatter.page = tmp_page out.append(formatter.table(0)) return out
def construct_table(request, cache, pagelist, metakeys, legend='', checkAccess=True, styles=dict(), options=dict()): request.page.formatter = request.formatter formatter = request.formatter _ = request.getText pagename = request.page.page_name row = 0 formatopts = {'tableclass': 'metatable'} # Populate icon cache icon_cache = cache.setdefault("icons", dict()) icon_cache["edit"] = request.theme.make_icon('edit') icon_cache["formedit"] = request.theme.make_icon('formedit') # Limit the maximum number of pages displayed pagepathstrip = options.get('pathstrip', 0) try: pagepathstrip = int(pagepathstrip) except ValueError: pagepathstrip = 0 if pagepathstrip < 0: pagepathstrip = 0 # Properties # Default and override properties propdefault = options.get('propdefault', '') propoverride = options.get('propoverride', '') if propoverride: propoverride = get_properties(request, propoverride) if propdefault: propdefault = get_properties(request, propdefault) # Properties dict per key properties = dict() emptyprop = dict().fromkeys(PROPERTIES, '') for key in metakeys: if propoverride: properties[key] = propoverride else: properties[key] = get_properties(request, key) if properties[key] == emptyprop: properties[key] = propdefault # To include only a [link] to page instead of pagename pagelinkonly = options.get('pagelinkonly', 0) # Transpose table, i.e. make table lanscape instead of portrait transpose = options.get('transpose', 0) # Limit the maximum number of pages displayed maxpages = len(pagelist) limit = options.get('limit', 0) try: limit = int(limit) except ValueError: limit = 0 if limit > maxpages or limit < 0: limit = 0 if limit: pagelist = pagelist[:limit] if 'width' in options: formatopts = {'tableclass': 'metatable wrap'} formatopts['tablewidth'] = options['width'] # Start table out = list() out.append(formatter.linebreak() + formatter.table(1, attrs=formatopts)) # If the first column is -, do not send page data send_pages = True if metakeys and metakeys[0] == '-': send_pages = False metakeys = metakeys[1:] if metakeys: # Give a class to headers to make it customisable out.append(formatter.table_row(1, {'rowclass': 'meta_header'})) if send_pages: # Upper left cell contains table size or has the desired legend if legend: out.extend(t_cell(request, cache, pagename, [legend])) elif limit: message = ["Showing (%s/%s) pages" % (len(pagelist), maxpages)] out.extend(t_cell(request, cache, pagename, message)) else: out.extend(t_cell(request, cache, pagename, [legend])) def key_cell(request, cache, metas, key, pageobj, styles, properties): out = list() style = styles.get(key, dict()) if key == 'gwikipagename': out.extend( t_cell(request, cache, pageobj, [pageobj.page_name], head=1, style=style)) return out colors = [x.strip() for x in properties if x.startswith('color ')] colormatch = None # Get first color match for color in colors: colorval = properties.get(color) # See that color is valid (either in the colorlist # or a valid hex color) if colorval not in COLORS: if not re.match('#[0-9a-fA-F]{6}', colorval): continue color = color.split()[-1] try: color_p = re.compile(color) except: continue for val in metas[key]: if color_p.match(val): colormatch = colorval if colormatch: break if colormatch: style['bgcolor'] = colormatch out.extend( t_cell(request, cache, pageobj, metas[key], style=style, key=key)) if colormatch: del style['bgcolor'] return out def page_rev_metas(request, page, metakeys, checkAccess): if '-gwikirevision-' in page: metas = get_metas(request, page, metakeys, checkAccess=checkAccess) page, revision = page.split('-gwikirevision-') else: metas = get_metas(request, page, metakeys, checkAccess=checkAccess) revision = '' return metas, page, revision def page_cell(request, cache, pageobj, revision, row, send_pages, pagelinkonly, pagepathstrip): out = list() if row: if row % 2: out.append( formatter.table_row(1, {'rowclass': 'metatable-odd-row'})) else: out.append( formatter.table_row(1, {'rowclass': 'metatable-even-row'})) if send_pages: linktext = '' if pagelinkonly: linktext = _('[link]') out.extend( t_cell(request, cache, pageobj, [pageobj.page_name], head=1, rev=revision, pathstrip=pagepathstrip, linkoverride=linktext)) return out tmp_page = request.page if transpose: for page in pagelist: metas, page, revision = page_rev_metas(request, page, metakeys, checkAccess) pageobj = Page(request, page) request.page = pageobj request.formatter.page = pageobj out.extend( page_cell(request, cache, pageobj, revision, 0, send_pages, pagelinkonly, pagepathstrip)) else: for key in metakeys: style = styles.get(key, dict()) # Styles can modify key naming name = style.get('gwikiname', '').strip('"') if not name and legend and key == 'gwikipagename': name = [legend] # We don't want stuff like bullet lists in out header headerstyle = dict() for st in style: if not st.startswith('gwiki'): headerstyle[st] = style[st] if name: out.extend( t_cell(request, cache, request.page, [name], style=headerstyle, key=key)) else: out.extend( t_cell(request, cache, request.page, [key], style=headerstyle, key=key)) if metakeys: out.append(formatter.table_row(0)) if transpose: for key in metakeys: style = styles.get(key, dict()) # Styles can modify key naming name = style.get('gwikiname', '').strip('"') if not name and legend and key == 'gwikipagename': name = [legend] # We don't want stuff like bullet lists in out header headerstyle = dict() for st in style: if not st.startswith('gwiki'): headerstyle[st] = style[st] if name: out.extend( t_cell(request, cache, tmp_page, [name], style=headerstyle, key=key)) else: out.extend( t_cell(request, cache, tmp_page, [key], style=headerstyle, key=key)) row = row + 1 for page in pagelist: metas, page, revision = page_rev_metas(request, page, [key], checkAccess) pageobj = Page(request, page) request.page = pageobj request.formatter.page = pageobj out.extend( key_cell(request, cache, metas, key, pageobj, styles, properties[key])) out.append(formatter.table_row(0)) else: for page in pagelist: metas, page, revision = page_rev_metas(request, page, metakeys, checkAccess) row = row + 1 pageobj = Page(request, page) request.page = pageobj request.formatter.page = pageobj out.extend( page_cell(request, cache, pageobj, revision, row, send_pages, pagelinkonly, pagepathstrip)) for key in metakeys: out.extend( key_cell(request, cache, metas, key, pageobj, styles, properties[key])) out.append(formatter.table_row(0)) request.page = tmp_page request.formatter.page = tmp_page out.append(formatter.table(0)) return out
def repl_subfun(mo): dt, pagekey, val = mo.groups() pagekey = form_unescape(pagekey) msg = dt key = pagekey.split(SEPARATOR)[1] properties = get_properties(request, key) values = list() # Placeholder key key if key in vals_on_keys: for keyval in sorted(vals_on_keys[key]): keyval = keyval.strip() # Is this really needed? If so, probably should make the length configurable.. # if len(keyval) > 30: # showval = keyval[:27] + '...' # else: # showval = keyval showval = keyval values.append((keyval, showval)) formtype = properties.get('hint') constraint = properties.get('constraint') desc = properties.get('description') default = properties.get('default', '') hidden = False if formtype == "hidden": hidden = True if not formtype in formtypes: formtype = "selection" if (not formtype == "radio" and not (formtype == "checkbox" and constraint == "existing")): cloneable = "true" else: cloneable = "false" if desc: msg = msg.replace('</dt>', ' %s</dt>' % \ request.formatter.icon('info')) msg = msg.replace('<dt>', wr( '<dt class="mt-tooltip" title="%s" rel="%s">', key, desc)) msg = msg.replace('<dd>', wr('<dd class="metaformedit" data-cloneable="%s" data-default="%s">', cloneable, default)) msg += formtypes[formtype](request, pagekey, val, values) if (not constraint == 'existing' and not formtype in ['textbox', 'textarea', 'file', 'date']): msg += wr('<textarea form="metaformedit" name="%s"></textarea>', pagekey) if hidden: msg = request.formatter.div(1, css_class='comment') + msg + \ request.formatter.div(0) return msg
def execute(pagename, request): form = values_to_form(request.values) util = form.get('util', [None])[0] if util == "format": txt = form.get('text', [None])[0] request.write(format_wikitext(request, txt)) elif util == "getTemplate": template = form.get('name', [None])[0] template_page = wikiutil.unquoteWikiname(template) if request.user.may.read(template_page): Page(request, template_page).send_raw() elif util == "newPage": page = form.get('page', [None])[0] content = form.get('content', [""])[0] request.content_type = "application/json" if request.environ['REQUEST_METHOD'] != 'POST': return if not page: msg = "Page name not defined!" json.dump(dict(status="error", msg=msg), request) return if not request.user.may.write(page): msg = "You are not allowed to edit this page" json.dump(dict(status="error", msg=msg), request) return p = Page(request, page) if p.exists(): msg = "Page already exists" json.dump(dict(status="error", msg=msg), request) return editor = PageEditor(request, page) msg = editor.saveText(content, p.get_real_rev()) json.dump(dict(status="ok", msg=msg), request) elif util == "getProperties": request.content_type = "application/json" key = form.get('key', [''])[0] json.dump(get_properties(request, key), request) return elif util == "uploadFile": request.content_type = "application/json" if not request.user.may.write(pagename): msg = u"You are not allowed to edit this page!" json.dump(dict(status="error", msg=msg), request) request.status_code = 403 return from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists try: overwrite = int(form.get('overwrite', ['0'])[0]) except: overwrite = 0 response = dict(success=list(), failed=list()) for name in request.files: _file = request.files.get(name) filename = _file.filename try: t,s = add_attachment(request, pagename, filename, _file.stream, overwrite=overwrite) response['success'].append(filename) except AttachmentAlreadyExists: response['failed'].append(filename) json.dump(response, request) return elif util == "getAttachments": request.content_type = "application/json" from MoinMoin.action.AttachFile import _get_files, getAttachUrl files = _get_files(request, pagename) response = [] for name in files: response.append(dict(url=getAttachUrl(pagename, name, request),name=name)) json.dump(response, request) return
def execute(pagename, request): form = values_to_form(request.values) util = form.get('util', [None])[0] if util == "format": txt = form.get('text', [None])[0] request.write(format_wikitext(request, txt)) elif util == "getTemplate": template = form.get('name', [None])[0] template_page = wikiutil.unquoteWikiname(template) if request.user.may.read(template_page): Page(request, template_page).send_raw() elif util == "newPage": page = form.get('page', [None])[0] content = form.get('content', [""])[0] request.content_type = "application/json" if request.environ['REQUEST_METHOD'] != 'POST': return if not page: msg = "Page name not defined!" json.dump(dict(status="error", msg=msg), request) return if not request.user.may.write(page): msg = "You are not allowed to edit this page" json.dump(dict(status="error", msg=msg), request) return p = Page(request, page) if p.exists(): msg = "Page already exists" json.dump(dict(status="error", msg=msg), request) return editor = PageEditor(request, page) msg = editor.saveText(content, p.get_real_rev()) json.dump(dict(status="ok", msg=msg), request) elif util == "getProperties": request.content_type = "application/json" key = form.get('key', [''])[0] json.dump(get_properties(request, key), request) return elif util == "uploadFile": request.content_type = "application/json" if not request.user.may.write(pagename): msg = u"You are not allowed to edit this page!" json.dump(dict(status="error", msg=msg), request) request.status_code = 403 return from MoinMoin.action.AttachFile import add_attachment, AttachmentAlreadyExists try: overwrite = int(form.get('overwrite', ['0'])[0]) except: overwrite = 0 response = dict(success=list(), failed=list()) for name in request.files: _file = request.files.get(name) filename = _file.filename try: t, s = add_attachment(request, pagename, filename, _file.stream, overwrite=overwrite) response['success'].append(filename) except AttachmentAlreadyExists: response['failed'].append(filename) json.dump(response, request) return elif util == "getAttachments": request.content_type = "application/json" from MoinMoin.action.AttachFile import _get_files, getAttachUrl files = _get_files(request, pagename) response = [] for name in files: response.append( dict(url=getAttachUrl(pagename, name, request), name=name)) json.dump(response, request) return