# HG changeset patch # User Paul Boddie # Date 1354402456 -3600 # Node ID 3f974f3a51e717c2f3b26d8e9c2c697f79b1b58c # Parent 601968fcbaf14b7f3045d15b6ee77ec29d35ab63 Moved the action base class into the library. Changed the initialisation of the action to receive both page name and request. diff -r 601968fcbaf1 -r 3f974f3a51e7 MoinForms.py --- a/MoinForms.py Sat Dec 01 23:26:28 2012 +0100 +++ b/MoinForms.py Sat Dec 01 23:54:16 2012 +0100 @@ -6,6 +6,7 @@ @license: GNU GPL (v2 or later), see COPYING.txt for details. """ +from MoinMoin.action import do_show from MoinMoin import wikiutil from MoinSupport import * import re @@ -15,6 +16,101 @@ form_field_regexp_str = r"<>" form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) +# Common action functionality. + +class MoinFormHandlerAction: + + "A handler action that can be specialised for individual forms." + + def __init__(self, pagename, request): + self.pagename = pagename + self.request = request + + def processForm(self): + + """ + Interpret the request details and modify them according to the structure + of the interpreted information. + """ + + # Get the form fields and obtain the hierarchical field structure. + + form = get_form(self.request) + fields = getFields(form, remove=True) + + # Modify, serialise and show the form. + + self.modifyFields(fields) + self.serialiseFields(fields, form) + do_show(self.pagename, self.request) + + def serialiseFields(self, fields, form, path=None): + + """ + Serialise the given 'fields' to the given 'form', using the given 'path' + to name the entries. + """ + + for key, value in fields.items(): + + # Serialise sections. + + if isinstance(value, dict): + for index, element in enumerate(getSectionElements(value)): + element_ref = "%s$%s" % (key, index) + + self.serialiseFields(element, form, + path and ("%s/%s" % (path, element_ref)) or element_ref + ) + + # Serialise fields. + + else: + form[path and ("%s/%s" % (path, key)) or key] = value + + def modifyFields(self, fields): + + "Modify the given 'fields', removing and adding items." + + # First, remove fields. + + for key in fields.keys(): + if key.startswith("_remove="): + self.removeField(key[8:], fields) + + # Then, add fields. + + for key in fields.keys(): + if key.startswith("_add="): + self.addField(key[5:], fields) + + def removeField(self, path, fields): + + """ + Remove the section element indicated by the given 'path' from the + 'fields'. + """ + + section, (name, index) = getSectionForPath(path, fields) + del section[name][index] + + def addField(self, path, fields): + + """ + Add a section element indicated by the given 'path' to the 'fields'. + """ + + section, (name, index) = getSectionForPath(path, fields) + placeholder = {"_new" : ""} + + if section.has_key(name): + indexes = section[name].keys() + max_index = max(map(int, indexes)) + section[name][max_index + 1] = placeholder + else: + max_index = -1 + section[name] = {0 : placeholder} + # Common formatting functions. def formatForm(text, request, fmt, attrs=None, write=None): diff -r 601968fcbaf1 -r 3f974f3a51e7 actions/MoinFormHandler.py --- a/actions/MoinFormHandler.py Sat Dec 01 23:26:28 2012 +0100 +++ b/actions/MoinFormHandler.py Sat Dec 01 23:54:16 2012 +0100 @@ -6,108 +6,13 @@ @license: GNU GPL (v2 or later), see COPYING.txt for details. """ -from MoinMoin.action import do_show -from MoinMoin import config -from MoinForms import * +from MoinForms import MoinFormHandlerAction Dependencies = ['pages'] -class MoinFormHandlerAction: - - "A handler action that can be specialised for individual forms." - - def __init__(self, pagename): - self.pagename = pagename - - def processForm(self, request): - - """ - Interpret the given 'request' details and modify them according to the - structure of the interpreted information. - """ - - # Get the form fields and obtain the hierarchical field structure. - - form = get_form(request) - fields = getFields(form, remove=True) - - # Process modification operations. - - self.modifyFields(fields) - self.serialiseFields(fields, form) - - do_show(self.pagename, request) - - def serialiseFields(self, fields, form, path=None): - - """ - Serialise the given 'fields' to the given 'form', using the given 'path' - to name the entries. - """ - - for key, value in fields.items(): - - # Serialise sections. - - if isinstance(value, dict): - for index, element in enumerate(getSectionElements(value)): - element_ref = "%s$%s" % (key, index) - - self.serialiseFields(element, form, - path and ("%s/%s" % (path, element_ref)) or element_ref - ) - - # Serialise fields. - - else: - form[path and ("%s/%s" % (path, key)) or key] = value - - def modifyFields(self, fields): - - "Modify the given 'fields', removing and adding items." - - # First, remove fields. - - for key in fields.keys(): - if key.startswith("_remove="): - self.removeField(key[8:], fields) - - # Then, add fields. - - for key in fields.keys(): - if key.startswith("_add="): - self.addField(key[5:], fields) - - def removeField(self, path, fields): - - """ - Remove the section element indicated by the given 'path' from the - 'fields'. - """ - - section, (name, index) = getSectionForPath(path, fields) - del section[name][index] - - def addField(self, path, fields): - - """ - Add a section element indicated by the given 'path' to the 'fields'. - """ - - section, (name, index) = getSectionForPath(path, fields) - placeholder = {"_new" : ""} - - if section.has_key(name): - indexes = section[name].keys() - max_index = max(map(int, indexes)) - section[name][max_index + 1] = placeholder - else: - max_index = -1 - section[name] = {0 : placeholder} - # Action function. def execute(pagename, request): - MoinFormHandlerAction(pagename).processForm(request) + MoinFormHandlerAction(pagename, request).processForm() # vim: tabstop=4 expandtab shiftwidth=4