1.1 --- a/actions/MoinFormHandler.py Sat Dec 01 23:26:28 2012 +0100
1.2 +++ b/actions/MoinFormHandler.py Sat Dec 01 23:54:16 2012 +0100
1.3 @@ -6,108 +6,13 @@
1.4 @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.5 """
1.6
1.7 -from MoinMoin.action import do_show
1.8 -from MoinMoin import config
1.9 -from MoinForms import *
1.10 +from MoinForms import MoinFormHandlerAction
1.11
1.12 Dependencies = ['pages']
1.13
1.14 -class MoinFormHandlerAction:
1.15 -
1.16 - "A handler action that can be specialised for individual forms."
1.17 -
1.18 - def __init__(self, pagename):
1.19 - self.pagename = pagename
1.20 -
1.21 - def processForm(self, request):
1.22 -
1.23 - """
1.24 - Interpret the given 'request' details and modify them according to the
1.25 - structure of the interpreted information.
1.26 - """
1.27 -
1.28 - # Get the form fields and obtain the hierarchical field structure.
1.29 -
1.30 - form = get_form(request)
1.31 - fields = getFields(form, remove=True)
1.32 -
1.33 - # Process modification operations.
1.34 -
1.35 - self.modifyFields(fields)
1.36 - self.serialiseFields(fields, form)
1.37 -
1.38 - do_show(self.pagename, request)
1.39 -
1.40 - def serialiseFields(self, fields, form, path=None):
1.41 -
1.42 - """
1.43 - Serialise the given 'fields' to the given 'form', using the given 'path'
1.44 - to name the entries.
1.45 - """
1.46 -
1.47 - for key, value in fields.items():
1.48 -
1.49 - # Serialise sections.
1.50 -
1.51 - if isinstance(value, dict):
1.52 - for index, element in enumerate(getSectionElements(value)):
1.53 - element_ref = "%s$%s" % (key, index)
1.54 -
1.55 - self.serialiseFields(element, form,
1.56 - path and ("%s/%s" % (path, element_ref)) or element_ref
1.57 - )
1.58 -
1.59 - # Serialise fields.
1.60 -
1.61 - else:
1.62 - form[path and ("%s/%s" % (path, key)) or key] = value
1.63 -
1.64 - def modifyFields(self, fields):
1.65 -
1.66 - "Modify the given 'fields', removing and adding items."
1.67 -
1.68 - # First, remove fields.
1.69 -
1.70 - for key in fields.keys():
1.71 - if key.startswith("_remove="):
1.72 - self.removeField(key[8:], fields)
1.73 -
1.74 - # Then, add fields.
1.75 -
1.76 - for key in fields.keys():
1.77 - if key.startswith("_add="):
1.78 - self.addField(key[5:], fields)
1.79 -
1.80 - def removeField(self, path, fields):
1.81 -
1.82 - """
1.83 - Remove the section element indicated by the given 'path' from the
1.84 - 'fields'.
1.85 - """
1.86 -
1.87 - section, (name, index) = getSectionForPath(path, fields)
1.88 - del section[name][index]
1.89 -
1.90 - def addField(self, path, fields):
1.91 -
1.92 - """
1.93 - Add a section element indicated by the given 'path' to the 'fields'.
1.94 - """
1.95 -
1.96 - section, (name, index) = getSectionForPath(path, fields)
1.97 - placeholder = {"_new" : ""}
1.98 -
1.99 - if section.has_key(name):
1.100 - indexes = section[name].keys()
1.101 - max_index = max(map(int, indexes))
1.102 - section[name][max_index + 1] = placeholder
1.103 - else:
1.104 - max_index = -1
1.105 - section[name] = {0 : placeholder}
1.106 -
1.107 # Action function.
1.108
1.109 def execute(pagename, request):
1.110 - MoinFormHandlerAction(pagename).processForm(request)
1.111 + MoinFormHandlerAction(pagename, request).processForm()
1.112
1.113 # vim: tabstop=4 expandtab shiftwidth=4