Esempio n. 1
0
def pull_translation_files_from_transifex(domain, data, email=None):
    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('target_lang') or data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          lock_translations=data.get('lock_translations'),
                          use_version_postfix='yes'
                          in data['use_version_postfix'])
    translation_file = None
    try:
        translation_file, filename = transifex.generate_excel_file()
        with open(translation_file.name, 'rb') as file_obj:
            email = EmailMessage(
                subject='[{}] - Transifex pulled translations'.format(
                    settings.SERVER_ENVIRONMENT),
                body="PFA Translations pulled from transifex.",
                to=[email],
                from_email=settings.DEFAULT_FROM_EMAIL)
            email.attach(filename=filename, content=file_obj.read())
            email.send()
    finally:
        if translation_file and os.path.exists(translation_file.name):
            os.remove(translation_file.name)
Esempio n. 2
0
def backup_project_from_transifex(domain, data, email):
    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          use_version_postfix='yes' in data['use_version_postfix'])
    project_details = transifex.client.project_details().json()
    target_lang_codes = project_details.get('teams')
    with NamedTemporaryFile(mode='w+b', suffix='.zip') as tmp:
        with ZipFile(tmp, 'w') as zipfile:
            for target_lang in target_lang_codes:
                transifex = Transifex(domain,
                                      data.get('app_id'),
                                      target_lang,
                                      data.get('transifex_project_slug'),
                                      version,
                                      use_version_postfix='yes' in data['use_version_postfix'])
                translation_file, filename = transifex.generate_excel_file()
                with open(translation_file.name, 'rb') as file_obj:
                    zipfile.writestr(filename, file_obj.read())
                os.remove(translation_file.name)
        tmp.seek(0)
        email = EmailMessage(
            subject='[{}] - Transifex backup translations'.format(settings.SERVER_ENVIRONMENT),
            body="PFA Translations backup from transifex.",
            to=[email],
            from_email=settings.DEFAULT_FROM_EMAIL
        )
        filename = "%s-TransifexBackup.zip" % project_details.get('name')
        email.attach(filename=filename, content=tmp.read())
        email.send()
Esempio n. 3
0
def backup_project_from_transifex(domain, data, email):
    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          use_version_postfix='yes'
                          in data['use_version_postfix'])
    project_details = transifex.client.project_details().json()
    target_lang_codes = project_details.get('teams')
    with NamedTemporaryFile(mode='w+b', suffix='.zip') as tmp:
        with ZipFile(tmp, 'w') as zipfile:
            for target_lang in target_lang_codes:
                transifex = Transifex(domain,
                                      data.get('app_id'),
                                      target_lang,
                                      data.get('transifex_project_slug'),
                                      version,
                                      use_version_postfix='yes'
                                      in data['use_version_postfix'])
                translation_file, filename = transifex.generate_excel_file()
                with open(translation_file.name, 'rb') as file_obj:
                    zipfile.writestr(filename, file_obj.read())
                os.remove(translation_file.name)
        tmp.seek(0)
        email = EmailMessage(
            subject='[{}] - Transifex backup translations'.format(
                settings.SERVER_ENVIRONMENT),
            body="PFA Translations backup from transifex.",
            to=[email],
            from_email=settings.DEFAULT_FROM_EMAIL)
        filename = "%s-TransifexBackup.zip" % project_details.get('name')
        email.attach(filename=filename, content=tmp.read())
        email.send()
Esempio n. 4
0
 def transifex(self, domain, form_data):
     transifex_project_slug = form_data.get('transifex_project_slug')
     source_language_code = form_data.get('target_lang') or form_data.get('source_lang')
     return Transifex(domain, form_data['app_id'], source_language_code, transifex_project_slug,
                      form_data['version'],
                      use_version_postfix='yes' in form_data['use_version_postfix'],
                      update_resource=(form_data['action'] == 'update'))
Esempio n. 5
0
def pull_translation_files_from_transifex(domain, data, user_email=None):
    def notify_error(error):
        email = EmailMessage(
            subject='[{}] - Transifex pulled translations'.format(
                settings.SERVER_ENVIRONMENT),
            body=
            "The request could not be completed. Something went wrong with the download. "
            "Error raised : {}. "
            "If you see this repeatedly and need support, please report an issue. "
            .format(error),
            to=[user_email],
            from_email=settings.DEFAULT_FROM_EMAIL)
        email.send()

    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('target_lang') or data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          lock_translations=data.get('lock_translations'),
                          use_version_postfix='yes'
                          in data['use_version_postfix'])
    translation_file = None
    try:
        translation_file, filename = transifex.generate_excel_file()
        with open(translation_file.name, 'rb') as file_obj:
            email = EmailMessage(
                subject='[{}] - Transifex pulled translations'.format(
                    settings.SERVER_ENVIRONMENT),
                body="PFA Translations pulled from transifex.",
                to=[user_email],
                from_email=settings.DEFAULT_FROM_EMAIL)
            email.attach(filename=filename, content=file_obj.read())
            email.send()
    except Exception as e:
        notify_error(e)
        six.reraise(*sys.exc_info())
    finally:
        if translation_file and os.path.exists(translation_file.name):
            os.remove(translation_file.name)
Esempio n. 6
0
def delete_resources_on_transifex(domain, data, email):
    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('target_lang') or data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          use_version_postfix='yes' in data['use_version_postfix'])
    delete_status = transifex.delete_resources()
    result_note = "Hi,\nThe request to delete resources for app {app_id}(version {version}), " \
                  "was completed on project {transifex_project_slug} on transifex. " \
                  "The result is as follows:\n".format(**data)
    email = EmailMessage(
        subject='[{}] - Transifex removed translations'.format(settings.SERVER_ENVIRONMENT),
        body=(result_note +
              "\n".join([' '.join([sheet_name, result]) for sheet_name, result in delete_status.items()])
              ),
        to=[email],
        from_email=settings.DEFAULT_FROM_EMAIL
    )
    email.send()
Esempio n. 7
0
def push_translation_files_to_transifex(domain, data, email):
    upload_status = None
    if data.get('target_lang'):
        upload_status = Transifex(
            domain,
            data.get('app_id'),
            data.get('target_lang'),
            data.get('transifex_project_slug'),
            data.get('version'),
            is_source_file=False,
            exclude_if_default=True,
            use_version_postfix='yes'
            in data['use_version_postfix']).send_translation_files()
    elif data.get('source_lang'):
        upload_status = Transifex(
            domain,
            data.get('app_id'),
            data.get('source_lang'),
            data.get('transifex_project_slug'),
            data.get('version'),
            use_version_postfix='yes' in data['use_version_postfix'],
            update_resource=(
                data['action'] == 'update')).send_translation_files()
    data['language'] = data.get('target_lang') or data.get('source_lang')
    if upload_status:
        result_note = "Hi,\nThe upload for app {app_id}(version {version}), " \
                      "for language '{language}' " \
                      "was completed on project {transifex_project_slug} on transifex. " \
                      "The result is as follows:\n".format(**data)
        email = EmailMessage(
            subject='[{}] - Transifex pushed translations'.format(
                settings.SERVER_ENVIRONMENT),
            body=(result_note + "\n".join([
                ' '.join([sheet_name, result])
                for sheet_name, result in upload_status.items()
            ])),
            to=[email],
            from_email=settings.DEFAULT_FROM_EMAIL)
        email.send()
Esempio n. 8
0
def delete_resources_on_transifex(domain, data, email):
    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('target_lang') or data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          use_version_postfix='yes'
                          in data['use_version_postfix'])
    delete_status = transifex.delete_resources()
    result_note = "Hi,\nThe request to delete resources for app {app_id}(version {version}), " \
                  "was completed on project {transifex_project_slug} on transifex. " \
                  "The result is as follows:\n".format(**data)
    email = EmailMessage(
        subject='[{}] - Transifex removed translations'.format(
            settings.SERVER_ENVIRONMENT),
        body=(result_note + "\n".join([
            ' '.join([sheet_name, result])
            for sheet_name, result in delete_status.items()
        ])),
        to=[email],
        from_email=settings.DEFAULT_FROM_EMAIL)
    email.send()
Esempio n. 9
0
    def _generate_response_file(self, domain, project_slug, resource_slug):
        """
        extract translations from po file(s) pulled from transifex and converts to a xlsx/zip file

        :return: Workbook object or BytesIO object
        """
        target_lang = self.pull_resource_form.cleaned_data['target_lang']
        transifex = Transifex(domain=domain, app_id=None,
                              source_lang=target_lang,
                              project_slug=project_slug)
        if resource_slug:
            return self._generate_excel_file(transifex, resource_slug, target_lang)
        else:
            return self._generate_zip_file(transifex, target_lang)
Esempio n. 10
0
def pull_translation_files_from_transifex(domain, data, email=None):
    version = data.get('version')
    transifex = Transifex(domain,
                          data.get('app_id'),
                          data.get('target_lang') or data.get('source_lang'),
                          data.get('transifex_project_slug'),
                          version,
                          lock_translations=data.get('lock_translations'),
                          use_version_postfix='yes' in data['use_version_postfix'])
    translation_file = None
    try:
        translation_file, filename = transifex.generate_excel_file()
        with open(translation_file.name, 'rb') as file_obj:
            email = EmailMessage(
                subject='[{}] - Transifex pulled translations'.format(settings.SERVER_ENVIRONMENT),
                body="PFA Translations pulled from transifex.",
                to=[email],
                from_email=settings.DEFAULT_FROM_EMAIL
            )
            email.attach(filename=filename, content=file_obj.read())
            email.send()
    finally:
        if translation_file and os.path.exists(translation_file.name):
            os.remove(translation_file.name)
Esempio n. 11
0
def push_translation_files_to_transifex(domain, data, email):
    upload_status = None
    if data.get('target_lang'):
        upload_status = Transifex(domain,
                                  data.get('app_id'),
                                  data.get('target_lang'),
                                  data.get('transifex_project_slug'),
                                  data.get('version'),
                                  is_source_file=False,
                                  exclude_if_default=True,
                                  use_version_postfix='yes' in data['use_version_postfix']
                                  ).send_translation_files()
    elif data.get('source_lang'):
        upload_status = Transifex(domain,
                                  data.get('app_id'),
                                  data.get('source_lang'),
                                  data.get('transifex_project_slug'),
                                  data.get('version'),
                                  use_version_postfix='yes' in data['use_version_postfix'],
                                  update_resource=(data['action'] == 'update')
                                  ).send_translation_files()
    data['language'] = data.get('target_lang') or data.get('source_lang')
    if upload_status:
        result_note = "Hi,\nThe upload for app {app_id}(version {version}), " \
                      "for language '{language}' " \
                      "was completed on project {transifex_project_slug} on transifex. " \
                      "The result is as follows:\n".format(**data)
        email = EmailMessage(
            subject='[{}] - Transifex pushed translations'.format(settings.SERVER_ENVIRONMENT),
            body=(result_note +
                  "\n".join([' '.join([sheet_name, result]) for sheet_name, result in upload_status.items()])
                  ),
            to=[email],
            from_email=settings.DEFAULT_FROM_EMAIL
        )
        email.send()
Esempio n. 12
0
    def _generate_excel_file(self, domain, resource_slug, target_lang):
        """
        extract translations from po file pulled from transifex and converts to a xlsx file

        :return: Workbook object
        """
        transifex = Transifex(domain=domain,
                              app_id=None,
                              source_lang=target_lang,
                              project_slug=self.pull_resource_form.
                              cleaned_data['transifex_project_slug'],
                              version=None)
        wb = openpyxl.Workbook(write_only=True)
        ws = wb.create_sheet(title='translations')
        ws.append(['context', 'source', 'translation', 'occurrence'])
        for po_entry in transifex.client.get_translation(
                resource_slug, target_lang, False):
            ws.append([
                po_entry.msgctxt, po_entry.msgid, po_entry.msgstr,
                po_entry.occurrences[0][0] if po_entry.occurrences else ''
            ])
        return wb