Ejemplo n.º 1
0
def approve_translations(form, user, translations, locale):
    """Approve a series of translations.

    For documentation, refer to the `batch_action_template` function.

    """
    translations = translations.filter(approved=False)
    changed_translation_pks = list(translations.values_list('pk', flat=True))

    latest_translation_pk = None
    if changed_translation_pks:
        latest_translation_pk = translations.last().pk

    count, translated_resources, changed_entities = utils.get_translations_info(
        translations,
        locale,
    )
    translations.update(
        approved=True,
        approved_user=user,
        approved_date=timezone.now(),
        rejected=False,
        rejected_user=None,
        rejected_date=None,
    )

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': latest_translation_pk,
        'changed_translation_pks': changed_translation_pks,
    }
Ejemplo n.º 2
0
def approve_translations(form, user, translations, locale):
    """Approve a series of translations.

    For documentation, refer to the `batch_action_template` function.

    """
    invalid_translation_pks = list(
        translations.filter(
            approved=False,
            errors__isnull=False,
        ).values_list("pk", flat=True)
    )

    translations = translations.filter(
        approved=False,
        errors__isnull=True,
    )
    changed_translation_pks = list(translations.values_list("pk", flat=True))

    latest_translation_pk = None
    if changed_translation_pks:
        latest_translation_pk = translations.last().pk

    count, translated_resources, changed_entities = utils.get_translations_info(
        translations,
        locale,
    )

    # Log approving actions
    actions_to_log = [
        ActionLog(
            action_type=ActionLog.ActionType.TRANSLATION_APPROVED,
            performed_by=user,
            translation=t,
        )
        for t in translations
    ]
    ActionLog.objects.bulk_create(actions_to_log)

    # Approve translations.
    translations.update(
        approved=True,
        approved_user=user,
        approved_date=timezone.now(),
        rejected=False,
        rejected_user=None,
        rejected_date=None,
        pretranslated=False,
        fuzzy=False,
    )

    return {
        "count": count,
        "translated_resources": translated_resources,
        "changed_entities": changed_entities,
        "latest_translation_pk": latest_translation_pk,
        "changed_translation_pks": changed_translation_pks,
        "invalid_translation_pks": invalid_translation_pks,
    }
Ejemplo n.º 3
0
def replace_translations(form, user, translations, locale):
    """Replace characters in a series of translations.

    Replaces all occurences of the content of the `find` parameter with the
    content of the `replace` parameter.

    For documentation, refer to the `batch_action_template` function.

    """
    find = form.cleaned_data['find']
    replace = form.cleaned_data['replace']
    latest_translation_pk = None

    old_translations, translations_to_create, invalid_translation_pks = utils.find_and_replace(
        translations,
        find,
        replace,
        user
    )

    count, translated_resources, changed_entities = utils.get_translations_info(
        old_translations,
        locale,
    )

    # Deactivate and unapprove old translations
    old_translations.update(
        active=False,
        approved=False,
        approved_user=None,
        approved_date=None,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        fuzzy=False,
    )

    # Create new translations
    changed_translations = Translation.objects.bulk_create(
        translations_to_create,
    )

    changed_translation_pks = [c.pk for c in changed_translations]

    if changed_translation_pks:
        latest_translation_pk = max(changed_translation_pks)

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': latest_translation_pk,
        'changed_translation_pks': changed_translation_pks,
        'invalid_translation_pks': invalid_translation_pks,
    }
Ejemplo n.º 4
0
def replace_translations(form, user, translations, locale):
    """Replace characters in a series of translations.

    Replaces all occurences of the content of the `find` parameter with the
    content of the `replace` parameter.

    For documentation, refer to the `batch_action_template` function.

    """
    find = form.cleaned_data['find']
    replace = form.cleaned_data['replace']
    latest_translation_pk = None

    old_translations, translations_to_create, invalid_translation_pks = utils.find_and_replace(
        translations,
        find,
        replace,
        user
    )

    count, translated_resources, changed_entities = utils.get_translations_info(
        old_translations,
        locale,
    )

    # Deactivate and unapprove old translations
    old_translations.update(
        active=False,
        approved=False,
        approved_user=None,
        approved_date=None,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        fuzzy=False,
    )

    # Create new translations
    changed_translations = Translation.objects.bulk_create(
        translations_to_create,
    )

    changed_translation_pks = [c.pk for c in changed_translations]

    if changed_translation_pks:
        latest_translation_pk = max(changed_translation_pks)

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': latest_translation_pk,
        'changed_translation_pks': changed_translation_pks,
        'invalid_translation_pks': invalid_translation_pks,
    }
Ejemplo n.º 5
0
def reject_translations(form, user, translations, locale):
    """Reject a series of translations.

    Note that this function doesn't use the `translations` parameter, as it
    needs to impact non-active translations. Hence it will generate its own
    list of suggested translations to work on.

    For documentation, refer to the `batch_action_template` function.

    """
    suggestions = Translation.objects.filter(
        locale=locale,
        entity__pk__in=form.cleaned_data["entities"],
        approved=False,
        rejected=False,
    )
    count, translated_resources, changed_entities = utils.get_translations_info(
        suggestions,
        locale,
    )
    TranslationMemoryEntry.objects.filter(translation__in=suggestions).delete()

    # Log rejecting actions
    actions_to_log = [
        ActionLog(
            action_type=ActionLog.ActionType.TRANSLATION_REJECTED,
            performed_by=user,
            translation=t,
        )
        for t in translations
    ]
    ActionLog.objects.bulk_create(actions_to_log)

    # Reject translations.
    suggestions.update(
        active=False,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        approved=False,
        approved_user=None,
        approved_date=None,
        pretranslated=False,
        fuzzy=False,
    )

    return {
        "count": count,
        "translated_resources": translated_resources,
        "changed_entities": changed_entities,
        "latest_translation_pk": None,
        "changed_translation_pks": [],
        "invalid_translation_pks": [],
    }
Ejemplo n.º 6
0
def replace_translations(form, user, translations, locale):
    """Replace characters in a series of translations.

    Replaces all occurences of the content of the `find` parameter with the
    content of the `replace` parameter.

    For documentation, refer to the `batch_action_template` function.

    """
    find = form.cleaned_data['find']
    replace = form.cleaned_data['replace']
    latest_translation_pk = None

    try:
        old_translations, changed_translations = utils.find_and_replace(
            translations, find, replace, user)
        changed_translation_pks = [c.pk for c in changed_translations]
        if changed_translation_pks:
            latest_translation_pk = max(changed_translation_pks)
    except Translation.NotAllowed:
        return {
            'error': 'Empty translations not allowed',
        }

    # Unapprove old translations
    old_translations.update(
        approved=False,
        approved_user=None,
        approved_date=None,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        fuzzy=False,
    )

    count, translated_resources, changed_entities = utils.get_translations_info(
        old_translations,
        locale,
    )

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': latest_translation_pk,
        'changed_translation_pks': changed_translation_pks,
    }
Ejemplo n.º 7
0
def approve_translations(form, user, translations, locale):
    """Approve a series of translations.

    For documentation, refer to the `batch_action_template` function.

    """
    invalid_translation_pks = list(
        translations.filter(
            approved=False,
            errors__isnull=False,
        ).values_list('pk', flat=True)
    )

    translations = translations.filter(
        approved=False,
        errors__isnull=True,
    )
    changed_translation_pks = list(translations.values_list('pk', flat=True))

    latest_translation_pk = None
    if changed_translation_pks:
        latest_translation_pk = translations.last().pk

    count, translated_resources, changed_entities = utils.get_translations_info(
        translations,
        locale,
    )
    translations.update(
        approved=True,
        approved_user=user,
        approved_date=timezone.now(),
        rejected=False,
        rejected_user=None,
        rejected_date=None,
        fuzzy=False,
    )

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': latest_translation_pk,
        'changed_translation_pks': changed_translation_pks,
        'invalid_translation_pks': invalid_translation_pks,
    }
Ejemplo n.º 8
0
def reject_translations(form, user, translations, locale):
    """Reject a series of translations.

    Note that this function doesn't use the `translations` parameter, as it
    needs to impact non-active translations. Hence it will generate its own
    list of suggested translations to work on.

    For documentation, refer to the `batch_action_template` function.

    """
    suggestions = Translation.objects.filter(
        locale=locale,
        entity__pk__in=form.cleaned_data['entities'],
        approved=False,
        rejected=False
    )
    count, translated_resources, changed_entities = utils.get_translations_info(
        suggestions,
        locale,
    )
    TranslationMemoryEntry.objects.filter(translation__in=suggestions).delete()
    suggestions.update(
        active=False,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        approved=False,
        approved_user=None,
        approved_date=None,
        fuzzy=False,
    )

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': None,
        'changed_translation_pks': [],
        'invalid_translation_pks': [],
    }
Ejemplo n.º 9
0
def reject_translations(form, user, translations, locale):
    """Reject a series of translations.

    Note that this function doesn't use the `translations` parameter, as it
    needs to impact non-active translations. Hence it will generate its own
    list of suggested translations to work on.

    For documentation, refer to the `batch_action_template` function.

    """
    suggestions = Translation.objects.filter(
        locale=locale,
        entity__pk__in=form.cleaned_data['entities'],
        approved=False,
        rejected=False
    )
    count, translated_resources, changed_entities = utils.get_translations_info(
        suggestions,
        locale,
    )
    TranslationMemoryEntry.objects.filter(translation__in=suggestions).delete()
    suggestions.update(
        active=False,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        approved=False,
        approved_user=None,
        approved_date=None,
        fuzzy=False,
    )

    return {
        'count': count,
        'translated_resources': translated_resources,
        'changed_entities': changed_entities,
        'latest_translation_pk': None,
        'changed_translation_pks': [],
        'invalid_translation_pks': [],
    }
Ejemplo n.º 10
0
def replace_translations(form, user, translations, locale):
    """Replace characters in a series of translations.

    Replaces all occurences of the content of the `find` parameter with the
    content of the `replace` parameter.

    For documentation, refer to the `batch_action_template` function.

    """
    find = form.cleaned_data["find"]
    replace = form.cleaned_data["replace"]
    latest_translation_pk = None

    (
        old_translations,
        translations_to_create,
        invalid_translation_pks,
    ) = utils.find_and_replace(translations, find, replace, user)

    count, translated_resources, changed_entities = utils.get_translations_info(
        old_translations,
        locale,
    )

    # Log rejecting actions
    actions_to_log = [
        ActionLog(
            action_type=ActionLog.ActionType.TRANSLATION_REJECTED,
            performed_by=user,
            translation=t,
        ) for t in old_translations
    ]
    ActionLog.objects.bulk_create(actions_to_log)

    # Deactivate and unapprove old translations
    old_translations.update(
        active=False,
        approved=False,
        approved_user=None,
        approved_date=None,
        rejected=True,
        rejected_user=user,
        rejected_date=timezone.now(),
        fuzzy=False,
    )

    # Create new translations
    changed_translations = Translation.objects.bulk_create(
        translations_to_create, )

    # Log creating actions
    actions_to_log = [
        ActionLog(
            action_type=ActionLog.ActionType.TRANSLATION_CREATED,
            performed_by=user,
            translation=t,
        ) for t in changed_translations
    ]
    ActionLog.objects.bulk_create(actions_to_log)

    changed_translation_pks = [c.pk for c in changed_translations]

    if changed_translation_pks:
        latest_translation_pk = max(changed_translation_pks)

    return {
        "count": count,
        "translated_resources": translated_resources,
        "changed_entities": changed_entities,
        "latest_translation_pk": latest_translation_pk,
        "changed_translation_pks": changed_translation_pks,
        "invalid_translation_pks": invalid_translation_pks,
    }