Changes between Initial Version and Version 1 of S3/DefaultFilters

05/12/14 11:36:52 (8 years ago)
Fran Boon



  • S3/DefaultFilters

    v1 v1  
     1= Default Filters =
     3There 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.
     5In these cases, it is useful to provide [wiki:S3/FilterForms 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.
     7This can be achieved using Default Filters.
     9Here is an example, used within a template, to filter staff to just those for the user's organisation/branch:
     11def user_org_default_filter(selector, tablename=None):
     12    """
     13        Default filter for organisation_id:
     14        * Use the user's organisation if logged-in and associated with an
     15          organisation.
     16    """
     18    auth = current.auth
     19    user_org_id = auth.is_logged_in() and auth.user.organisation_id
     20    if user_org_id:
     21        return user_org_id
     22    else:
     23        # no default
     24        return {}
     26def customise_hrm_human_resource_controller(**attr):
     28    # Default Filter
     29    from s3 import s3_set_default_filter
     30    s3_set_default_filter("~.organisation_id",
     31                          user_org_default_filter,
     32                          tablename = "hrm_human_resource")
     34    return attr
     36settings.customise_hrm_human_resource_controller = customise_hrm_human_resource_controller