MoinForms

Change of actions/MoinFormHandler.py

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