Пример #1
0
def contribute_translations(cls, rel):
    """
    Contribute translations options to the inner Meta class and set the
    descriptors.

    This get's called from prepare_translatable_model
    """
    opts = cls._meta
    opts.translations_accessor = rel.get_accessor_name()
    if django.VERSION >= (1, 8):
        opts.translations_model = rel.field.model
    else:
        opts.translations_model = rel.model
    opts.translations_cache = '%s_cache' % rel.get_accessor_name()
    trans_opts = opts.translations_model._meta

    # Set descriptors
    ignore_fields = ('pk', 'master', 'master_id',
                     opts.translations_model._meta.pk.name)
    for field in trans_opts.fields:
        if field.name in ignore_fields:
            continue
        if field.name == 'language_code':
            attr = LanguageCodeAttribute(opts)
        else:
            attr = TranslatedAttribute(opts, field.name)
            attname = field.get_attname()
            if attname and attname != field.name:
                setattr(cls, attname, TranslatedAttribute(opts, attname))
        setattr(cls, field.name, attr)
Пример #2
0
    def contribute_translations(cls, rel):
        """
        Contribute translations options to the inner Meta class and set the
        descriptors.
        
        This get's called from TranslatableModelBase.__new__
        """
        opts = cls._meta
        opts.translations_accessor = rel.get_accessor_name()
        opts.translations_model = rel.model
        opts.translations_cache = '%s_cache' % rel.get_accessor_name()
        trans_opts = opts.translations_model._meta

        # Set descriptors
        ignore_fields = [
            'pk',
            'master',
            opts.translations_model._meta.pk.name,
        ]
        for field in trans_opts.fields:
            if field.name in ignore_fields:
                continue
            if field.name == 'language_code':
                attr = LanguageCodeAttribute(opts)
            else:
                attr = TranslatedAttribute(opts, field.name)
            setattr(cls, field.name, attr)
Пример #3
0
def prepare_translatable_model(sender, **kwargs):
    """ Make a model translatable if it inherits TranslatableModel.
        Invoked by Django after it has finished setting up any model.
    """
    model = sender
    if not issubclass(model, TranslatableModel) or model._meta.abstract:
        return

    if model._meta.proxy:
        model._meta.translations_accessor = model._meta.concrete_model._meta.translations_accessor
        model._meta.translations_model = model._meta.concrete_model._meta.translations_model

    if not hasattr(model._meta, 'translations_model'):
        raise ImproperlyConfigured(
            "No TranslatedFields found on %r, subclasses of "
            "TranslatableModel must define TranslatedFields." % model)

    #### Now we have to work ####

    # Create query foreign object
    if model._meta.proxy:
        hvad_query = model._meta.concrete_model._meta.get_field('_hvad_query')
    else:
        hvad_query = SingleTranslationObject(model)
        model.add_to_class('_hvad_query', hvad_query)

    # Set descriptors
    ignore_fields = ('pk', 'master', 'master_id', 'language_code',
                     model._meta.translations_model._meta.pk.name)
    setattr(model, 'language_code', LanguageCodeAttribute(model, hvad_query))
    for field in model._meta.translations_model._meta.fields:
        if field.name in ignore_fields:
            continue
        setattr(model, field.name,
                TranslatedAttribute(model, field.name, hvad_query))
        attname = field.get_attname()
        if attname and attname != field.name:
            setattr(model, attname,
                    TranslatedAttribute(model, attname, hvad_query))

    # Replace get_field_by_name with one that warns for common mistakes
    if not isinstance(model._meta.get_field, SmartGetField):
        model._meta.get_field = MethodType(
            SmartGetField(model._meta.get_field), model._meta)
Пример #4
0
    def contribute_translations(self, model, translations_model, related_name):
        """ Contribute translations model to the Meta class and set descriptors """

        model._meta.translations_accessor = related_name
        model._meta.translations_cache = '%s_cache' % related_name

        # Set descriptors
        ignore_fields = ('pk', 'master', 'master_id', translations_model._meta.pk.name)
        for field in translations_model._meta.fields:
            if field.name in ignore_fields:
                continue
            if field.name == 'language_code':
                attr = LanguageCodeAttribute(model._meta)
            else:
                attr = TranslatedAttribute(model._meta, field.name)
                attname = field.get_attname()
                if attname and attname != field.name:
                    setattr(model, attname, TranslatedAttribute(model._meta, attname))
            setattr(model, field.name, attr)