Esempio n. 1
0
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()
Esempio n. 2
0
File: utils.py Progetto: umdsp/romeu
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()
Esempio n. 3
0
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()
Esempio n. 4
0
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()
Esempio n. 5
0
def translate(request, model, pk, app_label=''):
    languages = getattr(settings, 'LANGUAGES', [])
    if len(languages) < 2:
        raise Http404(u'No multiple languages in setting.')
    user = request.user
    context = {}
    action_url = '/datatrans/translate/%s/%s/' % (model, pk)
    content_type = ContentType.objects.get(model=model)
    model_class = content_type.model_class()
    registry = utils.get_registry()
    if not model_class in registry:
        raise Http404(u'No registered model found for given query.')
    translatable_fields = registry[model_class]
    object = model_class.objects.get(pk=pk)
    can_translate_method = getattr(object, 'can_translate', None)
    assert not can_translate_method or object.can_translate(request)
    model_entry = translate_map[model]
    url_pattern = model_entry[0]
    url_field = model_entry[1]
    object_url = url_pattern % getattr(object, url_field)
    title_field = model_entry[2]
    object_title = getattr(object, title_field)
    edit_form_path = model_entry[3]
    components = edit_form_path.split('.')
    module = __import__(components[0])
    for component in components[1:]:
        module = getattr(module, component)
    edit_form_class = module
    edit_form = edit_form_class()
    translatable_fields = [
        field_name for field_name in edit_form.fields
        if field_name in translatable_fields
    ]
    if request.method == 'POST':
        post_dict = request.POST
        for key, value in post_dict.items():
            if key.startswith('csrf') or key.startswith('save'):
                continue
            if key.count('_'):
                splitted = key.split('_')
                language_code = splitted[-1]
                field_name = '_'.join(splitted[:-1])
                new_value = value
                keyvalues = KeyValue.objects.filter(
                    content_type=content_type,
                    object_id=int(pk),
                    field=field_name,
                    language=language_code).order_by('-updated')
                if keyvalues:
                    keyvalue = keyvalues[0]
                    value = keyvalue.value
                    if new_value:
                        keyvalue.value = new_value
                        keyvalue.edited = True
                        keyvalue.fuzzy = False
                        keyvalue.user = user
                        keyvalue.save()
                        if keyvalues.count() > 1:
                            for keyvalue in keyvalues[1:]:
                                keyvalue.delete()
                    else:
                        for keyvalue in keyvalues:
                            keyvalue.delete()
                else:
                    if new_value:
                        keyvalue = KeyValue(content_type=content_type,
                                            object_id=int(pk),
                                            field=field_name,
                                            language=language_code,
                                            value=new_value,
                                            updated=True,
                                            fuzzy=False)
                        keyvalue.user = user
                        keyvalue.save()
            else:
                raise Http404(u'?')
    original_language = object.original_language
    if original_language:
        try:
            user_languages = user.get_languages()
            languages = [
                l for l in languages
                if l[0] == original_language or l[0] in user_languages
            ]
        except:
            pass
    current_language = get_current_language()
    context['current_language_code'] = current_language
    context['current_language_name'] = dict(settings.LANGUAGES).get(
        current_language, _('unknown'))
    context['object'] = object
    context['original_language'] = original_language
    context['model'] = model
    context['model_title'] = model_class._meta.verbose_name
    context['object_title'] = object_title
    context['object_url'] = object_url
    if original_language:
        form_sets = []
        for field_name in translatable_fields:
            field_forms = []
            form_field = edit_form.fields[field_name]
            for language in languages:
                initial = {}
                language_code = language[0]
                translation_field_name = '%s_%s' % (field_name, language_code)
                if language_code == original_language:
                    value = object.__dict__[field_name]
                else:
                    keyvalues = KeyValue.objects.filter(
                        content_type=content_type,
                        object_id=int(pk),
                        field=field_name,
                        language=language_code).order_by('-updated')
                    if keyvalues:
                        keyvalue = keyvalues[0]
                        value = keyvalue.value
                    else:
                        value = None
                initial[translation_field_name] = value
                field_form = ModelTranslationForm(
                    form_field=form_field,
                    translation_field_name=translation_field_name,
                    initial=initial)
                field_forms.append([language_code, field_form])
            form_sets.append(field_forms)
        context['form_sets'] = form_sets
        context['action_url'] = action_url
    return render(request, 'datatrans/translate.html', context)