Example #1
0
def form_select_datastore_field_options(resource_id=None, allow_empty=False):
    # Need to check for resource_id as this form gets loaded on add, nut just edit
    # And on add there will be no resource_id
    if resource_id:
        datastore_fields = resource_get_ordered_fields(resource_id)
        return list_to_form_options(datastore_fields, allow_empty)

    return []
Example #2
0
def resource_view_get_ordered_fields(resource_id):
    """
    This is a replacement for resource_view_get_fields, but this function
    handles errors internally, and return the fields in their original order
    @param resource_id:
    @return:
    """
    return resource_get_ordered_fields(resource_id)
Example #3
0
    def render_record(self, c):
        """
        Render a record
        """

        # The record_dict does not have fields in the correct order
        # So load the fields, and create an OrderedDict with field: value
        c.field_data = OrderedDict()
        for field in resource_get_ordered_fields(c.resource['id']):
            if not field.startswith('_'):
                c.field_data[field] = c.record_dict.get(field, None)

        return p.toolkit.render('record/view.html')
Example #4
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 {}
Example #5
0
def resource_view_state(resource_view_json, resource_json):
    """
    Alter the recline view resource, adding in state info
    @param resource_view_json:
    @return:
    """
    resource_view = json.loads(resource_view_json)
    resource = json.loads(resource_json)

    # There is an annoying feature/bug in slickgrid, that if fitColumns=True
    # And grid is wider than available viewport, slickgrid columns cannot
    # Be resized until fitColumns is deactivated
    # So to fix, we're going to work out how many columns are in the dataset
    # To decide whether or not to turn on fitColumns
    # Messy, but better than trying to hack around with slickgrid

    resource_fields = resource_get_ordered_fields(resource_view['resource_id'])

    # Add hidden fields
    # The way Slickgrid is implemented, we cannot pass in an array of columns
    # Instead if display fields is set, we will mark all other columns as hidden
    hidden_fields = resource_view_get_hidden_fields(resource)

    num_fields = len(resource_fields) - len(hidden_fields)

    viewport_max_width = 920
    col_width = 100
    fit_columns = (num_fields * col_width) < viewport_max_width

    # Initiate the resource view
    view = resource_view_get_view(resource)

    # And get the state
    resource_view['state'] = view.get_slickgrid_state()

    # TODO: This can be merged into get_slickgrid_state
    resource_view['state']['fitColumns'] = fit_columns

    # ID and DQI always first
    resource_view['state']['columnsOrder'] = ["_id"]

    if 'gbifIssue' in resource_fields:
        resource_view['state']['columnsOrder'].append('gbifIssue')

    for field in resource_fields:
        if field not in hidden_fields and field not in resource_view['state']['columnsOrder']:
            # Add field to the ordered columns
            resource_view['state']['columnsOrder'].append(field)

    for group, fields in resource_view_get_field_groups(resource).items():

        for field, label in fields.items():

            if field in resource_fields and field not in hidden_fields:

                # If we have a group, add a tooltip
                # Otherwise will use title text
                if group:
                    resource_view['state']['columnsToolTip'].append(
                        {
                            'column': field,
                            'value': '%s: %s' % (group, label)
                        }
                    )

                # Add custom titles
                resource_view['state']['columnsTitle'].append(
                    {
                        'column': field,
                        'title': label
                    }
                )

    if view.grid_column_widths:
        for column, width in view.grid_column_widths.items():
            resource_view['state']['columnsWidth'].append(
                {
                    'column': column,
                    'width': width
                }
            )

    if hidden_fields:
        resource_filter_set_cookie(resource_view['resource_id'], hidden_fields)
        resource_view['state']['hiddenColumns'] = hidden_fields
    else:
        resource_filter_delete_cookie(resource_view['resource_id'])

    return json.dumps(resource_view)