Example #1
0
def get_resource_filter_pills(resource):
    """
    Get filter pills
    We don't want the field group pills - these are handled separately in get_resource_field_groups
    @param resource:
    @return:
    """

    filter_dict = parse_request_filters()

    def get_pill_filters(exclude_field, exclude_value):
        """
        Build filter, using filters which aren't exclude_field=exclude_value
        @param exclude_field:
        @param exclude_value:
        @return:
        """

        filters = []
        for field, values in filter_dict.items():
            for value in values:
                if not (field == exclude_field and value == exclude_value):
                    filters.append('%s:%s' % (field, value))

        return '|'.join(filters)

    pills = {}

    options = resource_filter_options(resource)

    field_labels = {}

    field_groups = resource_view_get_field_groups(resource)

    if field_groups:
        for fields in field_groups.values():
            for field_name, label in fields.items():
                field_labels[field_name] = label

    for field, values in filter_dict.items():
        for value in values:
            filters = get_pill_filters(field, value)

            # If this is the _tmgeom field, we don't want to output the whole value as it's in the format:
            # POLYGON ((-100.45898437499999 41.902277040963696, -100.45898437499999 47.54687159892238, -92.6806640625 47.54687159892238, -92.6806640625 41.902277040963696, -100.45898437499999 41.902277040963696))
            if field == '_tmgeom':
                pills['geometry'] = {'Polygon': filters}
            elif field in options:
                label = options[field]['label']
                try:
                    pills['Options'][label] = filters
                except KeyError:
                    pills['Options'] = {label: filters}
            else:

                try:
                    label = field_labels[field]
                except KeyError:
                    label = field

                try:
                    pills[label][value] = filters
                except KeyError:
                    pills[label] = {value: filters}

    # Remove the field group key, if it exists
    pills.pop(FIELD_DISPLAY_FILTER, None)
    return pills
Example #2
0
def resource_view_get_hidden_fields(resource):
    """
    Get a list of hidden fields
    This is called from resource_view_filters.html and helper resource_view_state
    So the same fields are hidden in the form and Slickgrid

    @param resource id:
    @return: list of hidden fields
    """

    """
    Parse hidden fields from the filter dictionary
    @param filter_dict:
    """

    filter_dict = parse_request_filters()

    # Get all display fields explicitly set
    display_fields = filter_dict.pop(FIELD_DISPLAY_FILTER, [])

    # Load the hidden fields cookie
    hidden_fields_cookie = resource_filter_get_cookie(resource['id'])

    # Retrieve the fields for this resource
    resource_fields = resource_get_ordered_fields(resource['id'])

    # If user has set display fields, loop through display fields
    # And available fields, to build a list of hidden fields
    if display_fields:

        # Ensure it's a list
        if not isinstance(display_fields, list):
            display_fields = [display_fields]

        # Make sure _id is never hidden
        display_fields.append('_id')

        # Make sure gbifIssue is never hidden (if it exists)
        if 'gbifIssue' in resource_fields:
            display_fields.append('gbifIssue')

        # Make sure all filtered fields are never hidden
        display_fields += filter_dict.keys()

        # Hidden fields are all other resource fields not in the display field array
        return list(set(resource_fields) - set(display_fields))

    elif hidden_fields_cookie:

        # Make sure that even if we're using the hidden fields cookie
        # All filtered fields are removed from the hidden field list
        hidden_fields_cookie = list(set(hidden_fields_cookie) - set(filter_dict.keys()))
        return hidden_fields_cookie

    else:

        # User has nto customised the grid - so see if we have custom display
        # fields set in the controller
        view_cls = resource_view_get_view(resource)

        if view_cls.grid_default_columns:
            return [f for f in resource_fields if f not in view_cls.grid_default_columns]

    return {}