1
2
3
4
5
6
7
8
9
10
11
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# BitBake Toaster Implementation
#
# Copyright (C) 2015 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#
from django.db.models import Q, Max, Min
from django.utils import dateparse, timezone
from datetime import timedelta
class TableFilter(object):
"""
Stores a filter for a named field, and can retrieve the action
requested from the set of actions for that filter;
the order in which actions are added governs the order in which they
are returned in the JSON for the filter
"""
def __init__(self, name, title):
self.name = name
self.title = title
self.__filter_action_map = {}
# retains the ordering of actions
self.__filter_action_keys = []
def add_action(self, action):
self.__filter_action_keys.append(action.name)
self.__filter_action_map[action.name] = action
def get_action(self, action_name):
return self.__filter_action_map[action_name]
def to_json(self, queryset):
"""
Dump all filter actions as an object which can be JSON serialised;
this is used to generate the JSON for processing in
table.js / filterOpenClicked()
"""
filter_actions = []
# add the "all" pseudo-filter action, which just selects the whole
# queryset
filter_actions.append({
'action_name' : 'all',
'title' : 'All',
'type': 'toggle',
'count' : queryset.count()
})
# add other filter actions
for action_name in self.__filter_action_keys:
filter_action = self.__filter_action_map[action_name]
obj = filter_action.to_json(queryset)
obj['action_name'] = action_name
filter_actions.append(obj)
return {
'name': self.name,
'title': self.title,
'filter_actions': filter_actions
}
class TableFilterQueryHelper(object):
def dateStringsToQ(self, field_name, date_from_str, date_to_str):
"""
Convert the date strings from_date_str and to_date_str into a
set of args in the form
{'<field_name>__gte': <date from>, '<field_name>__lte': <date to>}
where date_from and date_to are Django-timezone-aware dates; then
convert that into a Django Q object
Returns the Q object based on those criteria
"""
# one of the values required for the filter is missing, so set
# it to the one which was supplied
if date_from_str == '':
date_from_str = date_to_str
elif date_to_str == '':
date_to_str = date_from_str
date_from_naive = dateparse.parse_datetime(date_from_str + ' 00:00:00')
date_to_naive = dateparse.parse_datetime(date_to_str + ' 23:59:59')
tz = timezone.get_default_timezone()
date_from = timezone.make_aware(date_from_naive, tz)
date_to = timezone.make_aware(date_to_naive, tz)
args = {}
args[field_name + '__gte'] = date_from
args[field_name + '__lte'] = date_to
return Q(**args)
class TableFilterAction(object):
"""
A filter action which displays in the filter popup for a ToasterTable
and uses an associated QuerysetFilter to filter the queryset for that
ToasterTable
"""
def __init__(self, name, title, criteria):
self.name = name
self.title = title
self.criteria = criteria
# set in subclasses
self.type = None
def set_filter_params(self, params):
"""
params: (str) a string of extra parameters for the action;
the structure of this string depends on the type of action;
it's ignored for a toggle filter action, which is just on or off
"""
pass
def filter(self, queryset):
if self.criteria:
return queryset.filter(self.criteria)
else:
return queryset
def to_json(self, queryset):
""" Dump as a JSON object """
return {
'title': self.title,
'type': self.type,
'count': self.filter(queryset).count()
}
class TableFilterActionToggle(TableFilterAction):
"""
A single filter action which will populate one radio button of
a ToasterTable filter popup; this filter can either be on or off and
has no other parameters
"""
def __init__(self, *args):
super(TableFilterActionToggle, self).__init__(*args)
self.type = 'toggle'
class TableFilterActionDay(TableFilterAction):
"""
A filter action which filters according to the named datetime field and a
string representing a day ("today" or "yesterday")
"""
TODAY = 'today'
YESTERDAY = 'yesterday'
def __init__(self, name, title, field, day,
query_helper = TableFilterQueryHelper()):
"""
field: (string) the datetime field to filter by
day: (string) "today" or "yesterday"
"""
super(TableFilterActionDay, self).__init__(name, title, None)
self.type = 'day'
self.field = field
self.day = day
self.query_helper = query_helper
def filter(self, queryset):
"""
Apply the day filtering before returning the queryset;
this is done here as the value of the filter criteria changes
depending on when the filtering is applied
"""
now = timezone.now()
if self.day == self.YESTERDAY:
increment = timedelta(days=1)
wanted_date = now - increment
else:
wanted_date = now
wanted_date_str = wanted_date.strftime('%Y-%m-%d')
self.criteria = self.query_helper.dateStringsToQ(
self.field,
wanted_date_str,
wanted_date_str
)
return queryset.filter(self.criteria)
class TableFilterActionDateRange(TableFilterAction):
"""
A filter action which will filter the queryset by a date range.
The date range can be set via set_params()
"""
def __init__(self, name, title, field,
query_helper = TableFilterQueryHelper()):
"""
field: (string) the field to find the max/min range from in the queryset
"""
super(TableFilterActionDateRange, self).__init__(
name,
title,
None
)
self.type = 'daterange'
self.field = field
self.query_helper = query_helper
def set_filter_params(self, params):
"""
This filter depends on the user selecting some input, so it needs
to have its parameters set before its queryset is filtered
params: (str) a string of extra parameters for the filtering
in the format "2015-12-09,2015-12-11" (from,to); this is passed in the
querystring and used to set the criteria on the QuerysetFilter
associated with this action
"""
# if params are invalid, return immediately, resetting criteria
# on the QuerysetFilter
try:
date_from_str, date_to_str = params.split(',')
except ValueError:
self.criteria = None
return
# one of the values required for the filter is missing, so set
# it to the one which was supplied
self.criteria = self.query_helper.dateStringsToQ(
self.field,
date_from_str,
date_to_str
)
def to_json(self, queryset):
""" Dump as a JSON object """
data = super(TableFilterActionDateRange, self).to_json(queryset)
# additional data about the date range covered by the queryset's
# records, retrieved from its <field> column
data['min'] = queryset.aggregate(Min(self.field))[self.field + '__min']
data['max'] = queryset.aggregate(Max(self.field))[self.field + '__max']
# a range filter has a count of None, as the number of records it
# will select depends on the date range entered and we don't know
# that ahead of time
data['count'] = None
return data
class TableFilterMap(object):
"""
Map from field names to TableFilter objects for those fields
"""
def __init__(self):
self.__filters = {}
def add_filter(self, filter_name, table_filter):
""" table_filter is an instance of Filter """
self.__filters[filter_name] = table_filter
def get_filter(self, filter_name):
return self.__filters[filter_name]
def to_json(self, queryset):
data = {}
for filter_name, table_filter in self.__filters.items():
data[filter_name] = table_filter.to_json()
return data
|