Ejemplo n.º 1
0
    def test_render_formset(self):
        with self.assertNumQueries(1):
            Formset = translationformset_factory(Normal, extra=1)
            formset = Formset(instance=self.object)
            self.assertEqual(len(formset.forms), 3)
            self.assertIn('translated_field', formset.forms[0].fields)
            self.assertIn('language_code', formset.forms[0].fields)
            self.assertIn('DELETE', formset.forms[0].fields)
            self.assertIn('id', formset.forms[0].fields)
            self.assertNotIn('master', formset.forms[0].fields)
            self.assertEqual(formset.forms[0].initial['language_code'], 'en')
            self.assertEqual(formset.forms[0].initial['translated_field'], 'translated_test_en')
            self.assertEqual(formset.forms[1].initial['language_code'], 'fr')
            self.assertEqual(formset.forms[1].initial['translated_field'], 'translated_test_fr')
            self.assertEqual(formset.forms[2].initial, {})

        with self.assertNumQueries(1):
            class Form(ModelForm):
                class Meta:
                    fields = ('translated_field',)
            Formset = translationformset_factory(Normal, form=Form, extra=1)
            formset = Formset(instance=self.object)
            self.assertIn('translated_field', formset.forms[0].fields)
            self.assertIn('language_code', formset.forms[0].fields)
            self.assertIn('DELETE', formset.forms[0].fields)
            self.assertIn('id', formset.forms[0].fields)
            self.assertNotIn('master', formset.forms[0].fields)
Ejemplo n.º 2
0
    def test_render_formset(self):
        instance = Normal.objects.language('en').get(pk=self.normal_id[1])
        with self.assertNumQueries(1):
            Formset = translationformset_factory(Normal, extra=1, exclude=[])
            formset = Formset(instance=instance)
            self.assertEqual(len(formset.forms), 3)
            self.assertIn('translated_field', formset.forms[0].fields)
            self.assertIn('language_code', formset.forms[0].fields)
            self.assertIn('DELETE', formset.forms[0].fields)
            self.assertIn('id', formset.forms[0].fields)
            self.assertNotIn('master', formset.forms[0].fields)
            self.assertEqual(formset.forms[0].initial['language_code'], 'en')
            self.assertEqual(formset.forms[0].initial['translated_field'],
                             NORMAL[1].translated_field['en'])
            self.assertEqual(formset.forms[1].initial['language_code'], 'ja')
            self.assertEqual(formset.forms[1].initial['translated_field'],
                             NORMAL[1].translated_field['ja'])
            self.assertEqual(formset.forms[2].initial, {})

        with self.assertNumQueries(1):

            class Form(ModelForm):
                class Meta:
                    fields = ('translated_field', )

            Formset = translationformset_factory(Normal,
                                                 form=Form,
                                                 extra=1,
                                                 exclude=[])
            formset = Formset(instance=instance)
            self.assertIn('translated_field', formset.forms[0].fields)
            self.assertIn('language_code', formset.forms[0].fields)
            self.assertIn('DELETE', formset.forms[0].fields)
            self.assertIn('id', formset.forms[0].fields)
            self.assertNotIn('master', formset.forms[0].fields)
Ejemplo n.º 3
0
    def test_render_formset(self):
        instance = Normal.objects.language('en').get(pk=self.normal_id[1])
        with self.assertNumQueries(1):
            Formset = translationformset_factory(Normal, extra=1)
            formset = Formset(instance=instance)
            self.assertEqual(len(formset.forms), 3)
            self.assertIn('translated_field', formset.forms[0].fields)
            self.assertIn('language_code', formset.forms[0].fields)
            self.assertIn('DELETE', formset.forms[0].fields)
            self.assertIn('id', formset.forms[0].fields)
            self.assertNotIn('master', formset.forms[0].fields)
            self.assertEqual(formset.forms[0].initial['language_code'], 'en')
            self.assertEqual(formset.forms[0].initial['translated_field'],
                             NORMAL[1].translated_field['en'])
            self.assertEqual(formset.forms[1].initial['language_code'], 'ja')
            self.assertEqual(formset.forms[1].initial['translated_field'],
                             NORMAL[1].translated_field['ja'])
            self.assertEqual(formset.forms[2].initial, {})

        with self.assertNumQueries(1):
            class Form(ModelForm):
                class Meta:
                    fields = ('translated_field',)
            Formset = translationformset_factory(Normal, form=Form, extra=1)
            formset = Formset(instance=instance)
            self.assertIn('translated_field', formset.forms[0].fields)
            self.assertIn('language_code', formset.forms[0].fields)
            self.assertIn('DELETE', formset.forms[0].fields)
            self.assertIn('id', formset.forms[0].fields)
            self.assertNotIn('master', formset.forms[0].fields)
Ejemplo n.º 4
0
    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')
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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())
Ejemplo n.º 9
0
    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())
Ejemplo n.º 10
0
    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())
Ejemplo n.º 11
0
    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')
Ejemplo n.º 12
0
    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')