1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinFormHandler Action 4 5 @copyright: 2012 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin.action import do_show 10 from MoinMoin import config 11 from MoinForms import * 12 13 Dependencies = ['pages'] 14 15 class MoinFormHandlerAction: 16 17 "A handler action that can be specialised for individual forms." 18 19 def __init__(self, pagename): 20 self.pagename = pagename 21 22 def processForm(self, request): 23 24 """ 25 Interpret the given 'request' details and modify them according to the 26 structure of the interpreted information. 27 """ 28 29 # Get the form fields and obtain the hierarchical field structure. 30 31 form = get_form(request) 32 fields = getFields(form, remove=True) 33 34 # Process modification operations. 35 36 self.modifyFields(fields) 37 self.serialiseFields(fields, form) 38 39 do_show(self.pagename, request) 40 41 def serialiseFields(self, fields, form, path=None): 42 43 """ 44 Serialise the given 'fields' to the given 'form', using the given 'path' 45 to name the entries. 46 """ 47 48 for key, value in fields.items(): 49 50 # Serialise sections. 51 52 if isinstance(value, dict): 53 for index, element in enumerate(getSectionElements(value)): 54 element_ref = "%s$%s" % (key, index) 55 56 self.serialiseFields(element, form, 57 path and ("%s/%s" % (path, element_ref)) or element_ref 58 ) 59 60 # Serialise fields. 61 62 else: 63 form[path and ("%s/%s" % (path, key)) or key] = value 64 65 def modifyFields(self, fields): 66 67 "Modify the given 'fields', removing and adding items." 68 69 # First, remove fields. 70 71 for key in fields.keys(): 72 if key.startswith("_remove="): 73 self.removeField(key[8:], fields) 74 75 # Then, add fields. 76 77 for key in fields.keys(): 78 if key.startswith("_add="): 79 self.addField(key[5:], fields) 80 81 def removeField(self, path, fields): 82 83 """ 84 Remove the section element indicated by the given 'path' from the 85 'fields'. 86 """ 87 88 section, (name, index) = getSectionForPath(path, fields) 89 del section[name][index] 90 91 def addField(self, path, fields): 92 93 """ 94 Add a section element indicated by the given 'path' to the 'fields'. 95 """ 96 97 section, (name, index) = getSectionForPath(path, fields) 98 placeholder = {"_new" : ""} 99 100 if section.has_key(name): 101 indexes = section[name].keys() 102 max_index = max(map(int, indexes)) 103 section[name][max_index + 1] = placeholder 104 else: 105 max_index = -1 106 section[name] = {0 : placeholder} 107 108 # Action function. 109 110 def execute(pagename, request): 111 MoinFormHandlerAction(pagename).processForm(request) 112 113 # vim: tabstop=4 expandtab shiftwidth=4