diff --git a/document.py b/document.py index daa6583..b31da88 100644 --- a/document.py +++ b/document.py @@ -72,15 +72,19 @@ class Incoming(metaclass=PoolMeta): def _readxml_getvalue( self, xmltree, tags, vtype=None, allow_list=False, - text_field=True): + childs=[]): """ read 'text'-part from xml-xpath, convert to 'vtype' Args: tags (list): list of tags to build xpath - vtype (type-class, optional): to convert value. Defaults to None. - allow_list (boolean): get result as list of values - text_field (boolean): return content of 'text'-part of node + vtype (type-class, optional): to convert value of text-part + (if not childs). Defaults to None. + allow_list (boolean, optional): get result as list of values, + Defaults to False. + childs (list): read child-items of selected node + [('ns', 'tag', ),...], + if emtpy: read text-part of selected node, Defaults to [] Returns: various: converted value or None @@ -88,15 +92,29 @@ class Incoming(metaclass=PoolMeta): result = [] xpath = '/' + '/'.join(tags) nodes = xmltree.xpath(xpath, namespaces=xmltree.nsmap) + + # dict to find children of selected node + childs_dict = { + '{%(ns)s}%(tag)s' % {'ns': xmltree.nsmap[x[0]], 'tag': x[1]}: { + 'type': x[2], 'tag': x[1]} + for x in childs} + if nodes: - if not text_field: - result.extend(nodes) - else: - for node1 in nodes: - if node1.text: - result.append( - node1.text if vtype is None - else vtype(node1.text)) + for node1 in nodes: + if not childs_dict: + result.append( + node1.text if vtype is None else vtype(node1.text)) + else: + values = {} + for x in node1.getchildren(): + if x.tag in childs_dict.keys(): + values[childs_dict[x.tag]['tag']] = ( + x.text if childs_dict[x.tag]['type'] is None + else childs_dict[x.tag]['type'](x.text)) + result.append(values) + + if not allow_list: + break if not allow_list: return result[0] return result @@ -164,28 +182,21 @@ class Incoming(metaclass=PoolMeta): note_list = self._readxml_getvalue(xmltree, [ 'rsm:CrossIndustryInvoice', 'rsm:ExchangedDocument', 'ram:IncludedNote'], - allow_list=True, text_field=False) - print('\n## note_list:', (note_list,)) - if note_list: - invoice.description = self._readxml_getvalue( - note_list[0], ['ram:Content'], allow_list=False) - if len(note_list) > 1: - for x in note_list[1:]: - print('- x:', x) - cont_code = self._readxml_getvalue( - x, ['ram:ContentCode'], allow_list=False) - print('- cont_code:', cont_code) - cont_str = self._readxml_getvalue( - x, ['ram:Content'], allow_list=False) + allow_list=True, + childs=[('ram', 'Content', str), ('ram', 'ContentCode', str), + ('ram', 'SubjectCode', str)]) + + if note_list: + invoice.description = note_list[0].get('Content', None) + invoice.comment = '\n'.join([ + '%(code)s%(subj)s%(msg)s' % { + 'code': ('Code=%s, ' % x.get('ContentCode', '')) + if x.get('ContentCode', '') else '', + 'subj': ('Subject=%s, ' % x.get('SubjectCode', '')) + if x.get('SubjectCode', '') else '', + 'msg': x.get('Content', ''), + } for x in note_list[1:]]) - # descr_list = self._readxml_getvalue(xmltree, [ - # 'rsm:CrossIndustryInvoice', - # 'rsm:ExchangedDocument', 'ram:IncludedNote', - # 'ram:Content'], allow_list=True) - # if descr_list: - # invoice.description = descr_list[0] - # if len(descr_list) > 1: - # invoice.comment = '\n'.join(descr_list[1:]) return invoice def _readxml_convertdate(self, date_string): diff --git a/tests/document.py b/tests/document.py index fdc3240..bab94e1 100644 --- a/tests/document.py +++ b/tests/document.py @@ -59,6 +59,11 @@ class DocumentTestCase(object): self.assertEqual(invoice.invoice_date, date(2024, 6, 17)) self.assertEqual(invoice.currency.name, 'usd') self.assertEqual(invoice.company.rec_name, 'm-ds') + self.assertEqual(invoice.description, 'Description of invoice') + self.assertEqual( + invoice.comment, + 'Code=1, Some notes to the customer.\n' + + 'Code=22, Subject=42, Goes to field comment.') invoice.save() print('\n## invoice:', invoice) diff --git a/tests/facturx-extended.xml b/tests/facturx-extended.xml index 156ff35..e1d3b6f 100644 --- a/tests/facturx-extended.xml +++ b/tests/facturx-extended.xml @@ -21,6 +21,7 @@ Goes to field comment. 22 + 42