wiki:S3/DefaultFilters

Version 2 (modified by MattS, 10 years ago) ( diff )

--

Default Filters

There are times where a view contains many more records than are typically useful for a user, e.g. they have permissions to view all but are only responsible for a subset.

In these cases, it is useful to provide Filters to allow the user to filter the records to just those that are relevant. However, even better is to have the filters automatically default to the relevant subset, thus allowing the filter widgets to be used only when the, much-rarer, requirement is to un-filter to the full subset.

This can be achieved using Default Filters.

Here is an example, used within a template, to filter staff to just those for the user's organisation/branch:

def user_org_default_filter(selector, tablename=None):
    """
        Default filter for organisation_id:
        * Use the user's organisation if logged-in and associated with an
          organisation.
    """

    auth = current.auth
    user_org_id = auth.is_logged_in() and auth.user.organisation_id
    if user_org_id:
        return user_org_id
    else:
        # no default
        return {}

def customise_hrm_human_resource_controller(**attr):

    # Default Filter
    from s3 import s3_set_default_filter
    s3_set_default_filter("~.organisation_id",
                          user_org_default_filter,
                          tablename = "hrm_human_resource")

    return attr

settings.customise_hrm_human_resource_controller = customise_hrm_human_resource_controller
Note: See TracWiki for help on using the wiki.