Commit d30ccbdb5eb1b280bc15effc00fb18faa1d54cbf
1 parent
71357f92
Exists in
master
and in
8 other branches
Generalized filters to support plugins' filters
Signed-off-by: Gustavo Jaruga <darksshades@gmail.com> Signed-off-by: Matheus Faria <matheus.sousa.faria@gmail.com> Signed-off-by: Matheus Fernandes <matheus.souza.fernandes@gmail.com>
Showing
3 changed files
with
53 additions
and
125 deletions
Show diff stats
@@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
1 | +#!/usr/bin/env python | ||
2 | + | ||
3 | +import importlib | ||
4 | + | ||
5 | +from django.conf import settings | ||
6 | + | ||
7 | +def import_plugin_filters(request): | ||
8 | + plugin_filters = {} | ||
9 | + for app_name in settings.INSTALLED_APPS: | ||
10 | + | ||
11 | + module_name = '{}.filters'.format(app_name) | ||
12 | + try: | ||
13 | + module = importlib.import_module(module_name) | ||
14 | + except ImportError: | ||
15 | + continue | ||
16 | + | ||
17 | + get_filters = getattr(module, 'get_filters', None) | ||
18 | + if get_filters: | ||
19 | + plugin_filters.update(get_filters(request)) | ||
20 | + | ||
21 | + | ||
22 | + return plugin_filters |
colab/search/templates/search/includes/search_filters.html
@@ -112,10 +112,12 @@ | @@ -112,10 +112,12 @@ | ||
112 | 112 | ||
113 | <ul class="unstyled-list"> | 113 | <ul class="unstyled-list"> |
114 | 114 | ||
115 | - <li> | ||
116 | - <span class="glyphicon glyphicon-envelope"></span> | ||
117 | - <a href="{% append_to_get type='thread' %}">{% trans "Discussion" %}</a> | ||
118 | - </li> | 115 | + {% for type, name, icon in filters_options %} |
116 | + <li> | ||
117 | + <span class="glyphicon glyphicon-{{icon}}"></span> | ||
118 | + <a href="{% append_to_get type=type %}">{% trans name %}</a> | ||
119 | + </li> | ||
120 | + {% endfor %} | ||
119 | </ul> | 121 | </ul> |
120 | {% endif %} | 122 | {% endif %} |
121 | <hr /> | 123 | <hr /> |
colab/search/views.py
@@ -4,7 +4,7 @@ from django.conf import settings | @@ -4,7 +4,7 @@ from django.conf import settings | ||
4 | from django.utils.translation import ugettext as _ | 4 | from django.utils.translation import ugettext as _ |
5 | 5 | ||
6 | from haystack.views import SearchView | 6 | from haystack.views import SearchView |
7 | - | 7 | +from colab.plugins.utils import filters_importer |
8 | 8 | ||
9 | class ColabSearchView(SearchView): | 9 | class ColabSearchView(SearchView): |
10 | def extra_context(self, *args, **kwargs): | 10 | def extra_context(self, *args, **kwargs): |
@@ -16,6 +16,7 @@ class ColabSearchView(SearchView): | @@ -16,6 +16,7 @@ class ColabSearchView(SearchView): | ||
16 | types = { | 16 | types = { |
17 | 'thread': { | 17 | 'thread': { |
18 | 'name': _(u'Discussion'), | 18 | 'name': _(u'Discussion'), |
19 | + 'icon': 'envelope', | ||
19 | 'fields': ( | 20 | 'fields': ( |
20 | ('author', _(u'Author'), self.request.GET.get('author')), | 21 | ('author', _(u'Author'), self.request.GET.get('author')), |
21 | ( | 22 | ( |
@@ -26,113 +27,25 @@ class ColabSearchView(SearchView): | @@ -26,113 +27,25 @@ class ColabSearchView(SearchView): | ||
26 | ), | 27 | ), |
27 | }, | 28 | }, |
28 | } | 29 | } |
29 | - # TODO: Replace for a more generic plugin architecture | ||
30 | - # if settings.TRAC_ENABLED: | ||
31 | - # types['wiki'] = { | ||
32 | - # 'name': _(u'Wiki'), | ||
33 | - # 'fields': ( | ||
34 | - # ('author', _(u'Author'), self.request.GET.get('author')), | ||
35 | - # ( | ||
36 | - # 'collaborators', | ||
37 | - # _(u'Collaborators'), | ||
38 | - # self.request.GET.get('collaborators'), | ||
39 | - # ), | ||
40 | - # ), | ||
41 | - # } | ||
42 | - | ||
43 | - # types['ticket'] = { | ||
44 | - # 'name': _(u'Ticket'), | ||
45 | - # 'fields': ( | ||
46 | - # ( | ||
47 | - # 'milestone', | ||
48 | - # _(u'Milestone'), | ||
49 | - # self.request.GET.get('milestone') | ||
50 | - # ), | ||
51 | - # ( | ||
52 | - # 'priority', | ||
53 | - # _(u'Priority'), | ||
54 | - # self.request.GET.get('priority') | ||
55 | - # ), | ||
56 | - # ( | ||
57 | - # 'component', | ||
58 | - # _(u'Component'), | ||
59 | - # self.request.GET.get('component') | ||
60 | - # ), | ||
61 | - # ( | ||
62 | - # 'severity', | ||
63 | - # _(u'Severity'), | ||
64 | - # self.request.GET.get('severity') | ||
65 | - # ), | ||
66 | - # ( | ||
67 | - # 'reporter', | ||
68 | - # _(u'Reporter'), | ||
69 | - # self.request.GET.get('reporter') | ||
70 | - # ), | ||
71 | - # ('author', _(u'Author'), self.request.GET.get('author')), | ||
72 | - # ('tag', _(u'Status'), self.request.GET.get('tag')), | ||
73 | - # ( | ||
74 | - # 'keywords', | ||
75 | - # _(u'Keywords'), | ||
76 | - # self.request.GET.get('keywords'), | ||
77 | - # ), | ||
78 | - # ( | ||
79 | - # 'collaborators', | ||
80 | - # _(u'Collaborators'), | ||
81 | - # self.request.GET.get('collaborators') | ||
82 | - # ), | ||
83 | - # ), | ||
84 | - # } | ||
85 | - | ||
86 | - # types['changeset'] = { | ||
87 | - # 'name': _(u'Changeset'), | ||
88 | - # 'fields': ( | ||
89 | - # ('author', _(u'Author'), self.request.GET.get('author')), | ||
90 | - # ( | ||
91 | - # 'repository_name', | ||
92 | - # _(u'Repository'), | ||
93 | - # self.request.GET.get('repository_name'), | ||
94 | - # ), | ||
95 | - # ) | ||
96 | - # } | ||
97 | 30 | ||
98 | - # types['user'] = { | ||
99 | - # 'name': _(u'User'), | ||
100 | - # 'fields': ( | ||
101 | - # ( | ||
102 | - # 'username', | ||
103 | - # _(u'Username'), | ||
104 | - # self.request.GET.get('username'), | ||
105 | - # ), | ||
106 | - # ('name', _(u'Name'), self.request.GET.get('name')), | ||
107 | - # ( | ||
108 | - # 'institution', | ||
109 | - # _(u'Institution'), | ||
110 | - # self.request.GET.get('institution'), | ||
111 | - # ), | ||
112 | - # ('role', _(u'Role'), self.request.GET.get('role')) | ||
113 | - # ), | ||
114 | - # } | ||
115 | - | ||
116 | - # types['attachment'] = { | ||
117 | - # 'name': _(u'Attachment'), | ||
118 | - # 'fields': ( | ||
119 | - # ( | ||
120 | - # 'filename', | ||
121 | - # _(u'Filename'), | ||
122 | - # self.request.GET.get('filename') | ||
123 | - # ), | ||
124 | - # ('author', _(u'Author'), self.request.GET.get('author')), | ||
125 | - # ( | ||
126 | - # 'used_by', | ||
127 | - # _(u'Used by'), self.request.GET.get('used_by')), | ||
128 | - # ( | ||
129 | - # 'mimetype', | ||
130 | - # _(u'File type'), | ||
131 | - # self.request.GET.get('mimetype') | ||
132 | - # ), | ||
133 | - # ('size', _(u'Size'), self.request.GET.get('size')), | ||
134 | - # ) | ||
135 | - # } | 31 | + types['user'] = { |
32 | + 'name': _(u'User'), | ||
33 | + 'icon': 'user', | ||
34 | + 'fields': ( | ||
35 | + ( | ||
36 | + 'username', | ||
37 | + _(u'Username'), | ||
38 | + self.request.GET.get('username'), | ||
39 | + ), | ||
40 | + ('name', _(u'Name'), self.request.GET.get('name')), | ||
41 | + ( | ||
42 | + 'institution', | ||
43 | + _(u'Institution'), | ||
44 | + self.request.GET.get('institution'), | ||
45 | + ), | ||
46 | + ('role', _(u'Role'), self.request.GET.get('role')) | ||
47 | + ), | ||
48 | + } | ||
136 | 49 | ||
137 | try: | 50 | try: |
138 | type_chosen = self.form.cleaned_data.get('type') | 51 | type_chosen = self.form.cleaned_data.get('type') |
@@ -143,26 +56,17 @@ class ColabSearchView(SearchView): | @@ -143,26 +56,17 @@ class ColabSearchView(SearchView): | ||
143 | size_choices = () | 56 | size_choices = () |
144 | used_by_choices = () | 57 | used_by_choices = () |
145 | 58 | ||
146 | - # if type_chosen == 'attachment': | ||
147 | - # mimetype_choices = [ | ||
148 | - # (type_, display) | ||
149 | - # for type_, display, mimelist_ in settings.FILE_TYPE_GROUPINGS] | ||
150 | - # size_choices = [ | ||
151 | - # ('<500KB', u'< 500 KB'), | ||
152 | - # ('500KB__10MB', u'>= 500 KB <= 10 MB'), | ||
153 | - # ('>10MB', u'> 10 MB'), | ||
154 | - # ] | ||
155 | - # used_by_choices = set([ | ||
156 | - # (v, v) for v in Attachment.objects.values_list('used_by', | ||
157 | - # flat=True) | ||
158 | - # ]) | ||
159 | - | ||
160 | mimetype_chosen = self.request.GET.get('mimetype') | 59 | mimetype_chosen = self.request.GET.get('mimetype') |
161 | size_chosen = self.request.GET.get('size') | 60 | size_chosen = self.request.GET.get('size') |
162 | used_by_chosen = self.request.GET.get('used_by') | 61 | used_by_chosen = self.request.GET.get('used_by') |
163 | 62 | ||
63 | + types.update(filters_importer.import_plugin_filters(self.request)) | ||
64 | + | ||
65 | + filters_options = [(k, v['name'], v['icon']) \ | ||
66 | + for (k,v) in types.iteritems()] | ||
164 | return dict( | 67 | return dict( |
165 | filters=types.get(type_chosen), | 68 | filters=types.get(type_chosen), |
69 | + filters_options=filters_options, | ||
166 | type_chosen=type_chosen, | 70 | type_chosen=type_chosen, |
167 | order_data=settings.ORDERING_DATA, | 71 | order_data=settings.ORDERING_DATA, |
168 | date_format=date_format, | 72 | date_format=date_format, |