def disable_invalid_filters(env):
    """It analyzes all the existing active filters to check if they are still
    correct. If not, they are disabled for avoiding errors when clicking on
    them, or worse, if they are default filters when opening the model/action.

    To be run at the base end-migration script for having a general scope. Only
    assured to work on > v8.

    :param env: Environment parameter.
    """
    try:
        from odoo.tools.safe_eval import safe_eval
    except ImportError:
        from openerp.tools.safe_eval import safe_eval
    import time
    try:
        basestring
    except:  # For Python 3 compatibility
        basestring = str

    def format_message(f):
        msg = "FILTER DISABLED: "
        if f.user_id:
            msg += "Filter '%s' for user '%s'" % (f.name, f.user_id.name)
        else:
            msg += "Global Filter '%s'" % f.name
        msg += " for model '%s' has been disabled " % f.model_id
        return msg

    filters = env['ir.filters'].search([('domain', '!=', '[]')])
    for f in filters:
        if f.model_id not in env:
            continue  # Obsolete or invalid model
        model = env[f.model_id]
        columns = (
            getattr(model, '_columns', False) or getattr(model, '_fields')
        )
        # DOMAIN
        try:
            # Strange artifact found in a filter
            domain = f.domain.replace('%%', '%')
            model.search(
                safe_eval(domain, {'time': time, 'uid': env.uid}),
                limit=1,
            )
        except Exception:
            logger.warning(
                format_message(f) + "as it contains an invalid domain."
            )
            f.active = False
            continue
        # CONTEXT GROUP BY
        try:
            context = safe_eval(f.context, {'time': time, 'uid': env.uid})
        except Exception:
            logger.warning(
                format_message(f) + "as it contains an invalid context %s.",
                f.context
            )
            f.active = False
            continue
        keys = ['group_by', 'col_group_by']
        for key in keys:
            if not context.get(key):
                continue
            g = context[key]
            if not g:
                continue
            if isinstance(g, basestring):
                g = [g]
            for field_expr in g:
                field = field_expr.split(':')[0]  # Remove date specifiers
                if not columns.get(field):
                    logger.warning(
                        format_message(f) +
                        "as it contains an invalid %s." % key
                    )
                    f.active = False
                    break
def disable_invalid_filters ( env ) : try : from odoo . tools . safe_eval import safe_eval except ImportError : from openerp . tools . safe_eval import safe_eval import time try : basestring except : basestring = str def format_message ( f ) : msg = "FILTER DISABLED: " if f . user_id : msg += "Filter '%s' for user '%s'" % ( f . name , f . user_id . name ) else : msg += "Global Filter '%s'" % f . name msg += " for model '%s' has been disabled " % f . model_id return msg filters = env [ 'ir.filters' ] . search ( [ ( 'domain' , '!=' , '[]' ) ] ) for f in filters : if f . model_id not in env : continue model = env [ f . model_id ] columns = ( getattr ( model , '_columns' , False ) or getattr ( model , '_fields' ) ) try : domain = f . domain . replace ( '%%' , '%' ) model . search ( safe_eval ( domain , { 'time' : time , 'uid' : env . uid } ) , limit = 1 , ) except Exception : logger . warning ( format_message ( f ) + "as it contains an invalid domain." ) f . active = False continue try : context = safe_eval ( f . context , { 'time' : time , 'uid' : env . uid } ) except Exception : logger . warning ( format_message ( f ) + "as it contains an invalid context %s." , f . context ) f . active = False continue keys = [ 'group_by' , 'col_group_by' ] for key in keys : if not context . get ( key ) : continue g = context [ key ] if not g : continue if isinstance ( g , basestring ) : g = [ g ] for field_expr in g : field = field_expr . split ( ':' ) [ 0 ] if not columns . get ( field ) : logger . warning ( format_message ( f ) + "as it contains an invalid %s." % key ) f . active = False break
It analyzes all the existing active filters to check if they are still
    correct. If not, they are disabled for avoiding errors when clicking on
    them, or worse, if they are default filters when opening the model/action.

    To be run at the base end-migration script for having a general scope. Only
    assured to work on > v8.

    :param env: Environment parameter.