Exemplo n.º 1
0
    def __set__(self, instance, value):
        """Refuse to set the attribute if we're a translation target.
        """
        if get_source(instance):
            raise TypeError(
                "Can't set %r attribute on translation." % self.key)

        return super(SharedInstrumentedAttribute, self).__set__(
            instance, value)
def test_language_root_autolink(root, ml_events, config, db_session):
    from kotti_multilingual.resources import LanguageRoot
    from kotti_multilingual.api import get_source
    from kotti_multilingual.api import get_translations

    root['sl'] = LanguageRoot(language=u'sl')
    db_session.flush()
    root['en'] = LanguageRoot(language=u'en')
    db_session.flush()

    assert get_translations(root['sl']) == {'en': root['en']}
    assert get_source(root['en']) == root['sl']

    # Add another language root.  This one should also connect to 'sl'
    # automatically.
    root['de'] = LanguageRoot(language=u'de')

    assert get_source(root['de']) == root['sl']
    assert get_translations(root['de']) == {'en': root['en'], 'sl': root['sl']}
Exemplo n.º 3
0
 def __get__(self, obj, class_):
     source = get_source(obj)
     if source is not None:
         if not self.scalar:
             return [getattr(item, self.value_attr) for item in
                     getattr(source, self.target_collection)]
         else:
             return getattr(
                 getattr(source, self.target_collection),
                 self.value_attr
                 )
     return super(SharedAssociationProxy, self).__get__(obj, class_)
Exemplo n.º 4
0
    def __set__(self, instance, value):
        """Refuse to set the attribute if we're a translation target.
        """
        if get_source(instance):
            # just don't save the field, experienced a random
            # type error on edit with data fields like files
            return
            # raise TypeError(
            #     "Can't set %r attribute on translation." % self.key)

        return super(SharedInstrumentedAttribute, self).__set__(
            instance, value)
Exemplo n.º 5
0
def autolink_language_root(event):
    context = event.object

    lr = DBSession.query(LanguageRoot).first()
    if lr is None:
        return

    source = api.get_source(lr)
    if source is None:
        source = lr

    api.link_translation(source, context)
Exemplo n.º 6
0
    def __get__(self, instance, owner):
        """If we're a translation target, look up the attribute in the
        translation source instead of here.
        """
        if instance is None:
            return self

        source = get_source(instance)
        if source is not None:
            return getattr(source, self.key)

        return super(SharedInstrumentedAttribute, self).__get__(
            instance, owner)
Exemplo n.º 7
0
 def deferred_widget(node, kw):
     widget = widget_class(*args, **kwargs)
     # We assume by default that we are on an edit form. So
     # if the context is a translation, it will be returned
     # in readonly mode.
     # If we are on an addform, the widget will be returned
     # as usual.
     addform = kw.get('addform', False)
     if not addform:
         # Edit form
         request = kw['request']
         context = request.context
         if get_source(context) is not None:
             # This is a translation, so let's switch to
             # readonly mode
             widget.readonly = True
     return widget
Exemplo n.º 8
0
 def __set__(self, obj, values):
     if get_source(obj):
         return
     return super(SharedAssociationProxy, self).__set__(obj, values)
Exemplo n.º 9
0
def test_get_source(translated_docs, db_session):
    from kotti_multilingual.api import get_source

    source, target = translated_docs
    assert get_source(target) is source
Exemplo n.º 10
0
def test_get_source_none(root, db_session):
    from kotti_multilingual.api import get_source

    assert get_source(root) is None