Пример #1
0
def update_listener(mapper, connection, target):
    modified = Session.object_session(target).is_modified(target, include_collections=False)
    if modified:
        translations = {}
        insp = inspect(target)
        db = kernel.db.Database()
        for t in get_translations(target, db):
            if not t.lng_code in translations: translations[t.lng_code] = {}
            translations[t.lng_code][t.trl_code] = t
        default_lang = get_default_language().lng_code
        trans_lang = request.forms.get('translation_language')
        if not trans_lang: trans_lang = get_translation_language().lng_code
        if not trans_lang in translations: translations[trans_lang] = {}

        for f in target.translatable_fields:
            if getattr(target, f) is not None and getattr(target, f) != 'None' and modified:
                attr_state = insp.attrs[f]
                code = get_entity_code(target, f)
                if code in translations[trans_lang]:
                    translation = translations[trans_lang][code]
                else:
                    translation = Translation()
                    translation.trl_code = code
                    translation.lng_code = trans_lang
                translation.trl_text = str(getattr(target, f))
                db.add(translation)

                if not hasattr(target, '__translation__'): setattr(target, '__translation__', {})
                if not trans_lang in target.__translation__: target.__translation__[trans_lang] = {}
                target.__translation__[trans_lang][f] = getattr(target, f)
                if trans_lang != default_lang and len(attr_state.history.deleted) > 0:
                    setattr(target, f, attr_state.history.deleted[0])
        db.commit()
        db.close()
Пример #2
0
def after_insert_listener(mapper, connection, target):
    lang = request.forms.get('translation_language')
    if not lang: lang = get_translation_language().lng_code
    db = request.db()
    for c in target.__table__.columns:
        if c.name in target.translatable_fields:
            translation = Translation()
            translation.trl_code = get_entity_code(target, c.name)
            translation.lng_code = lang
            translation.trl_text = getattr(target, c.name)
            db.add(translation)
    db.commit()