예제 #1
0
파일: model.py 프로젝트: zizou4426/odoo
        def tr(src, ttype):
            # We try to do the same as the _(), but without the frame
            # inspection, since we aready are wrapping an osv function
            # trans_obj = self.get('ir.translation') cannot work yet :(
            ctx = {}
            if not kwargs:
                if args and isinstance(args[-1], dict):
                    ctx = args[-1]
            elif isinstance(kwargs, dict):
                if 'context' in kwargs:
                    ctx = kwargs['context']
                elif 'kwargs' in kwargs and kwargs['kwargs'].get('context'):
                    # http entry points such as call_kw()
                    ctx = kwargs['kwargs'].get('context')
                else:
                    try:
                        from odoo.http import request
                        ctx = request.env.context
                    except Exception:
                        pass

            lang = ctx and ctx.get('lang')
            if not (lang or hasattr(src, '__call__')):
                return src

            # We open a *new* cursor here, one reason is that failed SQL
            # queries (as in IntegrityError) will invalidate the current one.
            with odoo.sql_db.db_connect(dbname).cursor() as cr:
                if ttype == 'sql_constraint':
                    res = translate_sql_constraint(cr, key=key, lang=lang)
                else:
                    res = translate(cr, name=False, source_type=ttype,
                                    lang=lang, source=src)
                return res or src
예제 #2
0
def _as_validation_error(env, exc):
    """ Return the IntegrityError encapsuled in a nice ValidationError """

    unknown = _('Unknown')
    for _name, rclass in env.registry.items():
        if exc.diag.table_name == rclass._table:
            model = rclass
            field = model._fields.get(exc.diag.column_name)
            break
    else:
        model = DotDict({'_name': unknown.lower(), '_description': unknown})
        field = DotDict({'name': unknown.lower(), 'string': unknown})

    if exc.pgcode == errorcodes.NOT_NULL_VIOLATION:
        return ValidationError(_(
            "The operation cannot be completed:\n"
            "- Create/update: a mandatory field is not set.\n"
            "- Delete: another model requires the record being deleted."
            " If possible, archive it instead.\n\n"
            "Model: %(model_name)s (%(model_tech_name)s)\n"
            "Field: %(field_name)s (%(field_tech_name)s)\n",
            model_name=model._description,
            model_tech_name=model._name,
            field_name=field.string,
            field_tech_name=field.name,
        ))

    if exc.pgcode == errorcodes.FOREIGN_KEY_VIOLATION:
        return ValidationError(_(
            "The operation cannot be completed: another model requires "
            "the record being deleted. If possible, archive it instead.\n\n"
            "Model: %(model_name)s (%(model_tech_name)s)\n"
            "Constraint: %(constraint)s\n",
            model_name=model._description,
            model_tech_name=model._name,
            constraint=exc.diag.constraint_name,
        ))

    if exc.diag.constraint_name in env.registry._sql_constraints:
        return ValidationError(_(
            "The operation cannot be completed: %s",
            translate_sql_constraint(env.cr, exc.diag.constraint_name, env.context['lang'])
        ))

    return ValidationError(_("The operation cannot be completed: %s", exc.args[0]))