django-searchkit 1.1__py3-none-any.whl → 1.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. build/lib/build/lib/build/lib/example/example/__init__.py +0 -0
  2. build/lib/build/lib/build/lib/example/example/admin.py +16 -0
  3. build/lib/build/lib/build/lib/example/example/asgi.py +16 -0
  4. build/lib/build/lib/build/lib/example/example/management/__init__.py +0 -0
  5. build/lib/build/lib/build/lib/example/example/management/commands/__init__.py +0 -0
  6. build/lib/build/lib/build/lib/example/example/management/commands/createtestdata.py +62 -0
  7. build/lib/build/lib/build/lib/example/example/migrations/0001_initial.py +48 -0
  8. build/lib/build/lib/build/lib/example/example/migrations/__init__.py +0 -0
  9. build/lib/build/lib/build/lib/example/example/models.py +38 -0
  10. build/lib/build/lib/build/lib/example/example/settings.py +125 -0
  11. build/lib/build/lib/build/lib/example/example/urls.py +23 -0
  12. build/lib/build/lib/build/lib/example/example/wsgi.py +16 -0
  13. build/lib/build/lib/build/lib/searchkit/__init__.py +0 -0
  14. build/lib/build/lib/build/lib/searchkit/__version__.py +16 -0
  15. build/lib/build/lib/build/lib/searchkit/admin.py +30 -0
  16. build/lib/build/lib/build/lib/searchkit/apps.py +6 -0
  17. build/lib/build/lib/build/lib/searchkit/filters.py +28 -0
  18. build/lib/build/lib/build/lib/searchkit/forms/__init__.py +5 -0
  19. build/lib/build/lib/build/lib/searchkit/forms/fields.py +55 -0
  20. build/lib/build/lib/build/lib/searchkit/forms/search.py +62 -0
  21. build/lib/build/lib/build/lib/searchkit/forms/searchkit.py +154 -0
  22. build/lib/build/lib/build/lib/searchkit/forms/utils.py +178 -0
  23. build/lib/build/lib/build/lib/searchkit/migrations/0001_initial.py +30 -0
  24. build/lib/build/lib/build/lib/searchkit/migrations/0002_rename_searchkitsearch_search.py +18 -0
  25. build/lib/build/lib/build/lib/searchkit/migrations/__init__.py +0 -0
  26. build/lib/build/lib/build/lib/searchkit/models.py +21 -0
  27. build/lib/build/lib/build/lib/searchkit/templatetags/__init__.py +0 -0
  28. build/lib/build/lib/build/lib/searchkit/templatetags/searchkit.py +20 -0
  29. build/lib/build/lib/build/lib/searchkit/tests.py +400 -0
  30. build/lib/build/lib/build/lib/searchkit/urls.py +7 -0
  31. build/lib/build/lib/build/lib/searchkit/views.py +23 -0
  32. build/lib/build/lib/searchkit/__version__.py +1 -1
  33. build/lib/build/lib/searchkit/filters.py +5 -4
  34. build/lib/build/lib/searchkit/models.py +0 -6
  35. build/lib/build/lib/searchkit/tests.py +0 -2
  36. build/lib/build/lib/searchkit/views.py +3 -3
  37. build/lib/example/example/admin.py +1 -1
  38. build/lib/searchkit/__version__.py +1 -1
  39. build/lib/searchkit/filters.py +5 -4
  40. build/lib/searchkit/models.py +0 -6
  41. build/lib/searchkit/tests.py +0 -2
  42. build/lib/searchkit/views.py +3 -3
  43. {django_searchkit-1.1.dist-info → django_searchkit-1.2.dist-info}/METADATA +1 -1
  44. {django_searchkit-1.1.dist-info → django_searchkit-1.2.dist-info}/RECORD +53 -22
  45. searchkit/__version__.py +1 -1
  46. searchkit/filters.py +5 -4
  47. searchkit/models.py +0 -6
  48. searchkit/tests.py +0 -2
  49. searchkit/views.py +3 -3
  50. {django_searchkit-1.1.dist-info → django_searchkit-1.2.dist-info}/WHEEL +0 -0
  51. {django_searchkit-1.1.dist-info → django_searchkit-1.2.dist-info}/licenses/LICENCE +0 -0
  52. {django_searchkit-1.1.dist-info → django_searchkit-1.2.dist-info}/top_level.txt +0 -0
  53. {django_searchkit-1.1.dist-info → django_searchkit-1.2.dist-info}/zip-safe +0 -0
@@ -0,0 +1,400 @@
1
+ from urllib.parse import urlencode
2
+ from django.test import TestCase
3
+ from django.contrib.contenttypes.models import ContentType
4
+ from django.contrib.auth.models import User
5
+ from django.urls import reverse
6
+ from example.models import ModelA
7
+ from example.management.commands.createtestdata import Command as CreateTestData
8
+ from searchkit.forms.utils import FIELD_PLAN
9
+ from searchkit.forms.utils import SUPPORTED_FIELDS
10
+ from searchkit.forms.utils import ModelTree
11
+ from searchkit.forms import SearchForm
12
+ from searchkit.forms import SearchkitModelForm
13
+ from searchkit.forms import BaseSearchkitFormSet
14
+ from searchkit.forms import searchkit_formset_factory
15
+ from searchkit.models import Search
16
+ from searchkit import __version__
17
+
18
+
19
+ SearchkitFormSet = searchkit_formset_factory(model=ModelA)
20
+ SearchkitForm = SearchkitFormSet.form
21
+
22
+
23
+ INITIAL_DATA = [
24
+ dict(
25
+ field='model_b__chars',
26
+ operator='contains',
27
+ value='anytext',
28
+ ),
29
+ dict(
30
+ field='integer',
31
+ operator='range',
32
+ value=[1, 123],
33
+ ),
34
+ dict(
35
+ field='float',
36
+ operator='gt',
37
+ value='0.3',
38
+ ),
39
+ dict(
40
+ field='decimal',
41
+ operator='lte',
42
+ value='1.23',
43
+ ),
44
+ dict(
45
+ field='date',
46
+ operator='exact',
47
+ value='2025-05-14',
48
+ ),
49
+ dict(
50
+ field='datetime',
51
+ operator='exact',
52
+ value='2025-05-14 08:45',
53
+ )
54
+ ]
55
+
56
+ add_prefix = lambda i: SearchkitFormSet().add_prefix(i)
57
+ contenttype = ContentType.objects.get_for_model(ModelA)
58
+ DEFAULT_PREFIX = SearchkitFormSet.get_default_prefix()
59
+
60
+ def get_form_data(initial_data=INITIAL_DATA):
61
+ count = len(initial_data)
62
+ data = {
63
+ 'name': 'test search', # The name of the search.
64
+ 'searchkit_model': f'{contenttype.pk}', # Data for the searchkit-model form.
65
+ f'{DEFAULT_PREFIX}-TOTAL_FORMS': f'{count}', # Data for the managment form.
66
+ f'{DEFAULT_PREFIX}-INITIAL_FORMS': f'{count}', # Data for the managment form.
67
+ }
68
+ for i, d in enumerate(initial_data):
69
+ prefix = SearchkitFormSet().add_prefix(i)
70
+ for key, value in d.items():
71
+ if isinstance(value, list):
72
+ for i, v in enumerate(value):
73
+ data.update({f'{prefix}-{key}_{i}': v})
74
+ else:
75
+ data.update({f'{prefix}-{key}': value})
76
+ return data
77
+
78
+ FORM_DATA = get_form_data()
79
+
80
+
81
+ class CheckFormMixin:
82
+ """
83
+ Mixin to check the form fields and their choices.
84
+ """
85
+ def check_form(self, form):
86
+ # Three fields should be generated on instantiation.
87
+ self.assertIn('field', form.fields)
88
+ self.assertIn('operator', form.fields)
89
+ self.assertIn('value', form.fields)
90
+ self.assertEqual(len(form.fields), 3)
91
+
92
+ # Check field choices for the model.
93
+ form_model_field = form.fields['field']
94
+ self.assertTrue(form_model_field.choices)
95
+ options = [c[0] for c in form_model_field.choices]
96
+ tree = ModelTree(ModelA)
97
+ for node in tree.iterate():
98
+ for model_field in node.model._meta.fields:
99
+ if not any(isinstance(model_field, f) for f in SUPPORTED_FIELDS):
100
+ continue
101
+ if node.is_root:
102
+ self.assertIn(model_field.name, options)
103
+ else:
104
+ self.assertIn(f'{node.field_path}__{model_field.name}', options)
105
+
106
+ # Check the field_plan choosen based on the model_field.
107
+ field_plan = next(iter([p for t, p in FIELD_PLAN.items() if t(form.model_field)]))
108
+ self.assertEqual(form.field_plan, field_plan)
109
+
110
+ # Check choices of the operator field based on the field_plan.
111
+ operator_field = form.fields['operator']
112
+ self.assertTrue(operator_field.choices)
113
+ self.assertEqual(len(operator_field.choices), len(form.field_plan))
114
+ for operator in form.field_plan.keys():
115
+ self.assertIn(operator, [c[0] for c in operator_field.choices])
116
+
117
+
118
+ class SearchkitFormTestCase(CheckFormMixin, TestCase):
119
+
120
+ def test_blank_searchkitform(self):
121
+ form = SearchkitForm(prefix=add_prefix(0))
122
+ self.check_form(form)
123
+
124
+ # Form should not be bound or valid.
125
+ self.assertFalse(form.is_bound)
126
+ self.assertFalse(form.is_valid())
127
+
128
+ def test_searchkitform_with_invalid_model_field_data(self):
129
+ data = {
130
+ f'{add_prefix(0)}-field': 'foobar',
131
+ }
132
+ form = SearchkitForm(data, prefix=add_prefix(0))
133
+ self.check_form(form)
134
+
135
+ # Form should be invalid.
136
+ self.assertFalse(form.is_valid())
137
+
138
+ # Check error message in html.
139
+ errors = ['Select a valid choice. foobar is not one of the available choices.']
140
+ self.assertIn(errors, form.errors.values())
141
+
142
+ def test_searchkitform_with_valid_model_field_data(self):
143
+ data = {
144
+ f'{add_prefix(0)}-field': 'integer',
145
+ }
146
+ form = SearchkitForm(data, prefix=add_prefix(0))
147
+ self.check_form(form)
148
+
149
+ # Form should be invalid since no value data is provieded.
150
+ self.assertFalse(form.is_valid())
151
+
152
+ def test_searchkitform_with_invalid_operator_data(self):
153
+ data = {
154
+ f'{add_prefix(0)}-field': 'integer',
155
+ f'{add_prefix(0)}-operator': 'foobar',
156
+ }
157
+ form = SearchkitForm(data, prefix=add_prefix(0))
158
+ self.check_form(form)
159
+
160
+ # Form should be invalid.
161
+ self.assertFalse(form.is_valid())
162
+
163
+ # Check error message in html.
164
+ errors = ['Select a valid choice. foobar is not one of the available choices.']
165
+ self.assertIn(errors, form.errors.values())
166
+
167
+ def test_searchkitform_with_valid_operator_data(self):
168
+ data = {
169
+ f'{add_prefix(0)}-field': 'integer',
170
+ f'{add_prefix(0)}-operator': 'exact',
171
+ }
172
+ form = SearchkitForm(data, prefix=add_prefix(0))
173
+ self.check_form(form)
174
+
175
+ # Form should be invalid since no value data is provieded.
176
+ self.assertFalse(form.is_valid())
177
+
178
+ def test_searchkitform_with_valid_data(self):
179
+ data = {
180
+ f'{add_prefix(0)}-field': 'integer',
181
+ f'{add_prefix(0)}-operator': 'exact',
182
+ f'{add_prefix(0)}-value': '123',
183
+ }
184
+ form = SearchkitForm(data, prefix=add_prefix(0))
185
+ self.check_form(form)
186
+
187
+ # Form should be valid.
188
+ self.assertTrue(form.is_valid())
189
+
190
+ def test_searchkitform_with_invalid_data(self):
191
+ data = {
192
+ f'{add_prefix(0)}-field': 'integer',
193
+ f'{add_prefix(0)}-operator': 'exact',
194
+ f'{add_prefix(0)}-value': 'foobar',
195
+ }
196
+ form = SearchkitForm(data, prefix=add_prefix(0))
197
+ self.check_form(form)
198
+
199
+ # Form should be invalid.
200
+ self.assertFalse(form.is_valid())
201
+
202
+ # Check error message in html.
203
+ errors = ['Enter a whole number.']
204
+ self.assertIn(errors, form.errors.values())
205
+
206
+
207
+ class SearchkitFormSetTestCase(CheckFormMixin, TestCase):
208
+ def test_blank_searchkitform(self):
209
+ # Instantiating the formset neither with a model instance nor with model
210
+ # related data or initial data should result in a formset without forms,
211
+ # that is invalid and unbound.
212
+ formset = SearchkitFormSet()
213
+ self.assertFalse(formset.is_bound)
214
+ self.assertFalse(formset.is_valid())
215
+
216
+ def test_searchkit_formset_with_valid_data(self):
217
+ formset = SearchkitFormSet(FORM_DATA)
218
+ self.assertTrue(formset.is_valid())
219
+
220
+ def test_searchkit_formset_with_invalid_data(self):
221
+ data = FORM_DATA.copy()
222
+ del data[f'{add_prefix(0)}-value']
223
+ formset = SearchkitFormSet(data)
224
+ self.assertFalse(formset.is_valid())
225
+
226
+ # Check error message in html.
227
+ errors = ['This field is required.']
228
+ self.assertIn(errors, formset.forms[0].errors.values())
229
+
230
+ def test_searchkit_formset_with_initial_data(self):
231
+ formset_class = searchkit_formset_factory(model=ModelA, extra=0)
232
+ formset = formset_class(initial=INITIAL_DATA)
233
+ self.assertFalse(formset.is_bound)
234
+ self.assertFalse(formset.is_valid())
235
+ self.assertEqual(len(formset.forms), len(INITIAL_DATA))
236
+ for i, form in enumerate(formset.forms):
237
+ self.assertEqual(form.initial, INITIAL_DATA[i])
238
+ self.check_form(form)
239
+
240
+
241
+ class SearchkitSearchFormTestCase(TestCase):
242
+ def test_searchkit_search_form_without_data(self):
243
+ form = SearchForm()
244
+ self.assertFalse(form.is_bound)
245
+ self.assertFalse(form.is_valid())
246
+ self.assertIsInstance(form.formset, BaseSearchkitFormSet)
247
+ self.assertEqual(form.formset.model, None)
248
+
249
+ def test_searchkit_search_form_with_data(self):
250
+ form = SearchForm(FORM_DATA)
251
+ self.assertTrue(form.is_bound)
252
+ self.assertTrue(form.is_valid())
253
+ self.assertIsInstance(form.formset, BaseSearchkitFormSet)
254
+ self.assertEqual(form.formset.model, ModelA)
255
+ self.assertEqual(form.instance.data, form.formset.cleaned_data)
256
+
257
+ # Saving the instance works.
258
+ form.instance.save()
259
+ self.assertTrue(form.instance.pk)
260
+
261
+ # Using the instance data as filter rules works.
262
+ filter_rules = form.instance.as_lookups()
263
+ self.assertEqual(len(filter_rules), len(INITIAL_DATA))
264
+ for data in INITIAL_DATA:
265
+ self.assertIn(f"{data['field']}__{data['operator']}", filter_rules)
266
+
267
+
268
+ class SearchkitModelFormTestCase(TestCase):
269
+ def test_searchkit_model_form_choices(self):
270
+ form = SearchkitModelForm()
271
+ labels = [c[1] for c in form.fields['searchkit_model'].choices]
272
+ self.assertEqual(len(labels), 3)
273
+ self.assertEqual('select a model', labels[0].lower())
274
+ self.assertEqual('example | model a', labels[1].lower())
275
+ self.assertEqual('example | model b', labels[2].lower())
276
+
277
+
278
+ class AdminBackendTest(TestCase):
279
+ @classmethod
280
+ def setUpTestData(cls):
281
+ CreateTestData().handle()
282
+
283
+ def setUp(self):
284
+ admin = User.objects.get(username='admin')
285
+ self.client.force_login(admin)
286
+
287
+ def test_search_form(self):
288
+ url = reverse('admin:searchkit_search_add')
289
+ resp = self.client.get(url)
290
+ self.assertEqual(resp.status_code, 200)
291
+ select = b'<select name="searchkit_model" class="searchkit-reload-on-change" data-total-forms="1" required id="id_searchkit_model">'
292
+ for snippet in select.split(b' '):
293
+ self.assertIn(snippet, resp.content)
294
+
295
+ def test_search_form_with_initial(self):
296
+ url = reverse('admin:searchkit_search_add') + '?searchkit_model=1'
297
+ resp = self.client.get(url)
298
+ self.assertEqual(resp.status_code, 200)
299
+ select = '<select name="searchkit_model" class="searchkit-reload-on-change" data-total-forms="1" required id="id_searchkit_model">'
300
+ for snippet in select.split(' '):
301
+ self.assertIn(snippet, str(resp.content))
302
+ self.assertIn('<option value="1" selected>', str(resp.content))
303
+ self.assertIn('name="searchkit-example-modela-0-field"', str(resp.content))
304
+
305
+ def test_add_search(self):
306
+ # Create a search object via the admin backend.
307
+ url = reverse('admin:searchkit_search_add')
308
+ data = FORM_DATA.copy()
309
+ data['_save_and_apply'] = True
310
+ resp = self.client.post(url, data, follow=True)
311
+ self.assertEqual(resp.status_code, 200)
312
+ self.assertEqual(len(Search.objects.all()), 1)
313
+
314
+ # Change it via backend.
315
+ url = reverse('admin:searchkit_search_change', args=(1,))
316
+ data['name'] = 'Changed name'
317
+ data['searchkit-example-modela-0-field'] = 'boolean'
318
+ data['searchkit-example-modela-0-operator'] = 'exact'
319
+ data['searchkit-example-modela-0-value'] = 'true'
320
+ resp = self.client.post(url, data, follow=True)
321
+ self.assertEqual(resp.status_code, 200)
322
+ self.assertEqual(Search.objects.get(pk=1).name, data['name'])
323
+
324
+ # Will the search be listed in the admin filter?
325
+ url = reverse('admin:example_modela_changelist')
326
+ resp = self.client.get(url)
327
+ self.assertEqual(resp.status_code, 200)
328
+ self.assertIn('href="?search=1"', str(resp.content))
329
+ self.assertIn(data['name'], str(resp.content))
330
+
331
+
332
+ class SearchViewTest(TestCase):
333
+
334
+ def setUp(self):
335
+ self.initial = [
336
+ dict(
337
+ field='integer',
338
+ operator='exact',
339
+ value=1,
340
+ )
341
+ ]
342
+ self.initial_range = [
343
+ dict(
344
+ field='integer',
345
+ operator='range',
346
+ value=[1,3],
347
+ )
348
+ ]
349
+
350
+ def test_search_view_invalid_data(self):
351
+ initial = self.initial.copy()
352
+ initial[0]['value'] = 'no integer'
353
+ data = get_form_data(initial)
354
+ url_params = urlencode(data)
355
+ base_url = reverse('searchkit_form')
356
+ url = f'{base_url}?{url_params}'
357
+ resp = self.client.get(url)
358
+ self.assertEqual(resp.status_code, 200)
359
+ html_error = '<li>Enter a whole number.</li>'
360
+ self.assertInHTML(html_error, str(resp.content))
361
+
362
+ def test_search_view_missing_data(self):
363
+ initial = self.initial.copy()
364
+ del(initial[0]['value'])
365
+ data = get_form_data(initial)
366
+ url_params = urlencode(data)
367
+ base_url = reverse('searchkit_form')
368
+ url = f'{base_url}?{url_params}'
369
+ resp = self.client.get(url)
370
+ self.assertEqual(resp.status_code, 200)
371
+ html_error = '<li>This field is required.</li>'
372
+ self.assertInHTML(html_error, str(resp.content))
373
+
374
+ def test_search_view_with_range_operator(self):
375
+ data = get_form_data(self.initial_range)
376
+ url_params = urlencode(data)
377
+ base_url = reverse('searchkit_form')
378
+ url = f'{base_url}?{url_params}'
379
+ resp = self.client.get(url)
380
+ self.assertEqual(resp.status_code, 200)
381
+ html = '<input type="number" name="searchkit-example-modela-0-value_1" value="3" id="id_searchkit-example-modela-0-value_1">'
382
+ self.assertInHTML(html, str(resp.content))
383
+
384
+ def test_search_view_with_model(self):
385
+ data = get_form_data(self.initial)
386
+ data['searchkit_model'] = ContentType.objects.get_for_model(ModelA).pk
387
+ url_params = urlencode(data)
388
+ base_url = reverse('searchkit_form')
389
+ url = f'{base_url}?{url_params}'
390
+ resp = self.client.get(url)
391
+ self.assertEqual(resp.status_code, 200)
392
+
393
+ def test_search_view_with_invalid_model(self):
394
+ data = get_form_data(self.initial)
395
+ data['searchkit_model'] = 9999 # Non-existing content type.
396
+ url_params = urlencode(data)
397
+ base_url = reverse('searchkit_form')
398
+ url = f'{base_url}?{url_params}'
399
+ resp = self.client.get(url)
400
+ self.assertEqual(resp.status_code, 400)
@@ -0,0 +1,7 @@
1
+ from django.urls import path
2
+ from .views import SearchkitAjaxView
3
+
4
+
5
+ urlpatterns = [
6
+ path("searchkit/", SearchkitAjaxView.as_view(), name="searchkit_form"),
7
+ ]
@@ -0,0 +1,23 @@
1
+ from django.utils.translation import gettext_lazy as _
2
+ from django.http import HttpResponse
3
+ from django.http import Http404, HttpResponseBadRequest
4
+ from django.apps import apps
5
+ from django.contrib.contenttypes.models import ContentType
6
+ from django.views.generic import View
7
+ from .forms import SearchkitModelForm
8
+ from .forms import searchkit_formset_factory
9
+
10
+
11
+ # FIXME: Check permissions and authentication.
12
+ class SearchkitAjaxView(View):
13
+ """
14
+ Reload the formset via ajax.
15
+ """
16
+ def get(self, request, **kwargs):
17
+ model_form = SearchkitModelForm(data=self.request.GET)
18
+ if model_form.is_valid():
19
+ model = model_form.cleaned_data['searchkit_model'].model_class()
20
+ formset = searchkit_formset_factory(model=model)(data=request.GET)
21
+ return HttpResponse(formset.render())
22
+ else:
23
+ return HttpResponseBadRequest(_('Invalid searchkit-model-form.'))
@@ -13,4 +13,4 @@ Version 0.x should be considered a development version with an unstable API,
13
13
  and backwards compatibility is not guaranteed for minor versions.
14
14
  """
15
15
 
16
- __version__ = "1.1"
16
+ __version__ = "1.2"
@@ -14,14 +14,15 @@ class SearchkitFilter(SimpleListFilter):
14
14
  self.searchkit_model = ContentType.objects.get_for_model(model)
15
15
  super().__init__(request, params, model, model_admin)
16
16
 
17
+ def has_output(self):
18
+ return True
19
+
17
20
  def lookups(self, request, model_admin):
18
- # Fetch the last three objects from SearchkitSearch and return them as
19
- # choices.
20
- searches = Search.objects.filter(contenttype=self.searchkit_model).order_by('-created_date')[:3]
21
+ searches = Search.objects.filter(contenttype=self.searchkit_model).order_by('-created_date')
21
22
  return [(str(obj.id), obj.name) for obj in searches]
22
23
 
23
24
  def queryset(self, request, queryset):
24
25
  # Filter the queryset based on the selected SearchkitSearch object
25
26
  if self.value():
26
27
  search = Search.objects.get(id=int(self.value()))
27
- return search.as_queryset()
28
+ return queryset.filter(**search.as_lookups())
@@ -19,9 +19,3 @@ class Search(models.Model):
19
19
  for data in self.data:
20
20
  lookups[f'{data["field"]}__{data["operator"]}'] = data['value']
21
21
  return lookups
22
-
23
- def as_queryset(self):
24
- """
25
- Returns a filtered queryset for the model.
26
- """
27
- return self.contenttype.model_class().objects.filter(**self.as_lookups())
@@ -263,8 +263,6 @@ class SearchkitSearchFormTestCase(TestCase):
263
263
  self.assertEqual(len(filter_rules), len(INITIAL_DATA))
264
264
  for data in INITIAL_DATA:
265
265
  self.assertIn(f"{data['field']}__{data['operator']}", filter_rules)
266
- queryset = form.instance.as_queryset()
267
- self.assertTrue(queryset.model == ModelA)
268
266
 
269
267
 
270
268
  class SearchkitModelFormTestCase(TestCase):
@@ -14,9 +14,9 @@ class SearchkitAjaxView(View):
14
14
  Reload the formset via ajax.
15
15
  """
16
16
  def get(self, request, **kwargs):
17
- contenttype_form = SearchkitModelForm(data=self.request.GET)
18
- if contenttype_form.is_valid():
19
- model = contenttype_form.cleaned_data['searchkit_model'].model_class()
17
+ model_form = SearchkitModelForm(data=self.request.GET)
18
+ if model_form.is_valid():
19
+ model = model_form.cleaned_data['searchkit_model'].model_class()
20
20
  formset = searchkit_formset_factory(model=model)(data=request.GET)
21
21
  return HttpResponse(formset.render())
22
22
  else:
@@ -7,7 +7,7 @@ from .models import ModelB
7
7
  @admin.register(ModelA)
8
8
  class ModelAAdmin(admin.ModelAdmin):
9
9
  list_display = [f.name for f in ModelA._meta.fields]
10
- list_filter = [SearchkitFilter, 'chars_choices']
10
+ list_filter = [SearchkitFilter]
11
11
 
12
12
 
13
13
  @admin.register(ModelB)
@@ -13,4 +13,4 @@ Version 0.x should be considered a development version with an unstable API,
13
13
  and backwards compatibility is not guaranteed for minor versions.
14
14
  """
15
15
 
16
- __version__ = "1.1"
16
+ __version__ = "1.2"
@@ -14,14 +14,15 @@ class SearchkitFilter(SimpleListFilter):
14
14
  self.searchkit_model = ContentType.objects.get_for_model(model)
15
15
  super().__init__(request, params, model, model_admin)
16
16
 
17
+ def has_output(self):
18
+ return True
19
+
17
20
  def lookups(self, request, model_admin):
18
- # Fetch the last three objects from SearchkitSearch and return them as
19
- # choices.
20
- searches = Search.objects.filter(contenttype=self.searchkit_model).order_by('-created_date')[:3]
21
+ searches = Search.objects.filter(contenttype=self.searchkit_model).order_by('-created_date')
21
22
  return [(str(obj.id), obj.name) for obj in searches]
22
23
 
23
24
  def queryset(self, request, queryset):
24
25
  # Filter the queryset based on the selected SearchkitSearch object
25
26
  if self.value():
26
27
  search = Search.objects.get(id=int(self.value()))
27
- return search.as_queryset()
28
+ return queryset.filter(**search.as_lookups())
@@ -19,9 +19,3 @@ class Search(models.Model):
19
19
  for data in self.data:
20
20
  lookups[f'{data["field"]}__{data["operator"]}'] = data['value']
21
21
  return lookups
22
-
23
- def as_queryset(self):
24
- """
25
- Returns a filtered queryset for the model.
26
- """
27
- return self.contenttype.model_class().objects.filter(**self.as_lookups())
@@ -263,8 +263,6 @@ class SearchkitSearchFormTestCase(TestCase):
263
263
  self.assertEqual(len(filter_rules), len(INITIAL_DATA))
264
264
  for data in INITIAL_DATA:
265
265
  self.assertIn(f"{data['field']}__{data['operator']}", filter_rules)
266
- queryset = form.instance.as_queryset()
267
- self.assertTrue(queryset.model == ModelA)
268
266
 
269
267
 
270
268
  class SearchkitModelFormTestCase(TestCase):
@@ -14,9 +14,9 @@ class SearchkitAjaxView(View):
14
14
  Reload the formset via ajax.
15
15
  """
16
16
  def get(self, request, **kwargs):
17
- contenttype_form = SearchkitModelForm(data=self.request.GET)
18
- if contenttype_form.is_valid():
19
- model = contenttype_form.cleaned_data['searchkit_model'].model_class()
17
+ model_form = SearchkitModelForm(data=self.request.GET)
18
+ if model_form.is_valid():
19
+ model = model_form.cleaned_data['searchkit_model'].model_class()
20
20
  formset = searchkit_formset_factory(model=model)(data=request.GET)
21
21
  return HttpResponse(formset.render())
22
22
  else:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: django-searchkit
3
- Version: 1.1
3
+ Version: 1.2
4
4
  Summary: Finally a real searchkit for django!
5
5
  Home-page: https://github.com/thomst/django-searchkit
6
6
  Author: Thomas Leichtfuß