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']}
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_)
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)
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)
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)
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
def __set__(self, obj, values): if get_source(obj): return return super(SharedAssociationProxy, self).__set__(obj, values)
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
def test_get_source_none(root, db_session): from kotti_multilingual.api import get_source assert get_source(root) is None