summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/toaster/toastergui/tablefilter.py
diff options
context:
space:
mode:
authorElliot Smith <elliot.smith@intel.com>2016-01-15 13:00:50 +0200
committerRichard Purdie <richard.purdie@linuxfoundation.org>2016-01-15 16:29:59 +0000
commit809046c6fbd544907b5d5f3bb554b71724c74661 (patch)
tree9a20e81100395ba67934f3e865c6e9356f4e0232 /bitbake/lib/toaster/toastergui/tablefilter.py
parent294579b531d5a96a17aa863554e71f4680d35812 (diff)
downloadpoky-809046c6fbd544907b5d5f3bb554b71724c74661.tar.gz
bitbake: toastergui: refactor ToasterTable filtering
The filter code for ToasterTable was difficult to follow and inflexible (not allowing different types of filter, for example). Refactor to a set of filter classes to make the structure cleaner and provide the flexibility needed for other filter types (e.g. date range filter). [YOCTO #8738] (Bitbake rev: 94031bb30bdaf665d0c8c68b591fcb7a17b6674d) Signed-off-by: Elliot Smith <elliot.smith@intel.com> Signed-off-by: Ed Bartosh <ed.bartosh@linux.intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/toaster/toastergui/tablefilter.py')
-rw-r--r--bitbake/lib/toaster/toastergui/tablefilter.py119
1 files changed, 119 insertions, 0 deletions
diff --git a/bitbake/lib/toaster/toastergui/tablefilter.py b/bitbake/lib/toaster/toastergui/tablefilter.py
new file mode 100644
index 0000000000..b42fd52865
--- /dev/null
+++ b/bitbake/lib/toaster/toastergui/tablefilter.py
@@ -0,0 +1,119 @@
1#
2# ex:ts=4:sw=4:sts=4:et
3# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
4#
5# BitBake Toaster Implementation
6#
7# Copyright (C) 2015 Intel Corporation
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License version 2 as
11# published by the Free Software Foundation.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License along
19# with this program; if not, write to the Free Software Foundation, Inc.,
20# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22class TableFilter(object):
23 """
24 Stores a filter for a named field, and can retrieve the action
25 requested for that filter
26 """
27 def __init__(self, name, title):
28 self.name = name
29 self.title = title
30 self.__filter_action_map = {}
31
32 def add_action(self, action):
33 self.__filter_action_map[action.name] = action
34
35 def get_action(self, action_name):
36 return self.__filter_action_map[action_name]
37
38 def to_json(self, queryset):
39 """
40 Dump all filter actions as an object which can be JSON serialised;
41 this is used to generate the JSON for processing in
42 table.js / filterOpenClicked()
43 """
44 filter_actions = []
45
46 # add the "all" pseudo-filter action, which just selects the whole
47 # queryset
48 filter_actions.append({
49 'action_name' : 'all',
50 'title' : 'All',
51 'type': 'toggle',
52 'count' : queryset.count()
53 })
54
55 # add other filter actions
56 for action_name, filter_action in self.__filter_action_map.iteritems():
57 obj = filter_action.to_json(queryset)
58 obj['action_name'] = action_name
59 filter_actions.append(obj)
60
61 return {
62 'name': self.name,
63 'title': self.title,
64 'filter_actions': filter_actions
65 }
66
67class TableFilterActionToggle(object):
68 """
69 Stores a single filter action which will populate one radio button of
70 a ToasterTable filter popup; this filter can either be on or off and
71 has no other parameters
72 """
73
74 def __init__(self, name, title, queryset_filter):
75 self.name = name
76 self.title = title
77 self.__queryset_filter = queryset_filter
78 self.type = 'toggle'
79
80 def set_params(self, params):
81 """
82 params: (str) a string of extra parameters for the action;
83 the structure of this string depends on the type of action;
84 it's ignored for a toggle filter action, which is just on or off
85 """
86 pass
87
88 def filter(self, queryset):
89 return self.__queryset_filter.filter(queryset)
90
91 def to_json(self, queryset):
92 """ Dump as a JSON object """
93 return {
94 'title': self.title,
95 'type': self.type,
96 'count': self.__queryset_filter.count(queryset)
97 }
98
99class TableFilterMap(object):
100 """
101 Map from field names to Filter objects for those fields
102 """
103 def __init__(self):
104 self.__filters = {}
105
106 def add_filter(self, filter_name, table_filter):
107 """ table_filter is an instance of Filter """
108 self.__filters[filter_name] = table_filter
109
110 def get_filter(self, filter_name):
111 return self.__filters[filter_name]
112
113 def to_json(self, queryset):
114 data = {}
115
116 for filter_name, table_filter in self.__filters.iteritems():
117 data[filter_name] = table_filter.to_json()
118
119 return data