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, }
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, }
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, }