• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NaturalHistoryMuseum / ckanext-graph / #119

31 Oct 2024 10:37AM UTC coverage: 9.607%. Remained the same
#119

push

coveralls-python

web-flow
merge: #31 from ginger/standardise

0 of 5 new or added lines in 3 files covered. (0.0%)

11 existing lines in 3 files now uncovered.

22 of 229 relevant lines covered (9.61%)

0.1 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/ckanext/graph/plugin.py
1
#!/usr/bin/env python
2
# encoding: utf-8
3
#
4
# This file is part of ckanext-graph
5
# Created by the Natural History Museum in London, UK
6

7
import logging
×
8

NEW
9
from ckan.plugins import SingletonPlugin, implements, interfaces, toolkit
×
10

NEW
11
import ckanext.datastore.interfaces as datastore_interfaces
×
12
from ckanext.graph.db import Query
×
13
from ckanext.graph.lib import utils
×
14
from ckanext.graph.logic.validators import in_list, is_boolean, is_date_castable
×
15

16
not_empty = toolkit.get_validator('not_empty')
×
17
ignore_empty = toolkit.get_validator('ignore_empty')
×
18

19
log = logging.getLogger(__name__)
×
20

21
DATE_INTERVALS = ['minute', 'hour', 'day', 'month', 'year']
×
22

23
TEMPORAL_FIELD_TYPES = ['date']
×
24

25

26
class GraphPlugin(SingletonPlugin):
×
27
    """
28
    Graph plugin.
29
    """
30

31
    implements(interfaces.IConfigurer)
×
32
    implements(interfaces.IResourceView, inherit=True)
×
33
    implements(datastore_interfaces.IDatastore, inherit=True)
×
34
    datastore_field_names = []
×
35

36
    ## IConfigurer
37
    def update_config(self, config):
×
38
        """
39
        Add our template directories to the list of available templates.
40

41
        :param config:
42
        """
43
        toolkit.add_template_directory(config, 'theme/templates')
×
44
        toolkit.add_resource('theme/assets', 'ckanext-graph')
×
45

46
    ## IResourceView
47
    def info(self):
×
UNCOV
48
        return {
×
49
            'name': 'graph',
50
            'title': 'Graph',
51
            'schema': {
52
                'show_date': [is_boolean],
53
                'date_field': [
54
                    ignore_empty,
55
                    is_date_castable,
56
                    in_list(self.datastore_field_names),
57
                ],
58
                'date_interval': [not_empty, in_list(DATE_INTERVALS)],
59
                'show_count': [is_boolean],
60
                'count_field': [ignore_empty, in_list(self.datastore_field_names)],
61
                'count_label': [],
62
            },
63
            'icon': 'bar-chart',
64
            'iframed': False,
65
            'filterable': True,
66
            'preview_enabled': False,
67
            'full_page_edit': False,
68
        }
69

70
    # IDatastore
71
    def datastore_search(self, context, data_dict, all_field_ids, query_dict):
×
UNCOV
72
        return query_dict
×
73

74
    def datastore_validate(self, context, data_dict, all_field_ids):
×
UNCOV
75
        return data_dict
×
76

77
    def view_template(self, context, data_dict):
×
UNCOV
78
        return 'graph/view.html'
×
79

80
    def form_template(self, context, data_dict):
×
UNCOV
81
        return 'graph/form.html'
×
82

83
    def can_view(self, data_dict):
×
84
        """
85
        Specify which resources can be viewed by this plugin.
86

87
        :param data_dict:
88
        """
89
        # Check that we have a datastore for this resource
90
        if data_dict['resource'].get('datastore_active'):
×
91
            return True
×
92
        return False
×
93

94
    def setup_template_variables(self, context, data_dict):
×
95
        """
96
        Setup variables available to templates.
97

98
        :param context:
99
        :param data_dict:
100
        """
101

102
        datastore_fields = utils.get_datastore_field_types()
×
103
        self.datastore_field_names = datastore_fields.keys()
×
104

105
        dropdown_options_count = [
×
106
            {'value': field_name, 'text': field_name}
107
            for field_name, field_type in datastore_fields.items()
108
        ]
109

110
        dropdown_options_date = [
×
111
            {'value': field_name, 'text': field_name}
112
            for field_name, field_type in datastore_fields.items()
113
            if field_type in TEMPORAL_FIELD_TYPES
114
            or 'date' in field_name.lower()
115
            or 'time' in field_name.lower()
116
        ]
117

118
        vars = {
×
119
            'count_field_options': [None]
120
            + sorted(dropdown_options_count, key=lambda x: x['text']),
121
            'date_field_options': [None]
122
            + sorted(dropdown_options_date, key=lambda x: x['text']),
123
            'date_interval_options': [
124
                {'value': interval, 'text': interval} for interval in DATE_INTERVALS
125
            ],
126
            'defaults': {},
127
            'graphs': [],
128
            'resource': data_dict['resource'],
129
        }
130

131
        if data_dict['resource_view'].get('show_count', None) and data_dict[
×
132
            'resource_view'
133
        ].get('count_field', None):
UNCOV
134
            count_field = data_dict['resource_view'].get('count_field')
×
135

136
            count_query = Query.new(count_field=count_field)
×
137

138
            records = count_query.run()
×
139

140
            if records:
×
141
                count_dict = {
×
142
                    'title': data_dict['resource_view'].get('count_label', None)
143
                    or count_field,
144
                    'data': [],
145
                    'options': {
146
                        'bars': {'show': True, 'barWidth': 0.6, 'align': 'center'},
147
                        'xaxis': {
148
                            'ticks': [],
149
                            'rotateTicks': 60,
150
                        },
151
                    },
152
                }
153

154
                for i, record in enumerate(records):
×
155
                    key, count = record
×
156
                    count_dict['data'].append([i, count])
×
157
                    count_dict['options']['xaxis']['ticks'].append([i, key.title()])
×
158

159
                vars['graphs'].append(count_dict)
×
160

161
        # Do we want a date statistics graph
162
        if (
×
163
            data_dict['resource_view'].get('show_date', False)
164
            and data_dict['resource_view'].get('date_field', None) is not None
165
        ):
UNCOV
166
            date_interval = data_dict['resource_view'].get('date_interval')
×
167
            date_field = data_dict['resource_view'].get('date_field')
×
168

169
            date_query = Query.new(date_field=date_field, date_interval=date_interval)
×
170

171
            records = date_query.run()
×
172

173
            if records:
×
UNCOV
174
                default_options = {
×
175
                    'grid': {'hoverable': True, 'clickable': True},
176
                    'xaxis': {'mode': 'time'},
177
                    'yaxis': {'tickDecimals': 0},
178
                }
179

180
                total_dict = {
×
181
                    'title': 'Total records',
182
                    'data': [],
183
                    'options': {
184
                        'series': {'lines': {'show': True}, 'points': {'show': True}},
185
                        '_date_interval': date_interval,
186
                    },
187
                }
188

189
                count_dict = {
×
190
                    'title': 'Per %s' % date_interval,
191
                    'data': [],
192
                    'options': {
193
                        'series': {
194
                            'bars': {'show': True, 'barWidth': 0.6, 'align': 'center'}
195
                        }
196
                    },
197
                }
198

199
                total_dict['options'].update(default_options)
×
200
                count_dict['options'].update(default_options)
×
201

202
                total = 0
×
203

204
                for record in records:
×
205
                    # Convert to string, and then parse as dates
206
                    # This works for all date and string fields
207
                    timestamp, count = record
×
208
                    total += count
×
209
                    total_dict['data'].append([timestamp, total])
×
210
                    count_dict['data'].append([timestamp, count])
×
211

212
                vars['graphs'].append(total_dict)
×
213
                vars['graphs'].append(count_dict)
×
214

215
        return vars
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc