Beispiel #1
0
 def __init__(self, *args, **kwargs):
     xml = args[0]
     fields = args[1]
     if kwargs.has_key('namespaces'):
         self.namespaces = kwargs.pop('namespaces')
     #super(XMLModel, self).__init__(*args, **kwargs)
     tree = etree.parse(StringIO.StringIO(xml))
     self.fieldnames = []
     for f in fields:
         tag = gettag(tree, f['xpath'], self.namespaces)
         value = gettagvalue(tag, f.get('function',None))
         field = {'label': f['form']['label'],
                  'value': value,}
         setattr(self, f['name'], field)
         self.fieldnames.append(f['name'])
Beispiel #2
0
 def prep_fields(fields, xml, namespaces=None):
     """Takes raw kwargs, fills in initial data from xml file.
     
     kwargs[*]['initial'] is used to populate form fields
     
     @param fields: Dict data structure representing fields.
     @param xml: String representation of an XML document.
     @return: fields, with initial data added.
     """
     thistree = etree.parse(StringIO.StringIO(xml))
     for f in fields:
         tag = gettag(thistree, f['xpath'], namespaces)
         value = gettagvalue(tag)
         # insert into form data
         if value:
             f['form']['initial'] = value
     return fields
Beispiel #3
0
 def process(xml, fields, form, namespaces=None):
     """Writes form.cleaned_data values to XML
     
     Uses XPaths from field_kwargs
     
     @param xml: String representation of an XML document.
     @param fields: Dict data structure representing fields.
     @return: XML document string
     """
     tree = etree.parse(StringIO.StringIO(xml))
     for f in deepcopy(fields):
         cleaned_data = form.cleaned_data[f['name']]
         
         # datetime
         if type(cleaned_data)   == type(datetime(1970,1,1, 1,1,1)):
             cleaned_data = cleaned_data.strftime('%Y-%m-%d %H:%M:%S')
         
         # date
         elif type(cleaned_data) == type(date(1970,1,1)):
             cleaned_data = cleaned_data.strftime('%Y-%m-%d')
         
         tag = gettag(tree, f['xpath'], namespaces)
         tag = settagvalue(tag, f['xpath'], cleaned_data, namespaces)
     return etree.tostring(tree, pretty_print=True)