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)
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)
def __get__(self, instance, owner): if instance._block_translations: return self.default_value(instance) curr_lang = get_current_language() if curr_lang == instance._initial_language: return self.default_value(instance) for trans in instance._new_translations: if trans.language == curr_lang: return getattr(trans, self.field_name) # FIME: Probably should use instance._state.adding instead of testing pk == None if instance.pk is not None: for trans in instance._translations_all(): if trans.language == curr_lang: return getattr(trans, self.field_name) return self.default_value(instance)
def __init__(self, *args, **kwargs): """ Probably should be moved to Metaclass """ self.languages = kwargs.pop('languages', get_active_languages()) multi_language_fields = self.multi_language_fields or [] # Walk through all fields and split multilanguage ones field_index = 0 for field_name, field in self.base_fields.items(): if field_name in multi_language_fields: # Create a field copy for each language for lang in self.languages: key = '{field_name}_{lang}'.format(field_name=field_name, lang=lang) new_field = copy(field) new_field.label = self.label_for_field(new_field, lang) self.base_fields.insert(field_index, key, new_field) field_index += 1 del self.base_fields[field_name] else: # For non-multilanguage fields just move them to proper position field = self.base_fields.pop(field_name) # FIXME: Ugly but works for now self.base_fields.insert(field_index, field_name, field) field_index += 1 # Now take care of initial data initial = {} orig_lang = get_current_language() if kwargs.get('instance'): for lang in self.languages: translation.activate(lang) for field_name in multi_language_fields: name = '{name}_{lang}'.format(name=field_name, lang=lang) initial[name] = getattr(kwargs['instance'], field_name) initial.update(kwargs.get('initial', {})) kwargs['initial'] = initial translation.activate(orig_lang) # Restore saved language super(MultilanguageModelForm, self).__init__(*args, **kwargs)
def _post_clean(self): # Default implementation is empty, but for the record... super(MultilanguageModelForm, self)._post_clean() if not self.is_valid(): return orig_lang = get_current_language() # Save language # For all configured languages for lang in self.languages: translation.activate(lang) suffix = "_{0}".format(lang) for lang_field_name in self.changed_data: # FIXME: Walk only through configured fields if lang_field_name.endswith(suffix): field_name = lang_field_name[:-len(suffix)] setattr(self.instance, field_name, self.cleaned_data[lang_field_name]) translation.activate(orig_lang) # Restore saved language # FIXME: Find more efficient way if self._validate_unique: self.validate_unique()