def _test(self, escaped_itext, expected):
     itext_value = ItextValue.from_node(
         WrappedNode(
             '<value xmlns="http://www.w3.org/2002/xforms">%s</value>' % (
                 escaped_itext
             )
         )
     )
     self.assertEqual(itext_value, expected)
Beispiel #2
0
def get_form_question_label_name_media(langs, form):
    """
    Returns form question label, name, and media, in given langs
    """
    if form.form_type == 'shadow_form':
        return []

    rows = []

    xform = form.wrapped_xform()
    itext_items = OrderedDict()
    nodes = []
    try:
        for lang in langs:
            nodes += xform.itext_node.findall("./{f}translation[@lang='%s']" %
                                              lang)
    except XFormException:
        pass

    for translation_node in nodes:
        lang = translation_node.attrib['lang']
        for text_node in translation_node.findall("./{f}text"):
            text_id = text_node.attrib['id']
            itext_items[text_id] = itext_items.get(text_id, {})

            for value_node in text_node.findall("./{f}value"):
                value_form = value_node.attrib.get("form", "default")
                value = ''
                for part in ItextValue.from_node(value_node).parts:
                    if isinstance(part, ItextOutput):
                        value += "<output value=\"" + part.ref + "\"/>"
                    else:
                        part = force_text(part)
                        part = part.replace('&', '&amp;')
                        part = part.replace('<', '&lt;')
                        part = part.replace('>', '&gt;')
                        value += mark_safe(part)
                itext_items[text_id][(lang, value_form)] = value

    app = form.get_app()
    for text_id, values in six.iteritems(itext_items):
        row = [text_id]
        for value_form in ["default", "image", "audio", "video"]:
            # Get the fallback value for this form
            fallback = ""
            for lang in app.langs:
                fallback = values.get((lang, value_form), fallback)
                if fallback:
                    break
            # Populate the row
            for lang in langs:
                row.append(values.get((lang, value_form), fallback))
        # Don't add empty rows:
        if any(row[1:]):
            rows.append(row)

    return rows
Beispiel #3
0
def get_form_question_label_name_media(langs, form):
    """
    Returns form question label, name, and media, in given langs
    """
    if form.form_type == 'shadow_form':
        return []

    rows = []

    xform = form.wrapped_xform()
    itext_items = OrderedDict()
    nodes = []
    try:
        for lang in langs:
            nodes += xform.itext_node.findall("./{f}translation[@lang='%s']" % lang)
    except XFormException:
        pass

    for translation_node in nodes:
        lang = translation_node.attrib['lang']
        for text_node in translation_node.findall("./{f}text"):
            text_id = text_node.attrib['id']
            itext_items[text_id] = itext_items.get(text_id, {})

            for value_node in text_node.findall("./{f}value"):
                value_form = value_node.attrib.get("form", "default")
                value = ''
                for part in ItextValue.from_node(value_node).parts:
                    if isinstance(part, ItextOutput):
                        value += "<output value=\"" + part.ref + "\"/>"
                    else:
                        part = force_text(part)
                        part = part.replace('&', '&amp;')
                        part = part.replace('<', '&lt;')
                        part = part.replace('>', '&gt;')
                        value += mark_safe(part)
                itext_items[text_id][(lang, value_form)] = value

    app = form.get_app()
    for text_id, values in six.iteritems(itext_items):
        row = [text_id]
        for value_form in ["default", "image", "audio", "video"]:
            # Get the fallback value for this form
            fallback = ""
            for lang in app.langs:
                fallback = values.get((lang, value_form), fallback)
                if fallback:
                    break
            # Populate the row
            for lang in langs:
                row.append(values.get((lang, value_form), fallback))
        # Don't add empty rows:
        if any(row[1:]):
            rows.append(row)

    return rows
Beispiel #4
0
def expected_bulk_app_sheet_rows(app):
    """
    Data rows for bulk app translation download
    """

    # keys are the names of sheets, values are lists of tuples representing rows
    rows = {MODULES_AND_FORMS_SHEET_NAME: []}

    for mod_index, module in enumerate(app.get_modules()):
        # This is duplicated logic from expected_bulk_app_sheet_headers,
        # which I don't love.
        module_string = "module" + str(mod_index + 1)

        # Add module to the first sheet
        row_data = _make_modules_and_forms_row(
            row_type="Module",
            sheet_name=module_string,
            languages=[module.name.get(lang) for lang in app.langs],
            media_image=[module.icon_by_language(lang) for lang in app.langs],
            media_audio=[module.audio_by_language(lang) for lang in app.langs],
            unique_id=module.unique_id,
        )
        rows[MODULES_AND_FORMS_SHEET_NAME].append(row_data)

        # Populate module sheet
        rows[module_string] = []
        if not isinstance(module, ReportModule):
            if module.case_list_form.form_id:
                # Add row for label of case list registration form
                rows[module_string].append(
                    ('case_list_form_label', 'list') + tuple(
                        module.case_list_form.label.get(lang, '')
                        for lang in app.langs))

            for list_or_detail, detail in [("list", module.case_details.short),
                                           ("detail", module.case_details.long)
                                           ]:
                # Add a row for each tab heading
                for index, tab in enumerate(detail.tabs):
                    rows[module_string].append(
                        ("Tab {}".format(index), list_or_detail) +
                        tuple(tab.header.get(lang, "") for lang in app.langs))

                # Add a row for each detail field
                # Complex fields may get multiple rows
                case_properties = detail.get_columns()
                for detail in case_properties:

                    field_name = detail.field
                    if re.search(r'\benum\b', detail.format
                                 ):  # enum, conditional-enum, enum-image
                        field_name += " (ID Mapping Text)"
                    elif detail.format == "graph":
                        field_name += " (graph)"

                    # Add a row for this case detail
                    rows[module_string].append((field_name, list_or_detail) +
                                               tuple(
                                                   detail.header.get(lang, "")
                                                   for lang in app.langs))

                    # Add a row for any mapping pairs
                    if re.search(r'\benum\b', detail.format):
                        for mapping in detail.enum:
                            rows[module_string].append(
                                (mapping.key + " (ID Mapping Value)",
                                 list_or_detail) + tuple(
                                     mapping.value.get(lang, "")
                                     for lang in app.langs))

                    # Add rows for graph configuration
                    if detail.format == "graph":
                        for key, val in six.iteritems(
                                detail.graph_configuration.
                                locale_specific_config):
                            rows[module_string].append(
                                (key + " (graph config)", list_or_detail) +
                                tuple(val.get(lang, "") for lang in app.langs))
                        for i, series in enumerate(
                                detail.graph_configuration.series):
                            for key, val in six.iteritems(
                                    series.locale_specific_config):
                                rows[module_string].append(
                                    ("{} {} (graph series config)".format(
                                        key, i), list_or_detail) + tuple(
                                            val.get(lang, "")
                                            for lang in app.langs))
                        for i, annotation in enumerate(
                                detail.graph_configuration.annotations):
                            rows[module_string].append(
                                ("graph annotation {}".format(i + 1),
                                 list_or_detail) + tuple(
                                     annotation.display_text.get(lang, "")
                                     for lang in app.langs))

            for form_index, form in enumerate(module.get_forms()):
                form_string = module_string + "_form" + str(form_index + 1)
                xform = form.wrapped_xform()

                # Add row for this form to the first sheet
                # This next line is same logic as above :(
                first_sheet_row = _make_modules_and_forms_row(
                    row_type="Form",
                    sheet_name=form_string,
                    languages=[form.name.get(lang) for lang in app.langs],
                    # leave all
                    media_image=[
                        form.icon_by_language(lang) for lang in app.langs
                    ],
                    media_audio=[
                        form.audio_by_language(lang) for lang in app.langs
                    ],
                    unique_id=form.unique_id)

                # Add form to the first street
                rows[MODULES_AND_FORMS_SHEET_NAME].append(first_sheet_row)

                if form.form_type == 'shadow_form':
                    continue

                # Populate form sheet
                rows[form_string] = []

                itext_items = OrderedDict()
                try:
                    nodes = xform.itext_node.findall("./{f}translation")
                except XFormException:
                    nodes = []

                for translation_node in nodes:
                    lang = translation_node.attrib['lang']
                    for text_node in translation_node.findall("./{f}text"):
                        text_id = text_node.attrib['id']
                        itext_items[text_id] = itext_items.get(text_id, {})

                        for value_node in text_node.findall("./{f}value"):
                            value_form = value_node.attrib.get(
                                "form", "default")
                            value = ''
                            for part in ItextValue.from_node(value_node).parts:
                                if isinstance(part, ItextOutput):
                                    value += "<output value=\"" + part.ref + "\"/>"
                                else:
                                    value += mark_safe(
                                        force_text(part).replace(
                                            '&',
                                            '&amp;').replace('<',
                                                             '&lt;').replace(
                                                                 '>', '&gt;'))
                            itext_items[text_id][(lang, value_form)] = value

                for text_id, values in six.iteritems(itext_items):
                    row = [text_id]
                    for value_form in ["default", "audio", "image", "video"]:
                        # Get the fallback value for this form
                        fallback = ""
                        for lang in app.langs:
                            fallback = values.get((lang, value_form), fallback)
                            if fallback:
                                break
                        # Populate the row
                        for lang in app.langs:
                            row.append(values.get((lang, value_form),
                                                  fallback))
                    # Don't add empty rows:
                    if any(row[1:]):
                        rows[form_string].append(row)
    return rows
Beispiel #5
0
def expected_bulk_app_sheet_rows(app):
    """
    Data rows for bulk app translation download
    """

    # keys are the names of sheets, values are lists of tuples representing rows
    rows = {"Modules_and_forms": []}

    for mod_index, module in enumerate(app.get_modules()):
        # This is duplicated logic from expected_bulk_app_sheet_headers,
        # which I don't love.
        module_string = "module" + str(mod_index + 1)

        # Add module to the first sheet
        row_data = make_modules_and_forms_row(
            row_type="Module",
            sheet_name=module_string,
            languages=[module.name.get(lang) for lang in app.langs],
            case_labels=[module.case_label.get(lang) for lang in app.langs],
            media_image=[module.icon_by_language(lang) for lang in app.langs],
            media_audio=[module.audio_by_language(lang) for lang in app.langs],
            unique_id=module.unique_id,
        )
        rows["Modules_and_forms"].append(row_data)

        # Populate module sheet
        rows[module_string] = []
        if not isinstance(module, ReportModule):
            for list_or_detail, case_properties in [
                ("list", module.case_details.short.get_columns()),
                ("detail", module.case_details.long.get_columns())
            ]:
                for detail in case_properties:

                    field_name = detail.field
                    if detail.format == "enum":
                        field_name += " (ID Mapping Text)"
                    elif detail.format == "graph":
                        field_name += " (graph)"

                    # Add a row for this case detail
                    rows[module_string].append(
                        (field_name, list_or_detail) +
                        tuple(detail.header.get(lang, "") for lang in app.langs)
                    )

                    # Add a row for any mapping pairs
                    if detail.format == "enum":
                        for mapping in detail.enum:
                            rows[module_string].append(
                                (
                                    mapping.key + " (ID Mapping Value)",
                                    list_or_detail
                                ) + tuple(
                                    mapping.value.get(lang, "")
                                    for lang in app.langs
                                )
                            )

                    # Add rows for graph configuration
                    if detail.format == "graph":
                        for key, val in detail.graph_configuration.locale_specific_config.iteritems():
                            rows[module_string].append(
                                (
                                    key + " (graph config)",
                                    list_or_detail
                                ) + tuple(val.get(lang, "") for lang in app.langs)
                            )
                        for i, annotation in enumerate(detail.graph_configuration.annotations):
                            rows[module_string].append(
                                (
                                    "graph annotation {}".format(i + 1),
                                    list_or_detail
                                ) + tuple(
                                    annotation.display_text.get(lang, "")
                                    for lang in app.langs
                                )
                            )

            for form_index, form in enumerate(module.get_forms()):
                form_string = module_string + "_form" + str(form_index + 1)
                xform = form.wrapped_xform()

                # Add row for this form to the first sheet
                # This next line is same logic as above :(
                first_sheet_row = make_modules_and_forms_row(
                    row_type="Form",
                    sheet_name=form_string,
                    languages=[form.name.get(lang) for lang in app.langs],
                    # leave all
                    case_labels=[None] * len(app.langs),
                    media_image=[form.icon_by_language(lang) for lang in app.langs],
                    media_audio=[form.audio_by_language(lang) for lang in app.langs],
                    unique_id=form.unique_id
                )

                # Add form to the first street
                rows["Modules_and_forms"].append(first_sheet_row)

                # Populate form sheet
                rows[form_string] = []

                itext_items = OrderedDict()
                try:
                    nodes = xform.itext_node.findall("./{f}translation")
                except XFormException:
                    nodes = []

                for translation_node in nodes:
                    lang = translation_node.attrib['lang']
                    for text_node in translation_node.findall("./{f}text"):
                        text_id = text_node.attrib['id']
                        itext_items[text_id] = itext_items.get(text_id, {})

                        for value_node in text_node.findall("./{f}value"):
                            value_form = value_node.attrib.get("form", "default")
                            value = ''
                            for part in ItextValue.from_node(value_node).parts:
                                if isinstance(part, ItextOutput):
                                    value += "<output value=\"" + part.ref + "\"/>"
                                else:
                                    value += mark_safe(force_text(part).replace('<', '&lt;').replace('>', '&gt;'))
                            itext_items[text_id][(lang, value_form)] = value

                for text_id, values in itext_items.iteritems():
                    row = [text_id]
                    for value_form in ["default", "audio", "image", "video"]:
                        # Get the fallback value for this form
                        fallback = ""
                        for lang in app.langs:
                            fallback = values.get((lang, value_form), fallback)
                            if fallback:
                                break
                        # Populate the row
                        for lang in app.langs:
                            row.append(values.get((lang, value_form), fallback))
                    # Don't add empty rows:
                    if any(row[1:]):
                        rows[form_string].append(row)
    return rows
Beispiel #6
0
def expected_bulk_app_sheet_rows(app):

    # keys are the names of sheets, values are lists of tuples representing rows
    rows = {"Modules_and_forms": []}

    for mod_index, module in enumerate(app.get_modules()):
        # This is duplicated logic from expected_bulk_app_sheet_headers,
        # which I don't love.
        module_string = "module" + str(mod_index + 1)

        # Add module to the first sheet
        row_data = make_modules_and_forms_row(
            row_type="Module",
            sheet_name=module_string,
            languages=[module.name.get(lang) for lang in app.langs],
            case_labels=[module.case_label.get(lang) for lang in app.langs],
            media_image=module.media_image,
            media_audio=module.media_audio,
            unique_id=module.unique_id,
        )
        rows["Modules_and_forms"].append(row_data)

        # Populate module sheet
        rows[module_string] = []
        if not isinstance(module, ReportModule):
            for list_or_detail, case_properties in [
                ("list", module.case_details.short.get_columns()),
                ("detail", module.case_details.long.get_columns())
            ]:
                for detail in case_properties:

                    field_name = detail.field
                    if detail.format == "enum":
                        field_name += " (ID Mapping Text)"
                    elif detail.format == "graph":
                        field_name += " (graph)"

                    # Add a row for this case detail
                    rows[module_string].append((field_name, list_or_detail) +
                                               tuple(
                                                   detail.header.get(lang, "")
                                                   for lang in app.langs))

                    # Add a row for any mapping pairs
                    if detail.format == "enum":
                        for mapping in detail.enum:
                            rows[module_string].append(
                                (mapping.key + " (ID Mapping Value)",
                                 list_or_detail) + tuple(
                                     mapping.value.get(lang, "")
                                     for lang in app.langs))

                    # Add rows for graph configuration
                    if detail.format == "graph":
                        for key, val in detail.graph_configuration.locale_specific_config.iteritems(
                        ):
                            rows[module_string].append(
                                (key + " (graph config)", list_or_detail) +
                                tuple(val.get(lang, "") for lang in app.langs))
                        for i, annotation in enumerate(
                                detail.graph_configuration.annotations):
                            rows[module_string].append(
                                ("graph annotation {}".format(i + 1),
                                 list_or_detail) + tuple(
                                     annotation.display_text.get(lang, "")
                                     for lang in app.langs))

            for form_index, form in enumerate(module.get_forms()):
                form_string = module_string + "_form" + str(form_index + 1)
                xform = form.wrapped_xform()

                # Add row for this form to the first sheet
                # This next line is same logic as above :(
                first_sheet_row = make_modules_and_forms_row(
                    row_type="Form",
                    sheet_name=form_string,
                    languages=[form.name.get(lang) for lang in app.langs],
                    # leave all
                    case_labels=[None] * len(app.langs),
                    media_image=form.media_image,
                    media_audio=form.media_audio,
                    unique_id=form.unique_id)

                # Add form to the first street
                rows["Modules_and_forms"].append(first_sheet_row)

                # Populate form sheet
                rows[form_string] = []

                itext_items = OrderedDict()
                try:
                    nodes = xform.itext_node.findall("./{f}translation")
                except XFormException:
                    nodes = []

                for translation_node in nodes:
                    lang = translation_node.attrib['lang']
                    for text_node in translation_node.findall("./{f}text"):
                        text_id = text_node.attrib['id']
                        itext_items[text_id] = itext_items.get(text_id, {})

                        for value_node in text_node.findall("./{f}value"):
                            value_form = value_node.attrib.get(
                                "form", "default")
                            value = ''
                            for part in ItextValue.from_node(value_node).parts:
                                if isinstance(part, ItextOutput):
                                    value += "<output value=\"" + part.ref + "\"/>"
                                else:
                                    value += escape(part)
                            itext_items[text_id][(lang, value_form)] = value

                for text_id, values in itext_items.iteritems():
                    row = [text_id]
                    for value_form in ["default", "audio", "image", "video"]:
                        # Get the fallback value for this form
                        fallback = ""
                        for lang in app.langs:
                            fallback = values.get((lang, value_form), fallback)
                            if fallback:
                                break
                        # Populate the row
                        for lang in app.langs:
                            row.append(values.get((lang, value_form),
                                                  fallback))
                    # Don't add empty rows:
                    if any(row[1:]):
                        rows[form_string].append(row)
    return rows
def expected_bulk_app_sheet_rows(app, exclude_module=None, exclude_form=None):
    """
    Data rows for bulk app translation download

    exclude_module and exclude_form are functions that take in one argument
    (form or module) and return True if the module/form should be excluded
    from the returned list
    """

    # keys are the names of sheets, values are lists of tuples representing rows
    rows = OrderedDict({MODULES_AND_FORMS_SHEET_NAME: []})

    for mod_index, module in enumerate(app.get_modules()):
        if exclude_module is not None and exclude_module(module):
            continue

        # This is duplicated logic from expected_bulk_app_sheet_headers,
        # which I don't love.
        module_string = "module" + str(mod_index + 1)

        # Add module to the first sheet
        row_data = _make_modules_and_forms_row(
            row_type="Module",
            sheet_name=module_string,
            languages=[module.name.get(lang) for lang in app.langs],
            media_image=[module.icon_by_language(lang) for lang in app.langs],
            media_audio=[module.audio_by_language(lang) for lang in app.langs],
            unique_id=module.unique_id,
        )
        rows[MODULES_AND_FORMS_SHEET_NAME].append(row_data)

        # Populate module sheet
        rows[module_string] = []
        if not isinstance(module, ReportModule):
            if module.case_list_form.form_id:
                # Add row for label of case list registration form
                rows[module_string].append(
                        ('case_list_form_label', 'list') +
                        tuple(module.case_list_form.label.get(lang, '') for lang in app.langs)
                )

            for list_or_detail, detail in [
                ("list", module.case_details.short),
                ("detail", module.case_details.long)
            ]:
                # Add a row for each tab heading
                for index, tab in enumerate(detail.tabs):
                    rows[module_string].append(
                        ("Tab {}".format(index), list_or_detail) +
                        tuple(tab.header.get(lang, "") for lang in app.langs)
                    )

                # Add a row for each detail field
                # Complex fields may get multiple rows
                case_properties = detail.get_columns()
                for detail in case_properties:

                    field_name = detail.field
                    if re.search(r'\benum\b', detail.format):   # enum, conditional-enum, enum-image
                        field_name += " (ID Mapping Text)"
                    elif detail.format == "graph":
                        field_name += " (graph)"

                    # Add a row for this case detail
                    rows[module_string].append(
                        (field_name, list_or_detail) +
                        tuple(detail.header.get(lang, "") for lang in app.langs)
                    )

                    # Add a row for any mapping pairs
                    if re.search(r'\benum\b', detail.format):
                        for mapping in detail.enum:
                            rows[module_string].append(
                                (
                                    mapping.key + " (ID Mapping Value)",
                                    list_or_detail
                                ) + tuple(
                                    mapping.value.get(lang, "")
                                    for lang in app.langs
                                )
                            )

                    # Add rows for graph configuration
                    if detail.format == "graph":
                        for key, val in six.iteritems(detail.graph_configuration.locale_specific_config):
                            rows[module_string].append(
                                (
                                    key + " (graph config)",
                                    list_or_detail
                                ) + tuple(val.get(lang, "") for lang in app.langs)
                            )
                        for i, series in enumerate(detail.graph_configuration.series):
                            for key, val in six.iteritems(series.locale_specific_config):
                                rows[module_string].append(
                                    (
                                        "{} {} (graph series config)".format(key, i),
                                        list_or_detail
                                    ) + tuple(val.get(lang, "") for lang in app.langs)
                                )
                        for i, annotation in enumerate(detail.graph_configuration.annotations):
                            rows[module_string].append(
                                (
                                    "graph annotation {}".format(i + 1),
                                    list_or_detail
                                ) + tuple(
                                    annotation.display_text.get(lang, "")
                                    for lang in app.langs
                                )
                            )

            for form_index, form in enumerate(module.get_forms()):
                if exclude_form is not None and exclude_form(form):
                    continue
                form_string = module_string + "_form" + str(form_index + 1)
                xform = form.wrapped_xform()

                # Add row for this form to the first sheet
                # This next line is same logic as above :(
                first_sheet_row = _make_modules_and_forms_row(
                    row_type="Form",
                    sheet_name=form_string,
                    languages=[form.name.get(lang) for lang in app.langs],
                    # leave all
                    media_image=[form.icon_by_language(lang) for lang in app.langs],
                    media_audio=[form.audio_by_language(lang) for lang in app.langs],
                    unique_id=form.unique_id
                )

                # Add form to the first street
                rows[MODULES_AND_FORMS_SHEET_NAME].append(first_sheet_row)

                if form.form_type == 'shadow_form':
                    continue

                # Populate form sheet
                rows[form_string] = []

                itext_items = OrderedDict()
                try:
                    nodes = xform.itext_node.findall("./{f}translation")
                except XFormException:
                    nodes = []

                for translation_node in nodes:
                    lang = translation_node.attrib['lang']
                    for text_node in translation_node.findall("./{f}text"):
                        text_id = text_node.attrib['id']
                        itext_items[text_id] = itext_items.get(text_id, {})

                        for value_node in text_node.findall("./{f}value"):
                            value_form = value_node.attrib.get("form", "default")
                            value = ''
                            for part in ItextValue.from_node(value_node).parts:
                                if isinstance(part, ItextOutput):
                                    value += "<output value=\"" + part.ref + "\"/>"
                                else:
                                    value += mark_safe(force_text(part).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'))
                            itext_items[text_id][(lang, value_form)] = value

                for text_id, values in six.iteritems(itext_items):
                    row = [text_id]
                    for value_form in ["default", "audio", "image", "video"]:
                        # Get the fallback value for this form
                        fallback = ""
                        for lang in app.langs:
                            fallback = values.get((lang, value_form), fallback)
                            if fallback:
                                break
                        # Populate the row
                        for lang in app.langs:
                            row.append(values.get((lang, value_form), fallback))
                    # Don't add empty rows:
                    if any(row[1:]):
                        rows[form_string].append(row)
    return rows