def __new__(cls, name, bases, attrs): local_trans_fields, inherited_trans_fields = \ cls.get_trans_fields(name, bases, attrs) if ('Meta' in attrs) and hasattr(attrs['Meta'], 'translate'): delattr(attrs['Meta'], 'translate') attrs = cls.rewrite_trans_fields(local_trans_fields, attrs) attrs = cls.rewrite_unique_together(local_trans_fields, attrs) new_obj = super(MultilingualModelBase, cls).__new__(cls, name, bases, attrs) new_obj._meta.translatable_fields = inherited_trans_fields + local_trans_fields # Add a property that masks the translatable fields for field_name in local_trans_fields: # If there is already a property with the same name, we will leave it # This also happens if the Class is created multiple times # (Django's ModelBase has the ability to detect this and "bail out" but we don't) if type(new_obj.__dict__.get(field_name)) == property: continue # Some fields add a descriptor (ie. FileField), we want to keep that on the model if field_name in new_obj.__dict__: primary_lang_field_name = '%s_%s' % ( field_name, get_normalized_language(settings.LANGUAGES[0][0])) setattr(new_obj, primary_lang_field_name, new_obj.__dict__[field_name]) getter = cls.generate_field_getter(field_name) setter = cls.generate_field_setter(field_name) setattr(new_obj, field_name, property(getter, setter)) return new_obj
def __new__(cls, name, bases, attrs): local_trans_fields, inherited_trans_fields = \ cls.get_trans_fields(name, bases, attrs) if ('Meta' in attrs) and hasattr(attrs['Meta'], 'translate'): delattr(attrs['Meta'], 'translate') attrs = cls.rewrite_trans_fields(local_trans_fields, attrs) attrs = cls.rewrite_unique_together(local_trans_fields, attrs) new_obj = super(MultilingualModelBase, cls).__new__(cls, name, bases, attrs) new_obj._meta.translatable_fields = inherited_trans_fields + local_trans_fields # Add a property that masks the translatable fields for field_name in local_trans_fields: # If there is already a property with the same name, we will leave it # This also happens if the Class is created multiple times # (Django's ModelBase has the ability to detect this and "bail out" but we don't) if type(new_obj.__dict__.get(field_name)) == property: continue # Some fields add a descriptor (ie. FileField), we want to keep that on the model if field_name in new_obj.__dict__: primary_lang_field_name = '%s_%s' % ( field_name, get_normalized_language(settings.LANGUAGES[0][0]) ) setattr(new_obj, primary_lang_field_name, new_obj.__dict__[field_name]) getter = cls.generate_field_getter(field_name) setter = cls.generate_field_setter(field_name) setattr(new_obj, field_name, property(getter, setter)) return new_obj
def _post_clean(self): # We force the language to the primary, temporarily disabling the # routing based on current active language. # This allows all fields to be assigned to the corresponding language old_force_language = self.instance._force_language self.instance._force_language = get_normalized_language(settings.LANGUAGES[0][0]) super(MultilingualModelForm, self)._post_clean() self.instance._force_language = old_force_language
def save(self, *args, **kwargs): # We have to force the primary language before saving or else # our "proxy" property will prevent the primary language values from being returned. old_forced_language = self._force_language self._force_language = get_normalized_language(settings.LANGUAGES[0][0]) super(MultilingualModel, self).save(*args, **kwargs) # Now we can switch back self._force_language = old_forced_language
def save(self, *args, **kwargs): # We have to force the primary language before saving or else # our "proxy" property will prevent the primary language values from being returned. old_forced_language = self._force_language self._force_language = get_normalized_language( settings.LANGUAGES[0][0]) super(MultilingualModel, self).save(*args, **kwargs) # Now we can switch back self._force_language = old_forced_language
def _post_clean(self): # We force the language to the primary, temporarily disabling the # routing based on current active language. # This allows all fields to be assigned to the corresponding language old_force_language = self.instance._force_language self.instance._force_language = get_normalized_language( settings.LANGUAGES[0][0]) super(MultilingualModelForm, self)._post_clean() self.instance._force_language = old_force_language
def __init__(self, data=None, files=None, instance=None, **kwargs): # We force the language to the primary, temporarily disabling the # routing based on current active language. # This allows all field values to be extracted from the model in super's init() # as it populates self.initial) if instance is not None: old_force_language = instance._force_language instance._force_language = get_normalized_language(settings.LANGUAGES[0][0]) else: old_force_language = None super(MultilingualModelForm, self).__init__( data=data, files=files, instance=instance, **kwargs ) self.instance._force_language = old_force_language
def __init__(self, *args, **kwargs): self._force_language = None # Rewrite any keyword arguments for translatable fields language = get_current_language() for field in self._meta.translatable_fields: if field in kwargs.keys(): attrname = get_real_field_name(field, language) if attrname != field: kwargs[attrname] = kwargs[field] del kwargs[field] # We have to force the primary language before initializing or else # our "proxy" property will prevent the primary language values from being returned. self._force_language = get_normalized_language(settings.LANGUAGES[0][0]) super(MultilingualModel, self).__init__(*args, **kwargs) self._force_language = None
def __init__(self, *args, **kwargs): self._force_language = None # Rewrite any keyword arguments for translatable fields language = get_current_language() for field in self._meta.translatable_fields: if field in kwargs.keys(): attrname = get_real_field_name(field, language) if attrname != field: kwargs[attrname] = kwargs[field] del kwargs[field] # We have to force the primary language before initializing or else # our "proxy" property will prevent the primary language values from being returned. self._force_language = get_normalized_language( settings.LANGUAGES[0][0]) super(MultilingualModel, self).__init__(*args, **kwargs) self._force_language = None
def __init__(self, data=None, files=None, instance=None, **kwargs): # We force the language to the primary, temporarily disabling the # routing based on current active language. # This allows all field values to be extracted from the model in super's init() # as it populates self.initial) if instance is not None: old_force_language = instance._force_language instance._force_language = get_normalized_language( settings.LANGUAGES[0][0]) else: old_force_language = None super(MultilingualModelForm, self).__init__(data=data, files=files, instance=instance, **kwargs) self.instance._force_language = old_force_language