Ejemplo n.º 1
0
    def __set__(self, instance, value):        
        curr_lang = get_current_language()
        default_lang = get_default_language()

        if not hasattr(instance, '_new_translations'):
            instance._new_translations = []

        if not instance._initialized() and instance.pk is not None:
            instance._initial_language = default_lang
            instance.__dict__[self.field_name] = value
            return

        if not hasattr(instance, '_initial_language'):
            if instance.pk is None:
                instance._initial_language = get_current_language()
            else:
                instance._initial_language = get_default_language()
                instance.__dict__[self.field_name] = value

        if curr_lang == default_lang:
            instance._initial_language = curr_lang

        if curr_lang == instance._initial_language:
            instance.__dict__[self.field_name] = value

        for trans in instance._new_translations:
            if trans.language == curr_lang:
                setattr(trans, self.field_name, value)
                trans.dirty = True
                return

        # FIME: Probably should use instance._state.adding instead of testing pk == None
        if instance.pk is not None and instance._initialized():
            for trans in instance._translations_all():
                if trans.language == curr_lang:
                    setattr(trans, self.field_name, value)
                    trans.dirty = True
                    instance._new_translations.append(trans)
                    return

        # Create translation if not found
        new_translation = instance._new_translation()
        setattr(new_translation, self.field_name, value)
        new_translation.language = curr_lang
        instance._new_translations.append(new_translation)
Ejemplo n.º 2
0
 def _filter_or_exclude(self, negate, *args, **kwargs):
     
     if get_default_language() != get_current_language():
         has_translated_fields = False   
         new_kwargs = {}
         for field in self.model.translated_fields:
             query_field = kwargs.pop(field, None)
             if query_field:
                 has_translated_fields = True
                 new_kwargs['translations__{0}'.format(field)] = query_field
         kwargs.update(new_kwargs)
         
         # Only add language, if translated fields are queried 
         if has_translated_fields and 'translations__language' not in kwargs.keys():
             kwargs['translations__language'] = get_current_language()
     
     return super(TranslatedQuerySet, self)._filter_or_exclude(negate, *args, **kwargs)
Ejemplo n.º 3
0
def is_trans_uniq(trans, uniq):
    errors = {}
    lookup_kwargs = {}
    for field_name in uniq:
        field_value = getattr(trans, field_name)
        if field_value is not None:
            lookup_kwargs[field_name] = field_value 
    qs = trans.__class__._default_manager.filter(**lookup_kwargs)
    if not trans._state.adding and trans.pk is not None:
        qs = qs.exclude(pk=trans.pk)
    
    if qs.exists():
        if len(uniq) == 1:
            key = TranslationErrorKey(uniq[0])
            key.lang = get_default_language()
        else:
            key = TranslationErrorKey(uniq[1])
            key.lang = trans.language
        err = _(u"%(field_name)s is not unique within this translation (%(lang)s)") \
            % {'field_name': capfirst(unicode(field_name)), 'lang': key.lang}
        err = TranslationErrorValue(err)
        err.lang = key.lang
        errors.setdefault(key, []).append(err)
    return errors
Ejemplo n.º 4
0
 def __init__(self, message, code=None, params=None, lang=None):
     if lang is None:
         lang = get_default_language()
     self.lang = lang
     super(TransValidationError, self).__init__(message, code, params)