Exemplo n.º 1
0
 def language_choices(self):
     language_choices = []
     results = []
     if self.domain:
         results = get_db().view('languages/list', startkey=[self.domain], endkey=[self.domain, {}], group='true').all()
     if results:
         for result in results:
             lang_code = result['key'][1]
             label = result['key'][1]
             long_form = langcodes.get_name(lang_code)
             if long_form:
                 label += " (" + langcodes.get_name(lang_code) + ")"
             language_choices.append((lang_code, label))
     else:
         language_choices = langcodes.get_all_langs_for_select()
     return language_choices
Exemplo n.º 2
0
 def language_choices(self):
     language_choices = []
     results = []
     if self.domain:
         results = get_db().view('languages/list', startkey=[self.domain], endkey=[self.domain, {}], group='true').all()
     if results:
         for result in results:
             lang_code = result['key'][1]
             label = result['key'][1]
             long_form = langcodes.get_name(lang_code)
             if long_form:
                 label += " (" + langcodes.get_name(lang_code) + ")"
             language_choices.append((lang_code, label))
     else:
         language_choices = langcodes.get_all_langs_for_select()
     return language_choices
Exemplo n.º 3
0
def _create_custom_app_strings(app, lang):

    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if app.build_version >= '2.8':
            numeric_nav_on = app.profile.get('properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features', {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text,) if not (text and text[0].isdigit()) else text
        return text

    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    yield 'cchq.case', "Case"
    yield 'cchq.referral', "Referral"

    # include language code names
    for lc in app.langs:
        name = langcodes.get_name(lc) or lc
        if name:
            yield lc, name

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            if detail_type.startswith('case'):
                label = trans(module.case_label)
            else:
                label = trans(module.referral_label)
            yield id_strings.detail_title_locale(module, detail_type), label

            for column in detail.get_columns():
                if not is_sort_only_column(column):
                    yield id_strings.detail_column_header_locale(module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(module, detail_type, column, item.key), trans(item.value)
                elif column.format == "graph":
                    for index, item in enumerate(column.graph_configuration.annotations):
                        yield id_strings.graph_annotation(module, detail_type, column, index), trans(item.values)
                    for property, values in column.graph_configuration.locale_specific_config.iteritems():
                        yield id_strings.graph_configuration(module, detail_type, column, property), trans(values)

            for tab in detail.get_tabs():
                yield id_strings.detail_tab_title_locale(module, detail_type, tab), trans(tab.header)

        yield id_strings.module_locale(module), maybe_add_index(trans(module.name))
        if hasattr(module, 'case_list'):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(module.case_list.label) or "Case List"
        if hasattr(module, 'referral_list'):
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)
Exemplo n.º 4
0
def _create_custom_app_strings(app, lang):
    from corehq.apps.app_manager.models import Module

    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if LooseVersion(app.build_spec.version) >= '2.8':
            numeric_nav_on = app.profile.get('properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features', {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text,) if not (text and text[0].isdigit()) else text
        return text

    id_strings = IdStrings()
    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    yield 'cchq.case', "Case"
    yield 'cchq.referral', "Referral"

    # include language code names
    for lc in app.langs:
        name = langcodes.get_name(lc) or lc
        if name:
            yield lc, name

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            if detail_type.startswith('case'):
                label = trans(module.case_label)
            else:
                label = trans(module.referral_label)
            yield id_strings.detail_title_locale(module, detail_type), label

            detail_column_infos = get_detail_column_infos(detail, include_sort=detail_type == 'case_short')
            for (column, sort_element, order) in detail_column_infos:
                if not is_sort_only_column(column):
                    yield id_strings.detail_column_header_locale(module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(module, detail_type, column, item.key), trans(item.value)

        yield id_strings.module_locale(module), maybe_add_index(trans(module.name))
        if isinstance(module, Module):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(module.case_list.label) or "Case List"
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)
Exemplo n.º 5
0
def get_domain_languages(domain):
    app_languages = get_app_languages(domain)
    translations = SMSTranslations.objects.filter(domain=domain).first()
    sms_languages = translations.langs if translations else []

    domain_languages = []
    for lang_code in app_languages.union(sms_languages):
        name = langcodes.get_name(lang_code)
        label = "{} ({})".format(lang_code, name) if name else lang_code
        domain_languages.append((lang_code, label))

    return sorted(domain_languages) or langcodes.get_all_langs_for_select()
Exemplo n.º 6
0
def _create_custom_app_strings(app, lang):

    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if app.build_version >= '2.8':
            numeric_nav_on = app.profile.get('properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features', {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text,) if not (text and text[0].isdigit()) else text
        return text

    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    yield 'cchq.case', "Case"
    yield 'cchq.referral', "Referral"

    # include language code names
    for lc in app.langs:
        name = langcodes.get_name(lc) or lc
        if name:
            yield lc, name

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            if detail_type.startswith('case'):
                label = trans(module.case_label)
            else:
                label = trans(module.referral_label)
            yield id_strings.detail_title_locale(module, detail_type), label

            for column in detail.get_columns():
                if not is_sort_only_column(column):
                    yield id_strings.detail_column_header_locale(module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(module, detail_type, column, item.key), trans(item.value)

        yield id_strings.module_locale(module), maybe_add_index(trans(module.name))
        if hasattr(module, 'case_list'):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(module.case_list.label) or "Case List"
        if hasattr(module, 'referral_list'):
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)
Exemplo n.º 7
0
def get_domain_languages(domain):
    query = AppES().domain(domain).terms_aggregation("langs", "languages").size(0)
    app_languages = query.run().aggregations.languages.keys

    translation_doc = StandaloneTranslationDoc.get_obj(domain, "sms")
    sms_languages = translation_doc.langs if translation_doc else []

    domain_languages = []
    for lang_code in set(app_languages + sms_languages):
        name = langcodes.get_name(lang_code)
        label = u"{} ({})".format(lang_code, name) if name else lang_code
        domain_languages.append((lang_code, label))

    return sorted(domain_languages) or langcodes.get_all_langs_for_select()
Exemplo n.º 8
0
def get_domain_languages(domain):
    query = (AppES().domain(domain).terms_aggregation('langs',
                                                      'languages').size(0))
    app_languages = query.run().aggregations.languages.keys

    translation_doc = StandaloneTranslationDoc.get_obj(domain, 'sms')
    sms_languages = translation_doc.langs if translation_doc else []

    domain_languages = []
    for lang_code in set(app_languages + sms_languages):
        name = langcodes.get_name(lang_code)
        label = u"{} ({})".format(lang_code, name) if name else lang_code
        domain_languages.append((lang_code, label))

    return sorted(domain_languages) or langcodes.get_all_langs_for_select()
Exemplo n.º 9
0
def get_domain_languages(domain):
    query = (AppES()
             .domain(domain)
             .terms_facet('langs', 'languages')
             .size(0))
    app_languages = query.run().facets.languages.terms

    translation_doc = StandaloneTranslationDoc.get_obj(domain, 'sms')
    sms_languages = translation_doc.langs if translation_doc else []

    domain_languages = []
    for lang_code in set(app_languages + sms_languages):
        name = langcodes.get_name(lang_code)
        label = u"{} ({})".format(lang_code, name) if name else lang_code
        domain_languages.append((lang_code, label))

    return sorted(domain_languages) or langcodes.get_all_langs_for_select()
Exemplo n.º 10
0
def get_domain_languages(domain):
    app_languages = [
        res['key'][1] for res in Application.get_db().view('languages/list',
                                                           startkey=[domain],
                                                           endkey=[domain, {}],
                                                           group='true').all()
    ]

    translation_doc = StandaloneTranslationDoc.get_obj(domain, 'sms')
    sms_languages = translation_doc.langs if translation_doc else []

    domain_languages = []
    for lang_code in set(app_languages + sms_languages):
        name = langcodes.get_name(lang_code)
        label = u"{} ({})".format(lang_code, name) if name else lang_code
        domain_languages.append((lang_code, label))

    return sorted(domain_languages) or langcodes.get_all_langs_for_select()
Exemplo n.º 11
0
def get_domain_languages(domain):
    app_languages = [
        res["key"][1]
        for res in Application.get_db()
        .view("languages/list", startkey=[domain], endkey=[domain, {}], group="true")
        .all()
    ]

    translation_doc = StandaloneTranslationDoc.get_obj(domain, "sms")
    sms_languages = translation_doc.langs if translation_doc else []

    domain_languages = []
    for lang_code in set(app_languages + sms_languages):
        name = langcodes.get_name(lang_code)
        label = u"{} ({})".format(lang_code, name) if name else lang_code
        domain_languages.append((lang_code, label))

    return sorted(domain_languages) or langcodes.get_all_langs_for_select()
Exemplo n.º 12
0
def _create_custom_app_strings(app, lang, for_default=False):

    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if app.build_version >= '2.8':
            numeric_nav_on = app.profile.get('properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features', {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text,) if not (text and text[0].isdigit()) else text
        return text

    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    yield 'cchq.case', "Case"
    yield 'cchq.referral', "Referral"

    # include language code names and current language
    for lc in app.langs:
        name = langcodes.get_name(lc) or lc
        if name:
            yield lc, name
    yield id_strings.current_language(), lang

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            if detail_type.startswith('case'):
                label = trans(module.case_label)
            elif detail_type.startswith('referral'):
                label = trans(module.referral_label)
            elif detail_type in ('product_short', 'product_long'):
                label = ''
            else:
                label = None
            if label is not None:
                yield id_strings.detail_title_locale(module, detail_type), label

            for column in detail.get_columns():
                if not is_sort_only_column(column):
                    yield id_strings.detail_column_header_locale(module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(
                            module, detail_type, column, item.key_as_variable
                        ), trans(item.value)
                elif column.format == "graph":
                    for index, item in enumerate(column.graph_configuration.annotations):
                        yield id_strings.graph_annotation(module, detail_type, column, index), trans(item.values)
                    for property, values in column.graph_configuration.locale_specific_config.iteritems():
                        yield id_strings.graph_configuration(module, detail_type, column, property), trans(values)
                    for index, item in enumerate(column.graph_configuration.series):
                        for property, values in item.locale_specific_config.iteritems():
                            yield id_strings.graph_series_configuration(
                                module, detail_type, column, index, property
                            ), trans(values)

            for tab in detail.get_tabs():
                yield id_strings.detail_tab_title_locale(module, detail_type, tab), trans(tab.header)

        yield id_strings.module_locale(module), maybe_add_index(trans(module.name))

        icon = module.icon_app_string(lang, for_default=for_default)
        audio = module.audio_app_string(lang, for_default=for_default)
        if icon:
            yield id_strings.module_icon_locale(module), icon
        if audio:
            yield id_strings.module_audio_locale(module), audio

        if hasattr(module, 'report_configs'):
            yield id_strings.report_menu(), 'Reports'
            yield id_strings.report_name_header(), 'Report Name'
            yield id_strings.report_description_header(), 'Report Description'
            yield id_strings.report_last_sync(), 'Last Sync'
            yield id_strings.report_data_table(), 'Data Table'
            for config in module.report_configs:
                yield id_strings.report_command(config.uuid), trans(config.header)
                yield id_strings.report_name(config.uuid), trans(config.header)
                yield id_strings.report_description(config.uuid), trans(config.localized_description)
                for column in config.report.report_columns:
                    yield (
                        id_strings.report_column_header(config.uuid, column.column_id),
                        column.get_header(lang)
                    )

        if hasattr(module, 'case_list'):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(module.case_list.label) or "Case List"
                icon = module.case_list.icon_app_string(lang, for_default=for_default)
                audio = module.case_list.audio_app_string(lang, for_default=for_default)
                if icon:
                    yield id_strings.case_list_icon_locale(module), icon
                if audio:
                    yield id_strings.case_list_audio_locale(module), audio

        if hasattr(module, 'referral_list'):
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)

            icon = form.icon_app_string(lang, for_default=for_default)
            audio = form.audio_app_string(lang, for_default=for_default)
            if icon:
                yield id_strings.form_icon_locale(form), icon
            if audio:
                yield id_strings.form_audio_locale(form), audio

        if hasattr(module, 'case_list_form') and module.case_list_form.form_id:
            yield (
                id_strings.case_list_form_locale(module),
                trans(module.case_list_form.label) or "Create a new Case"
            )
            icon = module.case_list_form.icon_app_string(lang, for_default=for_default)
            audio = module.case_list_form.audio_app_string(lang, for_default=for_default)
            if icon:
                yield id_strings.case_list_form_icon_locale(module), icon
            if audio:
                yield id_strings.case_list_form_audio_locale(module), audio
Exemplo n.º 13
0
def _create_custom_app_strings(app, lang, for_default=False):
    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if app.build_version and app.build_version >= LooseVersion('2.8'):
            numeric_nav_on = app.profile.get(
                'properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features',
                               {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text, ) if not (
                    text and text[0].isdigit()) else text
        return text

    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    for id, value in id_strings.REGEX_DEFAULT_VALUES.items():
        yield id, value

    if for_default:
        # include language code names and current language
        for lc in app.langs:
            name = langcodes.get_name(lc) or lc
            if not name:
                continue
            with localize(convert_to_two_letter_code(lc)):
                name = ugettext(name)
            yield lc, name

    yield id_strings.current_language(), lang

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            for column in detail.get_columns():
                yield id_strings.detail_column_header_locale(
                    module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image', 'conditional-enum'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(
                            module, detail_type, column,
                            item.key_as_variable), trans(item.value)
                elif column.format == "graph":
                    for index, item in enumerate(
                            column.graph_configuration.annotations):
                        yield id_strings.graph_annotation(
                            module, detail_type, column,
                            index), trans(item.values)
                    for property, values in six.iteritems(
                            column.graph_configuration.locale_specific_config):
                        yield id_strings.graph_configuration(
                            module, detail_type, column,
                            property), trans(values)
                    for index, item in enumerate(
                            column.graph_configuration.series):
                        for property, values in six.iteritems(
                                item.locale_specific_config):
                            yield id_strings.graph_series_configuration(
                                module, detail_type, column, index,
                                property), trans(values)

            # To list app strings for properties used as sorting properties only
            if detail.sort_elements:
                sort_only, sort_columns = get_sort_and_sort_only_columns(
                    detail, detail.sort_elements)
                for field, sort_element, order in sort_only:
                    if sort_element.has_display_values():
                        column = create_temp_sort_column(sort_element, order)
                        yield id_strings.detail_column_header_locale(module, detail_type, column), \
                            trans(column.header)

            for tab in detail.get_tabs():
                yield id_strings.detail_tab_title_locale(
                    module, detail_type, tab), trans(tab.header)

            if getattr(detail, 'lookup_display_results'):
                yield id_strings.callout_header_locale(module), trans(
                    detail.lookup_field_header)

        yield id_strings.module_locale(module), maybe_add_index(
            trans(module.name))

        icon = module.icon_app_string(lang, for_default=for_default)
        audio = module.audio_app_string(lang, for_default=for_default)
        custom_icon_form, custom_icon_text = module.custom_icon_form_and_text_by_language(
            lang)
        if icon:
            yield id_strings.module_icon_locale(module), icon
        if audio:
            yield id_strings.module_audio_locale(module), audio
        if custom_icon_form and custom_icon_text:
            yield _get_custom_icon_app_locale_and_value(custom_icon_form,
                                                        custom_icon_text,
                                                        module=module)

        if hasattr(module, 'report_configs'):
            for config in module.report_configs:
                yield id_strings.report_command(config.uuid), trans(
                    config.header)
                yield id_strings.report_name(config.uuid), trans(config.header)
                yield id_strings.report_description(config.uuid), trans(
                    config.localized_description)
                for column in config.report(app.domain).report_columns:
                    yield (id_strings.report_column_header(
                        config.uuid,
                        column.column_id), column.get_header(lang))
                for chart_id, graph_config in six.iteritems(
                        config.complete_graph_configs):
                    for index, item in enumerate(graph_config.annotations):
                        yield id_strings.mobile_ucr_annotation(
                            module, config.uuid, index), trans(item.values)
                    for property, values in six.iteritems(
                            graph_config.locale_specific_config):
                        yield id_strings.mobile_ucr_configuration(
                            module, config.uuid, property), trans(values)
                    for index, item in enumerate(graph_config.series):
                        for property, values in six.iteritems(
                                item.locale_specific_config):
                            yield id_strings.mobile_ucr_series_configuration(
                                module, config.uuid, index,
                                property), trans(values)

        if hasattr(module, 'case_list'):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(
                    module.case_list.label) or "Case List"
                icon = module.case_list.icon_app_string(
                    lang, for_default=for_default)
                audio = module.case_list.audio_app_string(
                    lang, for_default=for_default)
                if icon:
                    yield id_strings.case_list_icon_locale(module), icon
                if audio:
                    yield id_strings.case_list_audio_locale(module), audio

        if module_offers_search(module):
            yield id_strings.case_search_locale(module), trans(
                module.search_config.command_label)
            # icon and audio not yet available
            for prop in module.search_config.properties:
                yield id_strings.search_property_locale(
                    module, prop.name), trans(prop.label)

        if hasattr(module, 'referral_list'):
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(
                    module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)

            icon = form.icon_app_string(lang, for_default=for_default)
            audio = form.audio_app_string(lang, for_default=for_default)
            custom_icon_form, custom_icon_text = form.custom_icon_form_and_text_by_language(
                lang)
            if icon:
                yield id_strings.form_icon_locale(form), icon
            if audio:
                yield id_strings.form_audio_locale(form), audio
            if custom_icon_form and custom_icon_text:
                yield _get_custom_icon_app_locale_and_value(custom_icon_form,
                                                            custom_icon_text,
                                                            form=form)

        if hasattr(module, 'case_list_form') and module.case_list_form.form_id:
            yield (id_strings.case_list_form_locale(module),
                   trans(module.case_list_form.label) or "Create a new Case")
            icon = module.case_list_form.icon_app_string(
                lang, for_default=for_default)
            audio = module.case_list_form.audio_app_string(
                lang, for_default=for_default)
            if icon:
                yield id_strings.case_list_form_icon_locale(module), icon
            if audio:
                yield id_strings.case_list_form_audio_locale(module), audio
Exemplo n.º 14
0
def _handle_user_form(request, domain, couch_user=None):
    context = {}
    if couch_user:
        create_user = False
    else:
        create_user = True
    can_change_admin_status = ((request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain))
        and request.couch_user.user_id != couch_user.user_id)

    if couch_user.is_commcare_user():
        role_choices = UserRole.commcareuser_role_choices(domain)
    else:
        role_choices = UserRole.role_choices(domain)

    results = get_db().view('languages/list', startkey=[domain], endkey=[domain, {}], group='true').all()
    language_choices = []

    if results:
        for result in results:
            lang_code = result['key'][1]
            label = result['key'][1]
            long_form = langcodes.get_name(lang_code)
            if long_form:
                label += " (" + langcodes.get_name(lang_code) + ")"
            language_choices.append((lang_code, label))
    else:
        language_choices = langcodes.get_all_langs_for_select()

    if request.method == "POST" and request.POST['form_type'] == "basic-info":
        if couch_user.is_commcare_user():
            form = UserForm(request.POST, role_choices=role_choices, language_choices=language_choices)
        else:
            form = WebUserForm(request.POST, role_choices=role_choices, language_choices=language_choices)
        if form.is_valid():
            if create_user:
                django_user = User()
                django_user.username = form.cleaned_data['email']
                django_user.save()
                couch_user = CouchUser.from_django_user(django_user)
            if couch_user.is_current_web_user(request) or couch_user.is_commcare_user():
                couch_user.first_name = form.cleaned_data['first_name']
                couch_user.last_name = form.cleaned_data['last_name']
                couch_user.email = form.cleaned_data['email']
                if not couch_user.is_commcare_user():
                    couch_user.email_opt_out = form.cleaned_data['email_opt_out']
                couch_user.language = form.cleaned_data['language']
            if can_change_admin_status:
                role = form.cleaned_data['role']
                if role:
                    couch_user.set_role(domain, role)
            couch_user.save()
            if request.couch_user.get_id == couch_user.get_id and couch_user.language:
                # update local language in the session
                request.session['django_language'] = couch_user.language

            messages.success(request, 'Changes saved for user "%s"' % couch_user.username)
    else:
        form = UserForm(role_choices=role_choices, language_choices=language_choices)
        if couch_user.is_commcare_user():
            form = UserForm(role_choices=role_choices, language_choices=language_choices)
        else:
            form = WebUserForm(role_choices=role_choices, language_choices=language_choices)

        if not create_user:
            form.initial['first_name'] = couch_user.first_name
            form.initial['last_name'] = couch_user.last_name
            form.initial['email'] = couch_user.email
            form.initial['email_opt_out'] = couch_user.email_opt_out
            form.initial['language'] = couch_user.language
            if can_change_admin_status:
                if couch_user.is_commcare_user():
                    role = couch_user.get_role(domain)
                    if role is None:
                        initial = "none"
                    else:
                        initial = role.get_qualified_id()
                    form.initial['role'] = initial
                else:
                    form.initial['role'] = couch_user.get_role(domain, include_teams=False).get_qualified_id() or ''

    if not can_change_admin_status:
        del form.fields['role']

    context.update({"form": form,
                    "current_users_page": couch_user.is_current_web_user(request)})
    return context
Exemplo n.º 15
0
def _create_custom_app_strings(app, lang):

    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if app.build_version >= '2.8':
            numeric_nav_on = app.profile.get('properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features', {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text,) if not (text and text[0].isdigit()) else text
        return text

    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    yield 'cchq.case', "Case"
    yield 'cchq.referral', "Referral"

    # include language code names
    for lc in app.langs:
        name = langcodes.get_name(lc) or lc
        if name:
            yield lc, name

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            if detail_type.startswith('case'):
                label = trans(module.case_label)
            elif detail_type.startswith('referral'):
                label = trans(module.referral_label)
            elif detail_type in ('product_short', 'product_long'):
                label = ''
            else:
                label = None
            if label is not None:
                yield id_strings.detail_title_locale(module, detail_type), label

            for column in detail.get_columns():
                if not is_sort_only_column(column):
                    yield id_strings.detail_column_header_locale(module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(module, detail_type, column, item.key_as_variable), trans(item.value)
                elif column.format == "graph":
                    for index, item in enumerate(column.graph_configuration.annotations):
                        yield id_strings.graph_annotation(module, detail_type, column, index), trans(item.values)
                    for property, values in column.graph_configuration.locale_specific_config.iteritems():
                        yield id_strings.graph_configuration(module, detail_type, column, property), trans(values)

            for tab in detail.get_tabs():
                yield id_strings.detail_tab_title_locale(module, detail_type, tab), trans(tab.header)

        yield id_strings.module_locale(module), maybe_add_index(trans(module.name))
        if hasattr(module, 'report_configs'):
            for config in module.report_configs:
                yield id_strings.report_command(config.report_id), trans(config.header)
                yield id_strings.report_name(config.report_id), config.report.title
                yield id_strings.report_menu(), 'Reports'
                yield id_strings.report_name_header(), 'Report Name'
                yield id_strings.report_description_header(), 'Report Description'
                for column in config.report.report_columns:
                    yield (
                        id_strings.report_column_header(config.report_id, column.column_id),
                        column.get_header(lang)
                    )

        if hasattr(module, 'case_list'):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(module.case_list.label) or "Case List"
        if hasattr(module, 'referral_list'):
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)

        if hasattr(module, 'case_list_form') and module.case_list_form.form_id:
            yield (
                id_strings.case_list_form_locale(module),
                trans(module.case_list_form.label) or "Create a new Case"
            )
Exemplo n.º 16
0
def _handle_user_form(request, domain, couch_user=None):
    context = {}
    if couch_user:
        create_user = False
    else:
        create_user = True
    can_change_admin_status = ((request.user.is_superuser or request.couch_user.can_edit_web_users(domain=domain))
        and request.couch_user.user_id != couch_user.user_id)

    if couch_user.is_commcare_user():
        role_choices = UserRole.commcareuser_role_choices(domain)
    else:
        role_choices = UserRole.role_choices(domain)

    results = get_db().view('languages/list', startkey=[domain], endkey=[domain, {}], group='true').all()
    language_choices = []

    if results:
        for result in results:
            lang_code = result['key'][1]
            label = result['key'][1]
            long_form = langcodes.get_name(lang_code)
            if long_form:
                label += " (" + langcodes.get_name(lang_code) + ")"
            language_choices.append((lang_code, label))
    else:
        language_choices = langcodes.get_all_langs_for_select()

    if request.method == "POST" and request.POST['form_type'] == "basic-info":
        if couch_user.is_commcare_user():
            form = UserForm(request.POST, role_choices=role_choices, language_choices=language_choices)
        else:
            form = WebUserForm(request.POST, role_choices=role_choices, language_choices=language_choices)
        if form.is_valid():
            if create_user:
                django_user = User()
                django_user.username = form.cleaned_data['email']
                django_user.save()
                couch_user = CouchUser.from_django_user(django_user)
            if couch_user.is_current_web_user(request) or couch_user.is_commcare_user():
                couch_user.first_name = form.cleaned_data['first_name']
                couch_user.last_name = form.cleaned_data['last_name']
                couch_user.email = form.cleaned_data['email']
                if not couch_user.is_commcare_user():
                    couch_user.email_opt_in = form.cleaned_data['email_opt_in']
                couch_user.language = form.cleaned_data['language']
            if can_change_admin_status:
                role = form.cleaned_data['role']
                if role:
                    couch_user.set_role(domain, role)
            couch_user.save()
            if request.couch_user.get_id == couch_user.get_id and couch_user.language:
                # update local language in the session
                request.session['django_language'] = couch_user.language

            messages.success(request, 'Changes saved for user "%s"' % couch_user.username)
    else:
        form = UserForm(role_choices=role_choices, language_choices=language_choices)
        if couch_user.is_commcare_user():
            form = UserForm(role_choices=role_choices, language_choices=language_choices)
        else:
            form = WebUserForm(role_choices=role_choices, language_choices=language_choices)

        if not create_user:
            form.initial['first_name'] = couch_user.first_name
            form.initial['last_name'] = couch_user.last_name
            form.initial['email'] = couch_user.email
            form.initial['email_opt_in'] = couch_user.email_opt_in
            form.initial['language'] = couch_user.language
            if can_change_admin_status:
                if couch_user.is_commcare_user():
                    role = couch_user.get_role(domain)
                    if role is None:
                        initial = "none"
                    else:
                        initial = role.get_qualified_id()
                    form.initial['role'] = initial
                else:
                    form.initial['role'] = couch_user.get_role(domain, include_teams=False).get_qualified_id() or ''

    if not can_change_admin_status:
        del form.fields['role']

    context.update({"form": form,
                    "current_users_page": couch_user.is_current_web_user(request)})
    return context
Exemplo n.º 17
0
def _create_custom_app_strings(app, lang, for_default=False):

    def trans(d):
        return clean_trans(d, langs)

    def maybe_add_index(text):
        if app.build_version and app.build_version >= LooseVersion('2.8'):
            numeric_nav_on = app.profile.get('properties', {}).get('cc-entry-mode') == 'cc-entry-review'
            if app.profile.get('features', {}).get('sense') == 'true' or numeric_nav_on:
                text = "${0} %s" % (text,) if not (text and text[0].isdigit()) else text
        return text

    langs = [lang] + app.langs
    yield id_strings.homescreen_title(), app.name
    yield id_strings.app_display_name(), app.name

    for id, value in id_strings.REGEX_DEFAULT_VALUES.items():
        yield id, value

    if for_default:
        # include language code names and current language
        for lc in app.langs:
            name = langcodes.get_name(lc) or lc
            if not name:
                continue
            with localize(convert_to_two_letter_code(lc)):
                name = ugettext(name)
            yield lc, name

    yield id_strings.current_language(), lang

    for module in app.get_modules():
        for detail_type, detail, _ in module.get_details():
            for column in detail.get_columns():
                yield id_strings.detail_column_header_locale(module, detail_type, column), trans(column.header)

                if column.format in ('enum', 'enum-image', 'conditional-enum'):
                    for item in column.enum:
                        yield id_strings.detail_column_enum_variable(
                            module, detail_type, column, item.key_as_variable
                        ), trans(item.value)
                elif column.format == "graph":
                    for index, item in enumerate(column.graph_configuration.annotations):
                        yield id_strings.graph_annotation(module, detail_type, column, index), trans(item.values)
                    for property, values in six.iteritems(column.graph_configuration.locale_specific_config):
                        yield id_strings.graph_configuration(module, detail_type, column, property), trans(values)
                    for index, item in enumerate(column.graph_configuration.series):
                        for property, values in six.iteritems(item.locale_specific_config):
                            yield id_strings.graph_series_configuration(
                                module, detail_type, column, index, property
                            ), trans(values)

            # To list app strings for properties used as sorting properties only
            if detail.sort_elements:
                sort_only, sort_columns = get_sort_and_sort_only_columns(detail, detail.sort_elements)
                for field, sort_element, order in sort_only:
                    if sort_element.has_display_values():
                        column = create_temp_sort_column(sort_element, order)
                        yield id_strings.detail_column_header_locale(module, detail_type, column), \
                            trans(column.header)

            for tab in detail.get_tabs():
                yield id_strings.detail_tab_title_locale(module, detail_type, tab), trans(tab.header)

            if getattr(detail, 'lookup_display_results'):
                yield id_strings.callout_header_locale(module), trans(detail.lookup_field_header)

        yield id_strings.module_locale(module), maybe_add_index(trans(module.name))

        icon = module.icon_app_string(lang, for_default=for_default)
        audio = module.audio_app_string(lang, for_default=for_default)
        custom_icon_form, custom_icon_text = module.custom_icon_form_and_text_by_language(lang)
        if icon:
            yield id_strings.module_icon_locale(module), icon
        if audio:
            yield id_strings.module_audio_locale(module), audio
        if custom_icon_form and custom_icon_text:
            yield _get_custom_icon_app_locale_and_value(custom_icon_form, custom_icon_text, module=module)

        if hasattr(module, 'report_configs'):
            for config in module.report_configs:
                yield id_strings.report_command(config.uuid), trans(config.header)
                yield id_strings.report_name(config.uuid), trans(config.header)
                yield id_strings.report_description(config.uuid), trans(config.localized_description)
                for column in config.report(app.domain).report_columns:
                    yield (
                        id_strings.report_column_header(config.uuid, column.column_id),
                        column.get_header(lang)
                    )
                for chart_id, graph_config in six.iteritems(config.complete_graph_configs):
                    for index, item in enumerate(graph_config.annotations):
                        yield id_strings.mobile_ucr_annotation(module, config.uuid, index), trans(item.values)
                    for property, values in six.iteritems(graph_config.locale_specific_config):
                        yield id_strings.mobile_ucr_configuration(module, config.uuid, property), trans(values)
                    for index, item in enumerate(graph_config.series):
                        for property, values in six.iteritems(item.locale_specific_config):
                            yield id_strings.mobile_ucr_series_configuration(
                                module, config.uuid, index, property
                            ), trans(values)

        if hasattr(module, 'case_list'):
            if module.case_list.show:
                yield id_strings.case_list_locale(module), trans(module.case_list.label) or "Case List"
                icon = module.case_list.icon_app_string(lang, for_default=for_default)
                audio = module.case_list.audio_app_string(lang, for_default=for_default)
                if icon:
                    yield id_strings.case_list_icon_locale(module), icon
                if audio:
                    yield id_strings.case_list_audio_locale(module), audio

        if module_offers_search(module):
            yield id_strings.case_search_locale(module), trans(module.search_config.command_label)
            # icon and audio not yet available
            for prop in module.search_config.properties:
                yield id_strings.search_property_locale(module, prop.name), trans(prop.label)

        if hasattr(module, 'referral_list'):
            if module.referral_list.show:
                yield id_strings.referral_list_locale(module), trans(module.referral_list.label)
        for form in module.get_forms():
            form_name = trans(form.name) + ('${0}' if form.show_count else '')
            yield id_strings.form_locale(form), maybe_add_index(form_name)

            icon = form.icon_app_string(lang, for_default=for_default)
            audio = form.audio_app_string(lang, for_default=for_default)
            custom_icon_form, custom_icon_text = form.custom_icon_form_and_text_by_language(lang)
            if icon:
                yield id_strings.form_icon_locale(form), icon
            if audio:
                yield id_strings.form_audio_locale(form), audio
            if custom_icon_form and custom_icon_text:
                yield _get_custom_icon_app_locale_and_value(custom_icon_form, custom_icon_text, form=form)

            for id, custom_assertion in enumerate(form.custom_assertions):
                yield id_strings.custom_assertion_locale(module, form, id), trans(custom_assertion.text)

        if hasattr(module, 'case_list_form') and module.case_list_form.form_id:
            yield (
                id_strings.case_list_form_locale(module),
                trans(module.case_list_form.label) or "Create a new Case"
            )
            icon = module.case_list_form.icon_app_string(lang, for_default=for_default)
            audio = module.case_list_form.audio_app_string(lang, for_default=for_default)
            if icon:
                yield id_strings.case_list_form_icon_locale(module), icon
            if audio:
                yield id_strings.case_list_form_audio_locale(module), audio