Changes between Initial Version and Version 1 of S3/DefaultFilters


Ignore:
Timestamp:
05/12/14 11:36:52 (8 years ago)
Author:
Fran Boon
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • S3/DefaultFilters

    v1 v1  
     1= Default Filters =
     2
     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.
     4
     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.
     6
     7This can be achieved using Default Filters.
     8
     9Here is an example, used within a template, to filter staff to just those for the user's organisation/branch:
     10{{{
     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    """
     17
     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 {}
     25
     26def customise_hrm_human_resource_controller(**attr):
     27
     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")
     33
     34    return attr
     35
     36settings.customise_hrm_human_resource_controller = customise_hrm_human_resource_controller
     37}}}