Exemple #1
0
def _check_for_sheet_error(app, sheet, headers, processed_sheets=Ellipsis):
    expected_sheets = {h[0]: h[1] for h in headers}

    if sheet.worksheet.title in processed_sheets:
        raise BulkAppTranslationsException(_('Sheet "%s" was repeated. Only the first occurrence has been '
                                             'processed.') % sheet.worksheet.title)

    expected_headers = _get_expected_headers(sheet, expected_sheets)
    if expected_headers is None:
        raise BulkAppTranslationsException(_('Skipping sheet "%s", could not recognize title') %
                                           sheet.worksheet.title)

    num_required_headers = 0
    if is_modules_and_forms_sheet(sheet.worksheet.title):
        num_required_headers = 1    # type
    elif is_module_sheet(sheet.worksheet.title):
        num_required_headers = 2    # case property, list or detail
    elif is_form_sheet(sheet.worksheet.title):
        num_required_headers = 1    # label
    elif is_single_sheet(sheet.worksheet.title):
        num_required_headers = 4    # menu or form, case property, list or detail, label

    expected_required_headers = tuple(expected_headers[:num_required_headers])
    actual_required_headers = tuple(sheet.headers[:num_required_headers])
    if expected_required_headers != actual_required_headers:
        raise BulkAppTranslationsException(_('Skipping sheet {title}: expected first columns to be '
                                             '{expected}').format(
                                                 title=sheet.worksheet.title,
                                                 expected=", ".join(expected_required_headers)))
Exemple #2
0
def _get_expected_headers(sheet, expected_sheets):
    if sheet.worksheet.title == LEGACY_MODULES_AND_FORMS_SHEET_NAME:
        expected_headers = expected_sheets.get(MODULES_AND_FORMS_SHEET_NAME, None)
    elif is_module_sheet(sheet.worksheet.title) or is_form_sheet(sheet.worksheet.title):
        expected_headers = expected_sheets.get(sheet.worksheet.title.replace("module", "menu"), None)
    else:
        expected_headers = expected_sheets.get(sheet.worksheet.title, None)
    return expected_headers
def _process_rows(app, sheet_name, rows, names_map, lang=None):
    """
    Processes the rows of a worksheet of translations.

    This is the complement of get_bulk_app_sheets_by_name() and
    get_bulk_app_single_sheet_by_name(), from
    corehq/apps/translations/app_translations/download.py, which creates
    these worksheets and rows.

    :param app: The application being translated
    :param sheet_name: The tab name of the sheet being processed.
                       e.g. "menu1", "menu1_form1", or "Menus_and_forms"
    :param rows: The rows in the worksheet
    :param names_map: A map of sheet_name to module/form unique_id, used
                      to fetch a module/form even if it has been moved
                      since the worksheet was created
    :param lang: The language that the app is being translated into
    :return: A list of error messages or an empty list
    """
    if not sheet_name or not rows:
        return []

    if is_modules_and_forms_sheet(sheet_name):
        updater = BulkAppTranslationModulesAndFormsUpdater(app,
                                                           names_map,
                                                           lang=lang)
        return updater.update(rows)

    if is_module_sheet(sheet_name):
        unique_id = names_map.get(sheet_name)
        try:
            updater = BulkAppTranslationModuleUpdater(app,
                                                      sheet_name,
                                                      unique_id,
                                                      lang=lang)
        except ModuleNotFoundException:
            return [(messages.error,
                     _('Invalid menu in row "%s", skipping row.') % sheet_name)
                    ]
        return updater.update(rows)

    if is_form_sheet(sheet_name):
        unique_id = names_map.get(sheet_name)
        try:
            updater = BulkAppTranslationFormUpdater(app,
                                                    sheet_name,
                                                    unique_id,
                                                    lang=lang)
        except FormNotFoundException:
            return [(messages.error,
                     _('Invalid form in row "%s", skipping row.') % sheet_name)
                    ]
        return updater.update(rows)

    return [(messages.error,
             _('Did not recognize "%s", skipping row.') % sheet_name)]
Exemple #4
0
def _process_rows(app, sheet_name, rows, names_map, lang=None):
    """
    Processes the rows of a worksheet of translations.

    This is the complement of get_bulk_app_sheets_by_name() and
    get_bulk_app_single_sheet_by_name(), from
    corehq/apps/translations/app_translations/download.py, which creates
    these worksheets and rows.

    :param app: The application being translated
    :param sheet_name: The tab name of the sheet being processed.
                       e.g. "menu1", "menu1_form1", or "Menus_and_forms"
    :param rows: The rows in the worksheet
    :param names_map: A map of sheet_name to module/form unique_id, used
                      to fetch a module/form even if it has been moved
                      since the worksheet was created
    :param lang: The language that the app is being translated into
    :return: A list of error messages or an empty list
    """
    if not sheet_name or not rows:
        return []

    if is_modules_and_forms_sheet(sheet_name):
        updater = BulkAppTranslationModulesAndFormsUpdater(app, names_map, lang=lang)
        return updater.update(rows)

    if is_module_sheet(sheet_name):
        unique_id = names_map.get(sheet_name)
        try:
            updater = BulkAppTranslationModuleUpdater(app, sheet_name, unique_id, lang=lang)
        except ModuleNotFoundException:
            return [(
                messages.error,
                _('Invalid menu in row "%s", skipping row.') % sheet_name
            )]
        return updater.update(rows)

    if is_form_sheet(sheet_name):
        unique_id = names_map.get(sheet_name)
        try:
            updater = BulkAppTranslationFormUpdater(app, sheet_name, unique_id, lang=lang)
        except FormNotFoundException:
            return [(
                messages.error,
                _('Invalid form in row "%s", skipping row.') % sheet_name
            )]
        return updater.update(rows)

    return [(
        messages.error,
        _('Did not recognize "%s", skipping row.') % sheet_name
    )]
Exemple #5
0
    def compare(self):
        msgs = {}
        self._generate_expected_headers_and_rows()
        for sheet in self.uploaded_workbook.worksheets:
            sheet_name = sheet.worksheet.title
            # if sheet is not in the expected rows, ignore it. This can happen if the module/form sheet is excluded
            # from transifex integration
            if sheet_name not in self.expected_rows:
                continue

            rows = get_unicode_dicts(sheet)
            if is_modules_and_forms_sheet(sheet.worksheet.title):
                error_msgs = self._compare_sheet(sheet_name, rows, 'module_and_form')
            elif is_module_sheet(sheet.worksheet.title):
                error_msgs = self._compare_sheet(sheet_name, rows, 'module')
            elif is_form_sheet(sheet.worksheet.title):
                error_msgs = self._compare_sheet(sheet_name, rows, 'form')
            else:
                raise Exception("Got unexpected sheet name %s" % sheet_name)
            if error_msgs:
                msgs[sheet_name] = error_msgs
        return msgs
Exemple #6
0
 def _compare_multiple_sheets(self):
     msgs = {}
     sheets_rows = self._parse_uploaded_worksheet()
     for sheet_name in sheets_rows:
         if sheet_name == MODULES_AND_FORMS_SHEET_NAME:
             uploaded_module_or_form_id = MODULES_AND_FORMS_SHEET_NAME
         else:
             uploaded_module_or_form_id = self.uploaded_sheet_name_to_module_or_form_type_and_id[
                 sheet_name].id
         rows = sheets_rows[sheet_name]
         if is_modules_and_forms_sheet(sheet_name):
             error_msgs = self._compare_sheet(uploaded_module_or_form_id,
                                              rows, 'module_and_form')
         elif is_module_sheet(sheet_name):
             error_msgs = self._compare_sheet(uploaded_module_or_form_id,
                                              rows, 'module')
         elif is_form_sheet(sheet_name):
             error_msgs = self._compare_sheet(uploaded_module_or_form_id,
                                              rows, 'form')
         else:
             raise Exception("Got unexpected sheet name %s" % sheet_name)
         if error_msgs:
             msgs[sheet_name] = error_msgs
     return msgs