def test_triple(self): normal = Normal.objects.language('en').create(shared_field='SHARED', translated_field='English') other = Other.objects.create(normal=normal) standard = Standard.objects.create(normal=normal, normal_field='NORMAL FIELD') obj = Normal.objects.language('en').get(standards__pk=standard.pk) self.assertEqual(obj.pk, normal.pk) obj = Normal.objects.language('en').get(others__pk=other.pk) self.assertEqual(obj.pk, normal.pk) # We created an english Normal object, so we want to make sure that we use 'en' with LanguageOverride('en'): obj = get_translation_aware_manager(Standard).get(normal__others__pk=other.pk) self.assertEqual(obj.pk, standard.pk) # If we don't use language 'en', it should give DoesNotExist, when using the # translation aware manager with LanguageOverride('ja'): manager = get_translation_aware_manager(Standard) self.assertRaises(Standard.DoesNotExist, manager.get, normal__others__pk=other.pk) # However, if we don't use the translation aware manager, we can query any # the shared fields in any language, and it should return the object, # even though there is no translated Normal objects with LanguageOverride('ja'): obj = Standard.objects.get(normal__others__pk=other.pk) self.assertEqual(obj.pk, standard.pk)
def test_triple(self): normal = Normal.objects.language('en').create( shared_field='SHARED', translated_field='English') other = Other.objects.create(normal=normal) standard = Standard.objects.create(normal=normal, normal_field='NORMAL FIELD') obj = Normal.objects.language('en').get(standards__pk=standard.pk) self.assertEqual(obj.pk, normal.pk) obj = Normal.objects.language('en').get(others__pk=other.pk) self.assertEqual(obj.pk, normal.pk) # We created an english Normal object, so we want to make sure that we use 'en' with LanguageOverride('en'): obj = get_translation_aware_manager(Standard).get( normal__others__pk=other.pk) self.assertEqual(obj.pk, standard.pk) # If we don't use language 'en', it should give DoesNotExist, when using the # translation aware manager with LanguageOverride('ja'): manager = get_translation_aware_manager(Standard) self.assertRaises(Standard.DoesNotExist, manager.get, normal__others__pk=other.pk) # However, if we don't use the translation aware manager, we can query any # the shared fields in any language, and it should return the object, # even though there is no translated Normal objects with LanguageOverride('ja'): obj = Standard.objects.get(normal__others__pk=other.pk) self.assertEqual(obj.pk, standard.pk)
def fix_mca_translations(): player_activities = PlayerActivity.objects.all().order_by( 'pk') # filter(id=3260) tam = get_translation_aware_manager(MultiChoiceActivity) for activity in player_activities: english = tam.filter(activity=activity, language_code='en-us') english_list = list(english) spanish = tam.filter(activity=activity, language_code='es') spanish_list = list(spanish) creole = tam.filter(activity=activity, language_code='ht') creole_list = list(creole) for i, choice in enumerate(english): #print "CHOICE %s/%s BEFORE TRANSLATION:" % (choice.activity, choice.pk) #print [(c.pk, c.language_code, c.value) for c in choice.translations.all()] try: choice.translate('es') choice.value = spanish_list[i].translations.all()[0].value choice.save() except Exception, e: print "ACTIVITY %s ('%s') LACKS SPANISH TRANSLATION" % ( activity.pk, activity) try: choice.translate('ht') choice.value = creole_list[i].translations.all()[0].value choice.save() except Exception, e: print "ACTIVITY %s ('%s') LACKS CREOLE TRANSLATION" % ( activity.pk, activity)
def test_lookup_by_translated_field(self): en = Normal.objects.language('en').get(pk=1) translation_aware_manager = get_translation_aware_manager(Standard) with LanguageOverride('en'): by_translated_field = translation_aware_manager.get(normal__translated_field=en.translated_field) self.assertEqual(by_translated_field.normal.pk, en.pk) self.assertEqual(by_translated_field.normal.shared_field, en.shared_field) self.assertEqual(by_translated_field.normal.translated_field, en.translated_field) self.assertTrue(by_translated_field in en.standards.all())
def test_lookup_by_translated_field(self): en = Normal.objects.language('en').get(pk=1) translation_aware_manager = get_translation_aware_manager(Standard) with LanguageOverride('en'): by_translated_field = translation_aware_manager.get( normal__translated_field=en.translated_field) self.assertEqual(by_translated_field.normal.pk, en.pk) self.assertEqual(by_translated_field.normal.shared_field, en.shared_field) self.assertEqual(by_translated_field.normal.translated_field, en.translated_field) self.assertTrue(by_translated_field in en.standards.all())
def test_filter_by_translated_field(self): en = Normal.objects.language('en').get(pk=1) translation_aware_manager = get_translation_aware_manager(Standard) with LanguageOverride('en'): by_translated_field = translation_aware_manager.filter(normal__translated_field=en.translated_field) normals = [obj.normal.pk for obj in by_translated_field] expected = [en.pk] self.assertEqual(normals, expected) shared_fields = [obj.normal.shared_field for obj in by_translated_field] expected_fields = [en.shared_field] self.assertEqual(shared_fields, expected_fields) translated_fields = [obj.normal.translated_field for obj in by_translated_field] expected_fields = [en.translated_field] self.assertEqual(translated_fields, expected_fields) for obj in by_translated_field: self.assertTrue(obj in en.standards.all())
def test_filter_by_translated_field_using_q_objects(self): en = Normal.objects.language('en').get(pk=1) translation_aware_manager = get_translation_aware_manager(Standard) with LanguageOverride('en'): q = Q(normal__translated_field=en.translated_field) by_translated_field = translation_aware_manager.filter(q) normals = [obj.normal.pk for obj in by_translated_field] expected = [en.pk] self.assertEqual(normals, expected) shared_fields = [ obj.normal.shared_field for obj in by_translated_field ] expected_fields = [en.shared_field] self.assertEqual(shared_fields, expected_fields) translated_fields = [ obj.normal.translated_field for obj in by_translated_field ] expected_fields = [en.translated_field] self.assertEqual(translated_fields, expected_fields) for obj in by_translated_field: self.assertTrue(obj in en.standards.all())