def _pre_save(sender, instance, **kwargs): setattr(instance, 'datatrans_old_language', get_current_language()) default_lang = get_default_language() translation.activate(default_lang) # When we edit a registered model, update the original translations and mark them as unedited (to do) if instance.pk is not None: try: # Just because instance.pk is set, it does not mean that the instance # is saved. Most typical/important ex: loading fixtures original = sender.objects.get(pk=instance.pk) except ObjectDoesNotExist: return None register = get_registry() fields = register[sender].values() for field in fields: old_digest = make_digest(original.__dict__[field.name]) new_digest = make_digest(instance.__dict__[field.name]) # If changed, update keyvalues if old_digest != new_digest: # Check if the new value already exists, if not, create a new one. The old one will be obsoleted. old_count = KeyValue.objects.filter(digest=old_digest).count() new_count = KeyValue.objects.filter(digest=new_digest).count() _invalidate_word_count(sender, field, instance) if old_count != new_count or new_count == 0: kvs = KeyValue.objects.filter(digest=old_digest) for kv in kvs: if KeyValue.objects.filter(digest=new_digest, language=kv.language).count() > 0: continue new_value = instance.__dict__[field.name] if kv.language == default_lang else kv.value new_kv = KeyValue(digest=new_digest, language=kv.language, edited=kv.edited, fuzzy=True, value=new_value) new_kv.save()
def _pre_save(sender, instance, **kwargs): setattr(instance, 'datatrans_old_language', get_current_language()) default_lang = get_default_language() translation.activate(default_lang) # When we edit a registered model, update the original translations and mark them as unedited (to do) if instance.pk is not None: try: # Just because instance.pk is set, it does not mean that the instance # is saved. Most typical/important ex: loading fixtures original = sender.objects.get(pk=instance.pk) except ObjectDoesNotExist: return None register = get_registry() fields = register[sender].values() for field in fields: old_digest = make_digest(original.__dict__[field.name]) new_digest = make_digest(instance.__dict__[field.name]) # If changed, update keyvalues if old_digest != new_digest: # Check if the new value already exists, if not, create a new one. The old one will be obsoleted. if KeyValue.objects.filter(digest=new_digest).count() == 0: kvs = KeyValue.objects.filter(digest=old_digest) for kv in kvs: new_value = instance.__dict__[field.name] if kv.language == default_lang else kv.value new_kv = KeyValue(digest=new_digest, language=kv.language, edited=kv.edited, fuzzy=True, value=new_value) new_kv.save()
def _pre_save(sender, instance, **kwargs): setattr(instance, 'datatrans_old_language', get_current_language()) default_lang = get_default_language() translation.activate(default_lang) # When we edit a registered model, update the original translations and mark them as unedited (to do) if instance.pk is not None: try: # Just because instance.pk is set, it does not mean that the instance # is saved. Most typical/important ex: loading fixtures original = sender.objects.get(pk=instance.pk) except ObjectDoesNotExist: return None ct = ContentType.objects.get_for_model(sender) register = get_registry() fields = register[sender].values() for field in fields: old_digest = make_digest(original.__dict__[field.name] or '') new_digest = make_digest(instance.__dict__[field.name] or '') # If changed, update keyvalues if old_digest != new_digest: # Check if the new value already exists, if not, create a new one. The old one will be obsoleted. old_query = KeyValue.objects.filter(digest=old_digest, content_type__id=ct.id, object_id=original.id, field=field.name) new_query = KeyValue.objects.filter(digest=new_digest, content_type__id=ct.id, object_id=original.id, field=field.name) old_count = old_query.count() new_count = new_query.count() _invalidate_word_count(sender, field, instance) if old_count != new_count or new_count == 0: for kv in old_query: if new_query.filter(language=kv.language).count() > 0: continue new_value = instance.__dict__[ field. name] if kv.language == default_lang else kv.value new_kv = KeyValue(digest=new_digest, content_type_id=ct.id, object_id=original.id, field=field.name, language=kv.language, edited=kv.edited, fuzzy=True, value=new_value) new_kv.save()
def test_cache(self): digest = make_digest(self.message_en) type_id = ContentType.objects.get_for_model(self.instance.__class__).id cache_key = 'datatrans_%s_%s_%s_%s_%s' % (self.nl, digest, type_id, self.instance.id, self.field) self.assertEqual(cache.get(cache_key), None) translation.activate(self.nl) kv = KeyValue.objects.get_keyvalue(self.message_en, self.nl, self.instance, self.field) self.assertEqual(cache.get(cache_key).value, self.message_en) kv.value = self.message_nl kv.save() kv = KeyValue.objects.get_keyvalue(self.message_en, self.nl, self.instance, self.field) self.assertEqual(cache.get(cache_key).value, self.message_nl) kv.value = '%s2' % self.message_nl kv.save() self.assertEqual(cache.get(cache_key).value, '%s2' % self.message_nl) kv.delete() self.assertEqual(cache.get(cache_key), None) translation.deactivate()
def make_messages(build_digest_list=False): ''' This function loops over all the registered models and, when necessary, creates KeyValue entries for the fields specified. When build_digest_list is True, a list of digests will be created for all the translatable data. When it is False, it will return the number of processed objects. ''' object_count = 0 digest_list = [] for model in REGISTRY: fields = REGISTRY[model].values() objects = model.objects.all() for object in objects: for field in fields: for lang_code, lang_human in settings.LANGUAGES: value = object.__dict__[field.name] if build_digest_list: digest_list.append(make_digest(value)) KeyValue.objects.lookup(value, lang_code) object_count += 1 if build_digest_list: return digest_list else: return object_count
def make_messages(build_digest_list=False): """ This function loops over all the registered models and, when necessary, creates KeyValue entries for the fields specified. When build_digest_list is True, a list of digests will be created for all the translatable data. When it is False, it will return the number of processed objects. """ object_count = 0 digest_list = [] for model in REGISTRY: fields = REGISTRY[model].values() objects = model.objects.all() for object in objects: for field in fields: for lang_code, lang_human in settings.LANGUAGES: value = object.__dict__[field.name] if build_digest_list: digest_list.append(make_digest(value)) KeyValue.objects.lookup(value, lang_code, object, field.name) object_count += 1 if build_digest_list: return digest_list else: return object_count
def forwards(self, orm): "Write your forwards methods here." registry = get_registry() counts = defaultdict(lambda: []) for modelclass, fields in registry.items(): ct = ContentType.objects.get_for_model(modelclass) for object in modelclass.objects.all(): for field in fields.keys(): value = object.__dict__[field] counts[value].append((object, field)) digest = make_digest(value) done = {} for kv in KeyValue.objects.filter(digest=digest).all(): if kv.object_id is None: kv.content_object = object kv.field = field kv.save() else: if not kv.language in done: KeyValue.objects.get_or_create( digest=kv.digest, language=kv.language, object_id=object.id, content_type_id=ct.id, field=field, defaults={ 'value': kv.value, 'edited': kv.edited, 'fuzzy': kv.fuzzy, }) done[kv.language] = 1
def forwards(self, orm): "Write your forwards methods here." registry = get_registry() counts = defaultdict(lambda: []) for modelclass, fields in registry.items(): ct = ContentType.objects.get_for_model(modelclass) for object in modelclass.objects.all(): for field in fields.keys(): value = object.__dict__[field] counts[value].append((object, field)) digest = make_digest(value) done = {} for kv in KeyValue.objects.filter(digest=digest).all(): if kv.object_id is None: kv.content_object = object kv.field = field kv.save() else: if not kv.language in done: KeyValue.objects.get_or_create( digest = kv.digest, language = kv.language, object_id = object.id, content_type_id = ct.id, field = field, defaults = { 'value': kv.value, 'edited': kv.edited, 'fuzzy': kv.fuzzy, } ) done[kv.language] = 1
def _pre_save(sender, instance, **kwargs): setattr(instance, 'datatrans_old_language', get_current_language()) default_lang = get_default_language() translation.activate(default_lang) # When we edit a registered model, update the original translations and mark them as unedited (to do) if instance.pk is not None: register = get_registry() fields = register[sender].values() original = sender.objects.get(pk=instance.pk) for field in fields: old_digest = make_digest(original.__dict__[field.name]) new_digest = make_digest(instance.__dict__[field.name]) # If changed, update keyvalues if old_digest != new_digest: # Check if the new value already exists, if not, create a new one. The old one will be obsoleted. if KeyValue.objects.filter(digest=new_digest).count() == 0: kvs = KeyValue.objects.filter(digest=old_digest) for kv in kvs: new_value = instance.__dict__[field.name] if kv.language == default_lang else kv.value new_kv = KeyValue(digest=new_digest, language=kv.language, edited=kv.edited, fuzzy=True, value=new_value) new_kv.save()
def test_cache(self): value_en = 'test_cache_en' value_nl = 'test_cache_nl' digest = make_digest(value_en) cache_key = 'datatrans_%s_%s' % ('nl', digest) self.assertEqual(cache.get(cache_key), None) translation.activate('nl') kv = KeyValue.objects.get_keyvalue(value_en, 'nl') self.assertEqual(cache.get(cache_key).value, value_en) kv.value = value_nl kv.save() kv = KeyValue.objects.get_keyvalue(value_en, 'nl') self.assertEqual(cache.get(cache_key).value, value_nl) kv.value = '%s2' % value_nl kv.save() self.assertEqual(cache.get(cache_key).value, '%s2' % value_nl) kv.delete() self.assertEqual(cache.get(cache_key), None)
def test_cache(self): digest = make_digest(self.name_en) type_id = ContentType.objects.get_for_model(self.instance.__class__).id cache_key = 'datatrans_{}_{}_{}_{}_{}'.format(self.nl, digest, type_id, self.instance.id, self.field) self.assertEqual(cache.get(cache_key), None) translation.activate(self.nl) kv = KeyValue.objects.get_keyvalue(self.name_en, self.nl, self.instance, self.field) self.assertEqual(cache.get(cache_key).value, self.name_en) kv.value = self.name_nl kv.save() kv = KeyValue.objects.get_keyvalue(self.name_en, self.nl, self.instance, self.field) self.assertEqual(cache.get(cache_key).value, self.name_nl) kv.value = '{}2'.format(self.name_nl) kv.save() self.assertEqual(cache.get(cache_key).value, '{}2'.format(self.name_nl)) kv.delete() self.assertEqual(cache.get(cache_key), None)