Skip to content

Default

DefaultView

Bases: object

A view object, used to define custom views for records and grid view.

Source code in ckanext/nhm/views/default.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
class DefaultView(object):
    """
    A view object, used to define custom views for records and grid view.
    """

    resource_id = None

    format = None

    field_groups = {}
    field_facets = []

    filter_options = []

    # Default columns to show in grid
    grid_default_columns = []

    # Specific column widths
    grid_column_widths = {}

    # Default state
    state = {
        'gridOptions': {
            'defaultFormatter': 'NHMFormatter',
            'enableCellRangeSelection': False,
            'enableTextSelectionOnCells': False,
            'enableCellNavigation': False,
            'enableColumnReorder': False,
            'defaultColumnWidth': 70,
        },
        'columnsWidth': [
            {'column': '_id', 'width': 45},
        ],
        'columnsTitle': [
            {
                'column': '_id',
                'title': '',
                # This is just converted into a link so lets hide the title
            }
        ],
        'columnsToolTip': [],
    }

    @staticmethod
    def get_ordered_fields(resource_id):
        """
        Get fields ordered the same as the uploaded dataset.

        :param resource_id:
        """
        data = {'resource_id': resource_id, 'limit': 0}
        try:
            result = toolkit.get_action('datastore_search')({}, data)
        except toolkit.ObjectNotFound:
            return []
        else:
            return [f['id'] for f in result['fields']]

    def render_record(self, c):
        """
        Render a record.

        :param c:
        """

        # The record_dict does not have fields in the correct order
        # So load the fields, and create an OrderedDict with field: value
        toolkit.c.field_data = OrderedDict()

        for field in self.get_ordered_fields(toolkit.c.resource['id']):
            if not field.startswith('_'):
                toolkit.c.field_data[field] = toolkit.c.record_dict.get(field, None)

        return toolkit.render('record/view.html')

    def get_field_groups(self, resource):
        """
        Return the field groups.
        """
        return self.field_groups

    def get_slickgrid_state(self):
        """
        Return the state of the slickgrid.
        """
        return self.state

get_field_groups(resource)

Return the field groups.

Source code in ckanext/nhm/views/default.py
87
88
89
90
91
def get_field_groups(self, resource):
    """
    Return the field groups.
    """
    return self.field_groups

get_ordered_fields(resource_id) staticmethod

Get fields ordered the same as the uploaded dataset.

Parameters:

Name Type Description Default
resource_id
required
Source code in ckanext/nhm/views/default.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
@staticmethod
def get_ordered_fields(resource_id):
    """
    Get fields ordered the same as the uploaded dataset.

    :param resource_id:
    """
    data = {'resource_id': resource_id, 'limit': 0}
    try:
        result = toolkit.get_action('datastore_search')({}, data)
    except toolkit.ObjectNotFound:
        return []
    else:
        return [f['id'] for f in result['fields']]

get_slickgrid_state()

Return the state of the slickgrid.

Source code in ckanext/nhm/views/default.py
93
94
95
96
97
def get_slickgrid_state(self):
    """
    Return the state of the slickgrid.
    """
    return self.state

render_record(c)

Render a record.

Parameters:

Name Type Description Default
c
required
Source code in ckanext/nhm/views/default.py
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
def render_record(self, c):
    """
    Render a record.

    :param c:
    """

    # The record_dict does not have fields in the correct order
    # So load the fields, and create an OrderedDict with field: value
    toolkit.c.field_data = OrderedDict()

    for field in self.get_ordered_fields(toolkit.c.resource['id']):
        if not field.startswith('_'):
            toolkit.c.field_data[field] = toolkit.c.record_dict.get(field, None)

    return toolkit.render('record/view.html')