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'])
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