'Add default filters to resource, to be called a multi-record
view with a filter form is rendered the first time and before
the view elements get processed
@param request: the request
@param resource: the resource
@return: dict with default filters (URL vars)'

@staticmethod
def apply_filter_defaults(request, resource):

    s3 = current.response.s3
    get_vars = request.get_vars
    tablename = resource.tablename
    default_filters = {}
    filter_defaults = s3
    for level in ('filter_defaults', tablename):
        if (level not in filter_defaults):
            filter_defaults = None
            break
        filter_defaults = filter_defaults[level]
    filter_widgets = resource.get_config('filter_widgets')
    for filter_widget in filter_widgets:
        if filter_widget.opts.hidden:
            continue
        has_default = False
        if ('default' in filter_widget.opts):
            has_default = True
        elif (filter_defaults is None):
            continue
        defaults = set()
        variable = filter_widget.variable(resource, get_vars)
        multiple = (type(variable) is list)
        if multiple:
            for k in variable:
                values = filter_widget._values(get_vars, k)
                if values:
                    filter_widget.values[k] = values
                else:
                    defaults.add(k)
        else:
            values = filter_widget._values(get_vars, variable)
            if values:
                filter_widget.values[variable] = values
            else:
                defaults.add(variable)
        if has_default:
            widget_default = filter_widget.opts['default']
            if (not isinstance(widget_default, dict)):
                if multiple:
                    widget_default = dict(((k, widget_default) for k in variable))
                else:
                    widget_default = {variable: widget_default}
            for k in widget_default:
                if (k not in filter_widget.values):
                    defaults.add(k)
        else:
            widget_default = {}
        for variable in defaults:
            if ('__' in variable):
                (selector, operator) = variable.split('__', 1)
            else:
                (selector, operator) = (variable, None)
            if (filter_defaults and (selector in filter_defaults)):
                applicable_defaults = filter_defaults[selector]
            elif (variable in widget_default):
                applicable_defaults = widget_default[variable]
            else:
                continue
            if callable(applicable_defaults):
                applicable_defaults = applicable_defaults(selector, tablename=tablename)
            if isinstance(applicable_defaults, dict):
                if (operator in applicable_defaults):
                    default = applicable_defaults[operator]
                else:
                    continue
            elif (operator in (None, 'belongs', 'eq')):
                default = applicable_defaults
            else:
                continue
            if (default is None):
                continue
            elif (not isinstance(default, list)):
                default = [default]
            filter_widget.values[variable] = [(str(v) if (v is None) else v) for v in default]
            default_filters[variable] = ','.join((s3_unicode(v) for v in default))
        queries = S3URLQuery.parse(resource, default_filters)
        add_filter = resource.add_filter
        for alias in queries:
            for q in queries[alias]:
                add_filter(q)
    return default_filters
