1.1 --- a/XSLForms/Fields.py Fri Jan 27 00:33:47 2006 +0000
1.2 +++ b/XSLForms/Fields.py Fri Jan 27 00:34:56 2006 +0000
1.3 @@ -158,12 +158,14 @@
1.4
1.5 subnode.setAttributeNS(EMPTY_NAMESPACE, t[2], subvalue)
1.6
1.7 - def complete_selectors(self, selectors, fields, documents):
1.8 + def complete_selectors(self, selectors, fields, documents, create):
1.9
1.10 """
1.11 Fill in the given 'selectors' dictionary using the given
1.12 'fields' so that it contains mappings from selector names to
1.13 - parts of the specified 'documents'.
1.14 + parts of the specified 'documents'. If 'create' is set to a
1.15 + true value, selected elements will be created if not already
1.16 + present; otherwise, ignore such selectors.
1.17 """
1.18
1.19 for field, value in fields:
1.20 @@ -213,11 +215,21 @@
1.21 if index < 0:
1.22 break
1.23
1.24 + # If create is set, create selected elements.
1.25 +
1.26 + if create:
1.27 + try:
1.28 + node = self._enter_element(node, name, index)
1.29 + except FieldsError, exc:
1.30 + raise FieldsError, "In field '%s', name '%s' and index '%s' could not be added, since '%s' was found." % (
1.31 + field, name, index, exc.args[0])
1.32 +
1.33 # Where a node cannot be found, do not create a selector.
1.34
1.35 - node = self._find_element(node, name, index)
1.36 - if node is None:
1.37 - break
1.38 + else:
1.39 + node = self._find_element(node, name, index)
1.40 + if node is None:
1.41 + break
1.42
1.43 if not selectors.has_key(selector_name):
1.44 selectors[selector_name] = []
1.45 @@ -332,15 +344,17 @@
1.46 documents[model_name] = instance_root
1.47 return documents
1.48
1.49 - def get_selectors(self, fields, documents):
1.50 + def get_selectors(self, fields, documents, create=0):
1.51
1.52 """
1.53 Get a dictionary containing a mapping of selector names to
1.54 - selected parts of the given 'documents'.
1.55 + selected parts of the given 'documents'. If 'create' is set
1.56 + to a true value, selected elements will be created if not
1.57 + already present.
1.58 """
1.59
1.60 selectors = {}
1.61 - self.complete_selectors(selectors, fields, documents)
1.62 + self.complete_selectors(selectors, fields, documents, create)
1.63 return selectors
1.64
1.65 def new_instance(self, name):
1.66 @@ -410,14 +424,31 @@
1.67
1.68 return self.documents.get(name)
1.69
1.70 - def get_selectors(self):
1.71 + def get_selectors(self, create=0):
1.72
1.73 """
1.74 Get the form data selectors from the container, returning a dictionary
1.75 - mapping selector names to collections of selected elements.
1.76 + mapping selector names to collections of selected elements. If 'create'
1.77 + is set to a true value (unlike the default), the selected elements will
1.78 + be created in the form data document if not already present.
1.79 """
1.80
1.81 - return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents)
1.82 + return FieldProcessor.get_selectors(self, self.parameters.items(), self.documents, create)
1.83 +
1.84 + def get_selector(self, name, create=0):
1.85 +
1.86 + """
1.87 + Get the form data selectors for the given 'name', returning a collection
1.88 + of selected elements. If 'create' is set to a true value (unlike the
1.89 + default), the selected elements will be created in the form data
1.90 + document if not already present.
1.91 + """
1.92 +
1.93 + parameters = []
1.94 + for parameter_name, value in parameters.items():
1.95 + if parameter_name.startswith(name + Constants.selector_indicator):
1.96 + parameters.append((parameter_name, value))
1.97 + return FieldProcessor.get_selectors(self, parameters, self.documents, create)
1.98
1.99 def new_instance(self, name):
1.100