def test_mixed_update_translations(self): instance = Normal.objects.language('en').get(pk=self.normal_id[1]) Formset = translationformset_factory(Normal, extra=1, exclude=[]) initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') data.set_formset_field(initial, 1, 'translated_field', 'updated_ja') data.set_formset_field(initial, 2, 'language_code', 'de') data.set_formset_field(initial, 2, 'translated_field', 'Deutsch') formset = Formset(data=data, instance=instance) self.assertTrue(formset.is_valid()) formset.save() self.assertCountEqual(instance.translations.all_languages(), ('ja', 'de')) obj = Normal.objects.language('ja').get(pk=instance.pk) self.assertEqual(obj.shared_field, NORMAL[1].shared_field) self.assertEqual(obj.translated_field, 'updated_ja') obj = Normal.objects.language('de').get(pk=instance.pk) self.assertEqual(obj.shared_field, NORMAL[1].shared_field) self.assertEqual(obj.translated_field, 'Deutsch')
def test_create_translations(self): Formset = translationformset_factory(Normal, extra=1) initial = Formset(instance=self.object) data = FormData(initial) data.set_formset_field(initial, 2, 'language_code', 'de') data.set_formset_field(initial, 2, 'translated_field', 'translated_test_de') formset = Formset(data=data, instance=self.object) formset.save() obj = Normal.objects.language('de').get(pk=self.object.pk) self.assertEqual(obj.translated_field, 'translated_test_de') self.assertEqual(obj.translations.count(), 3)
def test_create_translations(self): instance = Normal.objects.untranslated().get(pk=self.normal_id[1]) Formset = translationformset_factory(Normal, extra=1, exclude=[]) initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 2, 'language_code', 'de') data.set_formset_field(initial, 2, 'translated_field', 'Deutsch') formset = Formset(data=data, instance=instance) formset.save() obj = Normal.objects.language('de').get(pk=instance.pk) self.assertEqual(obj.translated_field, 'Deutsch') self.assertEqual(obj.translations.count(), 3)
def test_mixed_update_translations(self): Formset = translationformset_factory(Normal, extra=1) initial = Formset(instance=self.object) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') data.set_formset_field(initial, 1, 'translated_field', 'updated_fr') data.set_formset_field(initial, 2, 'language_code', 'de') data.set_formset_field(initial, 2, 'translated_field', 'translated_test_de') formset = Formset(data=data, instance=self.object) self.assertTrue(formset.is_valid()) formset.save() self.assertCountEqual(self.object.get_available_languages(), ('fr', 'de')) obj = Normal.objects.language('fr').get(pk=self.object.pk) self.assertEqual(obj.shared_field, 'test') self.assertEqual(obj.translated_field, 'updated_fr') obj = Normal.objects.language('de').get(pk=self.object.pk) self.assertEqual(obj.shared_field, 'test') self.assertEqual(obj.translated_field, 'translated_test_de')
def test_mixed_update_translations(self): instance = Normal.objects.language('en').get(pk=self.normal_id[1]) Formset = translationformset_factory(Normal, extra=1) initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') data.set_formset_field(initial, 1, 'translated_field', 'updated_ja') data.set_formset_field(initial, 2, 'language_code', 'de') data.set_formset_field(initial, 2, 'translated_field', 'Deutsch') formset = Formset(data=data, instance=instance) self.assertTrue(formset.is_valid()) formset.save() self.assertCountEqual(instance.get_available_languages(), ('ja', 'de')) obj = Normal.objects.language('ja').get(pk=instance.pk) self.assertEqual(obj.shared_field, NORMAL[1].shared_field) self.assertEqual(obj.translated_field, 'updated_ja') obj = Normal.objects.language('de').get(pk=instance.pk) self.assertEqual(obj.shared_field, NORMAL[1].shared_field) self.assertEqual(obj.translated_field, 'Deutsch')
def test_delete_translations(self): instance = Normal.objects.language('en').get(pk=self.normal_id[1]) Formset = translationformset_factory(Normal, extra=1, exclude=[]) # Delete one of the two translations initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') formset = Formset(data=data, instance=instance) self.assertTrue(formset.is_valid()) formset.save() self.assertCountEqual(instance.get_available_languages(), ('ja', )) # Try to delete the other translation - should fail initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') formset = Formset(data=data, instance=instance) self.assertFalse(formset.is_valid())
def test_delete_translations(self): Formset = translationformset_factory(Normal, extra=1) # Delete one of the two translations initial = Formset(instance=self.object) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') formset = Formset(data=data, instance=self.object) self.assertTrue(formset.is_valid()) formset.save() self.assertCountEqual(self.object.get_available_languages(), ('fr',)) # Try to delete the other translation - should fail initial = Formset(instance=self.object) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') formset = Formset(data=data, instance=self.object) self.assertFalse(formset.is_valid())
def test_delete_translations(self): instance = Normal.objects.language('en').get(pk=self.normal_id[1]) Formset = translationformset_factory(Normal, extra=1, exclude=[]) # Delete one of the two translations initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') formset = Formset(data=data, instance=instance) self.assertTrue(formset.is_valid()) formset.save() self.assertCountEqual(instance.get_available_languages(), ('ja',)) # Try to delete the other translation - should fail initial = Formset(instance=instance) data = FormData(initial) data.set_formset_field(initial, 0, 'DELETE', 'DELETE') formset = Formset(data=data, instance=instance) self.assertFalse(formset.is_valid())