Ejemplo n.º 1
0
def _get_all_stock_report_helpers_from_form(xform):
    """
    Given an instance of an AbstractXFormInstance, extract the ledger actions and convert
    them to StockReportHelper objects.
    """
    form_xml = xform.get_xml_element()
    commtrack_node_names = ('{%s}balance' % COMMTRACK_REPORT_XMLNS,
                            '{%s}transfer' % COMMTRACK_REPORT_XMLNS)

    def _extract_ledger_nodes_from_xml(node):
        """
        Goes through a parsed XML document and recursively pulls out any ledger XML blocks.
        """
        for child in node:
            if child.tag in commtrack_node_names:
                yield child
            else:
                for e in _extract_ledger_nodes_from_xml(child):
                    yield e

    for elem in _extract_ledger_nodes_from_xml(form_xml):
        report_type, ledger_json = convert_xml_to_json(elem, last_xmlns=COMMTRACK_REPORT_XMLNS)

        # apply the same datetime & string conversions
        # that would be applied to XFormInstance.form
        adjust_datetimes(ledger_json)
        ledger_json = XFormInstance({'form': ledger_json}).form
        yield _ledger_json_to_stock_report_helper(xform, report_type, ledger_json)
Ejemplo n.º 2
0
def get_all_stock_report_helpers_from_form(xform):
    """
    Given an instance of an AbstractXFormInstance, extract the ledger actions and convert
    them to StockReportHelper objects.
    """
    form_xml = xform.get_xml_element()
    commtrack_node_names = ('{%s}balance' % COMMTRACK_REPORT_XMLNS,
                            '{%s}transfer' % COMMTRACK_REPORT_XMLNS)

    def _extract_ledger_nodes_from_xml(node):
        """
        Goes through a parsed XML document and recursively pulls out any ledger XML blocks.
        """
        for child in node:
            if child.tag in commtrack_node_names:
                yield child
            else:
                for e in _extract_ledger_nodes_from_xml(child):
                    yield e

    for elem in _extract_ledger_nodes_from_xml(form_xml):
        report_type, ledger_json = convert_xml_to_json(
            elem, last_xmlns=COMMTRACK_REPORT_XMLNS)
        if ledger_json.get('@date'):
            try:
                ledger_json['@date'] = adjust_text_to_datetime(
                    ledger_json['@date'])
            except iso8601.ParseError:
                pass
        yield _ledger_json_to_stock_report_helper(xform, report_type,
                                                  ledger_json)
Ejemplo n.º 3
0
def get_all_stock_report_helpers_from_form(xform):
    """
    Given an instance of an AbstractXFormInstance, extract the ledger actions and convert
    them to StockReportHelper objects.
    """
    form_xml = xform.get_xml_element()
    commtrack_node_names = ('{%s}balance' % COMMTRACK_REPORT_XMLNS,
                            '{%s}transfer' % COMMTRACK_REPORT_XMLNS)

    def _extract_ledger_nodes_from_xml(node):
        """
        Goes through a parsed XML document and recursively pulls out any ledger XML blocks.
        """
        for child in node:
            if child.tag in commtrack_node_names:
                yield child
            else:
                for e in _extract_ledger_nodes_from_xml(child):
                    yield e

    for elem in _extract_ledger_nodes_from_xml(form_xml):
        report_type, ledger_json = convert_xml_to_json(
            elem, last_xmlns=COMMTRACK_REPORT_XMLNS)

        # apply the same datetime & string conversions
        # that would be applied to XFormInstance.form
        adjust_datetimes(ledger_json)
        ledger_json = XFormInstance({'form': ledger_json}).form
        yield _ledger_json_to_stock_report_helper(xform, report_type,
                                                  ledger_json)
Ejemplo n.º 4
0
def unpack_commtrack(xform):
    form_xml = xform.get_xml_element()
    commtrack_node_names = ('{%s}balance' % COMMTRACK_REPORT_XMLNS,
                            '{%s}transfer' % COMMTRACK_REPORT_XMLNS)

    def commtrack_nodes(node):
        for child in node:
            if child.tag in commtrack_node_names:
                yield child
            else:
                for e in commtrack_nodes(child):
                    yield e

    for elem in commtrack_nodes(form_xml):
        report_type, ledger_json = convert_xml_to_json(
            elem, last_xmlns=COMMTRACK_REPORT_XMLNS)

        # apply the same datetime & string conversions
        # that would be applied to XFormInstance.form
        adjust_datetimes(ledger_json)
        ledger_json = XFormInstance({'form': ledger_json}).form

        yield ledger_json_to_stock_report_helper(
            xform, report_type, ledger_json)