django-searchkit 0.1__tar.gz → 1.1__tar.gz
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.
- {django_searchkit-0.1/django_searchkit.egg-info → django_searchkit-1.1}/PKG-INFO +34 -13
- django_searchkit-1.1/README.md +58 -0
- {django_searchkit-0.1 → django_searchkit-1.1/django_searchkit.egg-info}/PKG-INFO +34 -13
- {django_searchkit-0.1 → django_searchkit-1.1}/django_searchkit.egg-info/SOURCES.txt +1 -0
- django_searchkit-1.1/django_searchkit.egg-info/requires.txt +3 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/django_searchkit.egg-info/top_level.txt +1 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/admin.py +1 -1
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/__version__.py +1 -1
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/admin.py +4 -4
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/filters.py +7 -11
- django_searchkit-1.1/searchkit/forms/__init__.py +5 -0
- django_searchkit-1.1/searchkit/forms/search.py +62 -0
- django_searchkit-1.1/searchkit/forms/searchkit.py +154 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/forms/utils.py +44 -15
- django_searchkit-1.1/searchkit/migrations/0002_rename_searchkitsearch_search.py +18 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/models.py +8 -4
- django_searchkit-1.1/searchkit/templatetags/searchkit.py +20 -0
- django_searchkit-1.1/searchkit/tests.py +402 -0
- django_searchkit-1.1/searchkit/urls.py +7 -0
- django_searchkit-1.1/searchkit/views.py +23 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/setup.py +3 -6
- django_searchkit-0.1/README.md +0 -34
- django_searchkit-0.1/django_searchkit.egg-info/requires.txt +0 -1
- django_searchkit-0.1/searchkit/forms/__init__.py +0 -2
- django_searchkit-0.1/searchkit/forms/search.py +0 -45
- django_searchkit-0.1/searchkit/forms/searchkit.py +0 -192
- django_searchkit-0.1/searchkit/templatetags/searchkit.py +0 -47
- django_searchkit-0.1/searchkit/tests.py +0 -197
- django_searchkit-0.1/searchkit/urls.py +0 -8
- django_searchkit-0.1/searchkit/views.py +0 -30
- {django_searchkit-0.1 → django_searchkit-1.1}/LICENCE +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/MANIFEST.in +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/django_searchkit.egg-info/dependency_links.txt +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/django_searchkit.egg-info/zip-safe +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/asgi.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/management/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/management/commands/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/management/commands/createtestdata.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/migrations/0001_initial.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/migrations/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/models.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/settings.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/urls.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/example/example/wsgi.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/apps.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/forms/fields.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/migrations/0001_initial.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/migrations/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/searchkit/templatetags/__init__.py +0 -0
- {django_searchkit-0.1 → django_searchkit-1.1}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: django-searchkit
|
3
|
-
Version:
|
3
|
+
Version: 1.1
|
4
4
|
Summary: Finally a real searchkit for django!
|
5
5
|
Home-page: https://github.com/thomst/django-searchkit
|
6
6
|
Author: Thomas Leichtfuß
|
@@ -9,9 +9,6 @@ License: BSD License
|
|
9
9
|
Platform: OS Independent
|
10
10
|
Classifier: Development Status :: 5 - Production/Stable
|
11
11
|
Classifier: Framework :: Django
|
12
|
-
Classifier: Framework :: Django :: 3.0
|
13
|
-
Classifier: Framework :: Django :: 3.1
|
14
|
-
Classifier: Framework :: Django :: 3.2
|
15
12
|
Classifier: Framework :: Django :: 4.0
|
16
13
|
Classifier: Framework :: Django :: 4.1
|
17
14
|
Classifier: Framework :: Django :: 4.2
|
@@ -23,8 +20,6 @@ Classifier: License :: OSI Approved :: BSD License
|
|
23
20
|
Classifier: Operating System :: OS Independent
|
24
21
|
Classifier: Programming Language :: Python
|
25
22
|
Classifier: Programming Language :: Python :: 3
|
26
|
-
Classifier: Programming Language :: Python :: 3.7
|
27
|
-
Classifier: Programming Language :: Python :: 3.8
|
28
23
|
Classifier: Programming Language :: Python :: 3.9
|
29
24
|
Classifier: Programming Language :: Python :: 3.10
|
30
25
|
Classifier: Programming Language :: Python :: 3.11
|
@@ -33,7 +28,9 @@ Classifier: Topic :: Software Development
|
|
33
28
|
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
34
29
|
Description-Content-Type: text/markdown
|
35
30
|
License-File: LICENCE
|
36
|
-
Requires-Dist: Django>=
|
31
|
+
Requires-Dist: Django>=4.0
|
32
|
+
Requires-Dist: django-picklefield>=2.0
|
33
|
+
Requires-Dist: django-modeltree
|
37
34
|
Dynamic: author
|
38
35
|
Dynamic: author-email
|
39
36
|
Dynamic: classifier
|
@@ -50,13 +47,19 @@ Dynamic: summary
|
|
50
47
|
|
51
48
|
[<img src="https://github.com/thomst/django-searchkit/actions/workflows/ci.yml/badge.svg">](https://github.com/thomst/django-searchkit/)
|
52
49
|
[<img src="https://coveralls.io/repos/github/thomst/django-searchkit/badge.svg?branch=main">](https://coveralls.io/github/thomst/django-searchkit?branch=main)
|
53
|
-
[<img src="https://img.shields.io/badge/python-3.
|
54
|
-
[<img src="https://img.shields.io/badge/django-
|
50
|
+
[<img src="https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%203.11-blue">](https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%203.11-blue)
|
51
|
+
[<img src="https://img.shields.io/badge/django-4.0%20%7C%204.1%20%7C%204.2%20%7C%205.0%20%7C%205.1%20%7C%205.2-orange">](https://img.shields.io/badge/django-4.0%20%7C%204.1%20%7C%204.2%20%7C%205.0%20%7C%205.1%20%7C%205.2-orange)
|
55
52
|
|
56
53
|
|
57
54
|
## Description
|
58
55
|
|
59
|
-
|
56
|
+
Finally there is a real searchkit application for django that integrates best
|
57
|
+
with the django admin backend.
|
58
|
+
|
59
|
+
Build and apply complex searches on model instances right in the backend without
|
60
|
+
any coding. Save and reuse your searches by a handy django admin filter with a
|
61
|
+
single click.
|
62
|
+
|
60
63
|
|
61
64
|
## Setup
|
62
65
|
|
@@ -73,10 +76,28 @@ INSTALLED_APPS = [
|
|
73
76
|
]
|
74
77
|
```
|
75
78
|
|
76
|
-
|
79
|
+
Add the `SearkitFilter` to your `ModelAdmin`:
|
80
|
+
```
|
81
|
+
from django.contrib import admin
|
82
|
+
from searchkit.filters import SearchkitFilter
|
83
|
+
from .models import MyModel
|
84
|
+
|
77
85
|
|
78
|
-
|
86
|
+
@admin.register(MyModel)
|
87
|
+
class MyModelAdmin(admin.ModelAdmin):
|
88
|
+
...
|
89
|
+
list_filter = [
|
90
|
+
SearchkitFilter,
|
91
|
+
...
|
92
|
+
]
|
93
|
+
...
|
94
|
+
```
|
79
95
|
|
80
96
|
## Usage
|
81
97
|
|
82
|
-
|
98
|
+
1. Open the admin changelist of your Model.
|
99
|
+
2. Click the "Add filter" button of the Searchkit filter.
|
100
|
+
3. Give your Filter a name.
|
101
|
+
4. Configure as many filter rules as you want.
|
102
|
+
5. Click "Save and apply".
|
103
|
+
6. Reuse your filter whenever you want using the Searchkit filter section.
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# Welcome to django-searchkit
|
2
|
+
|
3
|
+
[<img src="https://github.com/thomst/django-searchkit/actions/workflows/ci.yml/badge.svg">](https://github.com/thomst/django-searchkit/)
|
4
|
+
[<img src="https://coveralls.io/repos/github/thomst/django-searchkit/badge.svg?branch=main">](https://coveralls.io/github/thomst/django-searchkit?branch=main)
|
5
|
+
[<img src="https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%203.11-blue">](https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%203.11-blue)
|
6
|
+
[<img src="https://img.shields.io/badge/django-4.0%20%7C%204.1%20%7C%204.2%20%7C%205.0%20%7C%205.1%20%7C%205.2-orange">](https://img.shields.io/badge/django-4.0%20%7C%204.1%20%7C%204.2%20%7C%205.0%20%7C%205.1%20%7C%205.2-orange)
|
7
|
+
|
8
|
+
|
9
|
+
## Description
|
10
|
+
|
11
|
+
Finally there is a real searchkit application for django that integrates best
|
12
|
+
with the django admin backend.
|
13
|
+
|
14
|
+
Build and apply complex searches on model instances right in the backend without
|
15
|
+
any coding. Save and reuse your searches by a handy django admin filter with a
|
16
|
+
single click.
|
17
|
+
|
18
|
+
|
19
|
+
## Setup
|
20
|
+
|
21
|
+
Install via pip:
|
22
|
+
```
|
23
|
+
pip install django-searchkit
|
24
|
+
```
|
25
|
+
|
26
|
+
Add `searchkit` to your `INSTALLED_APPS`:
|
27
|
+
```
|
28
|
+
INSTALLED_APPS = [
|
29
|
+
'searchkit',
|
30
|
+
...
|
31
|
+
]
|
32
|
+
```
|
33
|
+
|
34
|
+
Add the `SearkitFilter` to your `ModelAdmin`:
|
35
|
+
```
|
36
|
+
from django.contrib import admin
|
37
|
+
from searchkit.filters import SearchkitFilter
|
38
|
+
from .models import MyModel
|
39
|
+
|
40
|
+
|
41
|
+
@admin.register(MyModel)
|
42
|
+
class MyModelAdmin(admin.ModelAdmin):
|
43
|
+
...
|
44
|
+
list_filter = [
|
45
|
+
SearchkitFilter,
|
46
|
+
...
|
47
|
+
]
|
48
|
+
...
|
49
|
+
```
|
50
|
+
|
51
|
+
## Usage
|
52
|
+
|
53
|
+
1. Open the admin changelist of your Model.
|
54
|
+
2. Click the "Add filter" button of the Searchkit filter.
|
55
|
+
3. Give your Filter a name.
|
56
|
+
4. Configure as many filter rules as you want.
|
57
|
+
5. Click "Save and apply".
|
58
|
+
6. Reuse your filter whenever you want using the Searchkit filter section.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: django-searchkit
|
3
|
-
Version:
|
3
|
+
Version: 1.1
|
4
4
|
Summary: Finally a real searchkit for django!
|
5
5
|
Home-page: https://github.com/thomst/django-searchkit
|
6
6
|
Author: Thomas Leichtfuß
|
@@ -9,9 +9,6 @@ License: BSD License
|
|
9
9
|
Platform: OS Independent
|
10
10
|
Classifier: Development Status :: 5 - Production/Stable
|
11
11
|
Classifier: Framework :: Django
|
12
|
-
Classifier: Framework :: Django :: 3.0
|
13
|
-
Classifier: Framework :: Django :: 3.1
|
14
|
-
Classifier: Framework :: Django :: 3.2
|
15
12
|
Classifier: Framework :: Django :: 4.0
|
16
13
|
Classifier: Framework :: Django :: 4.1
|
17
14
|
Classifier: Framework :: Django :: 4.2
|
@@ -23,8 +20,6 @@ Classifier: License :: OSI Approved :: BSD License
|
|
23
20
|
Classifier: Operating System :: OS Independent
|
24
21
|
Classifier: Programming Language :: Python
|
25
22
|
Classifier: Programming Language :: Python :: 3
|
26
|
-
Classifier: Programming Language :: Python :: 3.7
|
27
|
-
Classifier: Programming Language :: Python :: 3.8
|
28
23
|
Classifier: Programming Language :: Python :: 3.9
|
29
24
|
Classifier: Programming Language :: Python :: 3.10
|
30
25
|
Classifier: Programming Language :: Python :: 3.11
|
@@ -33,7 +28,9 @@ Classifier: Topic :: Software Development
|
|
33
28
|
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
34
29
|
Description-Content-Type: text/markdown
|
35
30
|
License-File: LICENCE
|
36
|
-
Requires-Dist: Django>=
|
31
|
+
Requires-Dist: Django>=4.0
|
32
|
+
Requires-Dist: django-picklefield>=2.0
|
33
|
+
Requires-Dist: django-modeltree
|
37
34
|
Dynamic: author
|
38
35
|
Dynamic: author-email
|
39
36
|
Dynamic: classifier
|
@@ -50,13 +47,19 @@ Dynamic: summary
|
|
50
47
|
|
51
48
|
[<img src="https://github.com/thomst/django-searchkit/actions/workflows/ci.yml/badge.svg">](https://github.com/thomst/django-searchkit/)
|
52
49
|
[<img src="https://coveralls.io/repos/github/thomst/django-searchkit/badge.svg?branch=main">](https://coveralls.io/github/thomst/django-searchkit?branch=main)
|
53
|
-
[<img src="https://img.shields.io/badge/python-3.
|
54
|
-
[<img src="https://img.shields.io/badge/django-
|
50
|
+
[<img src="https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%203.11-blue">](https://img.shields.io/badge/python-3.9%20%7C%203.10%20%7C%203.11-blue)
|
51
|
+
[<img src="https://img.shields.io/badge/django-4.0%20%7C%204.1%20%7C%204.2%20%7C%205.0%20%7C%205.1%20%7C%205.2-orange">](https://img.shields.io/badge/django-4.0%20%7C%204.1%20%7C%204.2%20%7C%205.0%20%7C%205.1%20%7C%205.2-orange)
|
55
52
|
|
56
53
|
|
57
54
|
## Description
|
58
55
|
|
59
|
-
|
56
|
+
Finally there is a real searchkit application for django that integrates best
|
57
|
+
with the django admin backend.
|
58
|
+
|
59
|
+
Build and apply complex searches on model instances right in the backend without
|
60
|
+
any coding. Save and reuse your searches by a handy django admin filter with a
|
61
|
+
single click.
|
62
|
+
|
60
63
|
|
61
64
|
## Setup
|
62
65
|
|
@@ -73,10 +76,28 @@ INSTALLED_APPS = [
|
|
73
76
|
]
|
74
77
|
```
|
75
78
|
|
76
|
-
|
79
|
+
Add the `SearkitFilter` to your `ModelAdmin`:
|
80
|
+
```
|
81
|
+
from django.contrib import admin
|
82
|
+
from searchkit.filters import SearchkitFilter
|
83
|
+
from .models import MyModel
|
84
|
+
|
77
85
|
|
78
|
-
|
86
|
+
@admin.register(MyModel)
|
87
|
+
class MyModelAdmin(admin.ModelAdmin):
|
88
|
+
...
|
89
|
+
list_filter = [
|
90
|
+
SearchkitFilter,
|
91
|
+
...
|
92
|
+
]
|
93
|
+
...
|
94
|
+
```
|
79
95
|
|
80
96
|
## Usage
|
81
97
|
|
82
|
-
|
98
|
+
1. Open the admin changelist of your Model.
|
99
|
+
2. Click the "Add filter" button of the Searchkit filter.
|
100
|
+
3. Give your Filter a name.
|
101
|
+
4. Configure as many filter rules as you want.
|
102
|
+
5. Click "Save and apply".
|
103
|
+
6. Reuse your filter whenever you want using the Searchkit filter section.
|
@@ -35,6 +35,7 @@ searchkit/forms/search.py
|
|
35
35
|
searchkit/forms/searchkit.py
|
36
36
|
searchkit/forms/utils.py
|
37
37
|
searchkit/migrations/0001_initial.py
|
38
|
+
searchkit/migrations/0002_rename_searchkitsearch_search.py
|
38
39
|
searchkit/migrations/__init__.py
|
39
40
|
searchkit/templatetags/__init__.py
|
40
41
|
searchkit/templatetags/searchkit.py
|
@@ -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
|
10
|
+
list_filter = [SearchkitFilter]
|
11
11
|
|
12
12
|
|
13
13
|
@admin.register(ModelB)
|
@@ -1,14 +1,14 @@
|
|
1
1
|
from django.contrib import admin
|
2
2
|
from django.http import HttpResponseRedirect
|
3
3
|
from django.urls import reverse
|
4
|
-
from .models import
|
5
|
-
from .forms import
|
4
|
+
from .models import Search
|
5
|
+
from .forms import SearchForm
|
6
6
|
from .filters import SearchkitFilter
|
7
7
|
|
8
8
|
|
9
|
-
@admin.register(
|
9
|
+
@admin.register(Search)
|
10
10
|
class SearchkitSearchAdmin(admin.ModelAdmin):
|
11
|
-
form =
|
11
|
+
form = SearchForm
|
12
12
|
list_display = ('name', 'contenttype', 'created_date')
|
13
13
|
|
14
14
|
def get_url_for_applied_search(self, obj):
|
@@ -1,8 +1,6 @@
|
|
1
1
|
from django.contrib.admin import SimpleListFilter
|
2
2
|
from django.contrib.contenttypes.models import ContentType
|
3
|
-
from .models import
|
4
|
-
from .forms import SearchkitFormSet
|
5
|
-
from .forms.utils import get_filter_rules
|
3
|
+
from .models import Search
|
6
4
|
|
7
5
|
|
8
6
|
class SearchkitFilter(SimpleListFilter):
|
@@ -11,21 +9,19 @@ class SearchkitFilter(SimpleListFilter):
|
|
11
9
|
template = 'searchkit/searchkit_filter.html'
|
12
10
|
|
13
11
|
def __init__(self, request, params, model, model_admin):
|
14
|
-
# We need the app_label and
|
15
|
-
#
|
16
|
-
self.
|
17
|
-
self.model_name = model._meta.model_name
|
12
|
+
# We need the app_label and model as get parameter for the new search
|
13
|
+
# link.
|
14
|
+
self.searchkit_model = ContentType.objects.get_for_model(model)
|
18
15
|
super().__init__(request, params, model, model_admin)
|
19
16
|
|
20
17
|
def lookups(self, request, model_admin):
|
21
18
|
# Fetch the last three objects from SearchkitSearch and return them as
|
22
19
|
# choices.
|
23
|
-
|
24
|
-
searches = SearchkitSearch.objects.filter(contenttype=ct).order_by('-created_date')[:3]
|
20
|
+
searches = Search.objects.filter(contenttype=self.searchkit_model).order_by('-created_date')[:3]
|
25
21
|
return [(str(obj.id), obj.name) for obj in searches]
|
26
22
|
|
27
23
|
def queryset(self, request, queryset):
|
28
24
|
# Filter the queryset based on the selected SearchkitSearch object
|
29
25
|
if self.value():
|
30
|
-
search =
|
31
|
-
return
|
26
|
+
search = Search.objects.get(id=int(self.value()))
|
27
|
+
return search.as_queryset()
|
@@ -0,0 +1,62 @@
|
|
1
|
+
from django import forms
|
2
|
+
from django.utils.functional import cached_property
|
3
|
+
from django.contrib.contenttypes.models import ContentType
|
4
|
+
from ..models import Search
|
5
|
+
from .searchkit import SearchkitModelForm
|
6
|
+
from .searchkit import searchkit_formset_factory
|
7
|
+
from .utils import MediaMixin
|
8
|
+
|
9
|
+
|
10
|
+
class SearchForm(MediaMixin, forms.ModelForm):
|
11
|
+
"""
|
12
|
+
Represents a SearchkitSearch model. Using a SearchkitFormSet for the data
|
13
|
+
json field.
|
14
|
+
"""
|
15
|
+
class Meta:
|
16
|
+
model = Search
|
17
|
+
fields = ['name']
|
18
|
+
|
19
|
+
@cached_property
|
20
|
+
def searchkit_model(self):
|
21
|
+
if self.instance.pk:
|
22
|
+
return self.instance.contenttype.model_class()
|
23
|
+
elif self.searchkit_model_form.is_valid():
|
24
|
+
return self.searchkit_model_form.cleaned_data['searchkit_model'].model_class()
|
25
|
+
elif 'searchkit_model' in self.searchkit_model_form.initial:
|
26
|
+
value = self.searchkit_model_form.initial['searchkit_model']
|
27
|
+
try:
|
28
|
+
return self.searchkit_model_form.fields['searchkit_model'].clean(value).model_class()
|
29
|
+
except forms.ValidationError:
|
30
|
+
return None
|
31
|
+
|
32
|
+
@cached_property
|
33
|
+
def searchkit_model_form(self):
|
34
|
+
kwargs = dict(data=self.data or None, initial=self.initial or None)
|
35
|
+
if self.instance.pk:
|
36
|
+
kwargs['initial'] = dict(searchkit_model=self.instance.contenttype)
|
37
|
+
return SearchkitModelForm(**kwargs)
|
38
|
+
|
39
|
+
@cached_property
|
40
|
+
def formset(self):
|
41
|
+
"""
|
42
|
+
A searchkit formset for the model.
|
43
|
+
"""
|
44
|
+
kwargs = dict()
|
45
|
+
if self.searchkit_model and self.data:
|
46
|
+
kwargs = dict(data=self.data)
|
47
|
+
elif self.searchkit_model and self.instance.pk:
|
48
|
+
kwargs = dict(initial=self.instance.data)
|
49
|
+
|
50
|
+
extra = 0 if self.instance.pk else 1
|
51
|
+
formset = searchkit_formset_factory(model=self.searchkit_model, extra=extra)
|
52
|
+
return formset(**kwargs)
|
53
|
+
|
54
|
+
def is_valid(self):
|
55
|
+
return self.formset.is_valid() and self.searchkit_model_form.is_valid and super().is_valid()
|
56
|
+
|
57
|
+
def clean(self):
|
58
|
+
if self.searchkit_model_form.is_valid():
|
59
|
+
self.instance.contenttype = self.searchkit_model_form.cleaned_data['searchkit_model']
|
60
|
+
if self.formset.is_valid():
|
61
|
+
self.instance.data = self.formset.cleaned_data
|
62
|
+
return super().clean()
|
@@ -0,0 +1,154 @@
|
|
1
|
+
from django import forms
|
2
|
+
from django.utils.translation import gettext_lazy as _
|
3
|
+
from django.utils.functional import cached_property
|
4
|
+
from .utils import CssClassMixin, FIELD_PLAN, OPERATOR_DESCRIPTION
|
5
|
+
from .utils import SUPPORTED_FIELDS
|
6
|
+
from .utils import ModelTree
|
7
|
+
from .utils import MediaMixin
|
8
|
+
from .utils import get_searchable_models
|
9
|
+
|
10
|
+
|
11
|
+
class SearchkitModelForm(forms.Form):
|
12
|
+
"""
|
13
|
+
Form to select a content type.
|
14
|
+
"""
|
15
|
+
searchkit_model = forms.ModelChoiceField(
|
16
|
+
queryset=get_searchable_models(),
|
17
|
+
label=_('Model'),
|
18
|
+
empty_label=_('Select a Model'),
|
19
|
+
widget=forms.Select(attrs={
|
20
|
+
"class": CssClassMixin.reload_on_change_css_class,
|
21
|
+
"data-total-forms": 1,
|
22
|
+
}),
|
23
|
+
)
|
24
|
+
|
25
|
+
|
26
|
+
class BaseSearchkitForm(MediaMixin, CssClassMixin, forms.Form):
|
27
|
+
"""
|
28
|
+
Searchkit form representing a model field lookup based on the field name,
|
29
|
+
the operator and one or two values.
|
30
|
+
|
31
|
+
The unbound form is composed of an index field (the count of the searchkit
|
32
|
+
form) and a choice field offering the names of the model fields.
|
33
|
+
|
34
|
+
The bound form is dynamically extended by the operator field or the operator and
|
35
|
+
the value field depending on the provided data
|
36
|
+
|
37
|
+
See the FIELD_PLAN variable for the logic of building the form.
|
38
|
+
"""
|
39
|
+
model = None # Set by the formset factory.
|
40
|
+
|
41
|
+
def __init__(self, *args, **kwargs):
|
42
|
+
super().__init__(*args, **kwargs)
|
43
|
+
self.model_tree = ModelTree(self.model)
|
44
|
+
self.model_field = None
|
45
|
+
self.field_plan = None
|
46
|
+
self.operator = None
|
47
|
+
self._add_field_name_field()
|
48
|
+
lookup = self._preload_clean_data('field')
|
49
|
+
self.model_field = self._get_model_field(lookup)
|
50
|
+
self.field_plan = next(iter([p for t, p in FIELD_PLAN.items() if t(self.model_field)]))
|
51
|
+
self._add_operator_field()
|
52
|
+
self.operator = self._preload_clean_data('operator')
|
53
|
+
self._add_value_field()
|
54
|
+
|
55
|
+
@cached_property
|
56
|
+
def unprefixed_data(self):
|
57
|
+
data = dict()
|
58
|
+
for key, value in self.data.items():
|
59
|
+
if key.startswith(self.prefix):
|
60
|
+
data[key[len(self.prefix) + 1:]] = value
|
61
|
+
return data
|
62
|
+
|
63
|
+
def _preload_clean_data(self, field_name):
|
64
|
+
# Try the initial value first since it is already cleaned.
|
65
|
+
if self.initial and field_name in self.initial:
|
66
|
+
return self.initial[field_name]
|
67
|
+
# Otherwise look up the data dict.
|
68
|
+
elif field_name in self.unprefixed_data:
|
69
|
+
try:
|
70
|
+
# Do we have a valid value?
|
71
|
+
return self.fields[field_name].clean(self.unprefixed_data[field_name])
|
72
|
+
except forms.ValidationError:
|
73
|
+
return self.fields[field_name].choices[0][0]
|
74
|
+
else:
|
75
|
+
# At last simply return the first option which will be the selected
|
76
|
+
# one.
|
77
|
+
return self.fields[field_name].choices[0][0]
|
78
|
+
|
79
|
+
def _get_model_field(self, lookup):
|
80
|
+
path = lookup.split('__')
|
81
|
+
field_name = path[-1]
|
82
|
+
if path[:-1]:
|
83
|
+
model = self.model_tree.get('__'.join(path[:-1])).model
|
84
|
+
else:
|
85
|
+
model = self.model
|
86
|
+
return model._meta.get_field(field_name)
|
87
|
+
|
88
|
+
def _get_model_field_choices(self):
|
89
|
+
choices = list()
|
90
|
+
label_path = list()
|
91
|
+
for node in self.model_tree.iterate():
|
92
|
+
label_path.append
|
93
|
+
for model_field in node.model._meta.fields:
|
94
|
+
if not any(isinstance(model_field, f) for f in SUPPORTED_FIELDS):
|
95
|
+
continue
|
96
|
+
if node.is_root:
|
97
|
+
lookup = model_field.name
|
98
|
+
label = f'`{model_field.verbose_name}`'
|
99
|
+
else:
|
100
|
+
lookup = f'{node.field_path}__{model_field.name}'
|
101
|
+
get_field_name = lambda f: getattr(f, 'verbose_name', f.name)
|
102
|
+
label_path = [f'`{get_field_name(n.field)}` => <{n.model._meta.verbose_name}>' for n in node.path[1:]]
|
103
|
+
label = ".".join(label_path + [f'`{model_field.verbose_name}`'])
|
104
|
+
choices.append((lookup, label))
|
105
|
+
return choices
|
106
|
+
|
107
|
+
def _add_field_name_field(self):
|
108
|
+
initial = self.initial.get('field')
|
109
|
+
choices = self._get_model_field_choices()
|
110
|
+
field = forms.ChoiceField(label=_('Model field'), choices=choices, initial=initial)
|
111
|
+
field.widget.attrs.update({"class": self.reload_on_change_css_class})
|
112
|
+
self.fields['field'] = field
|
113
|
+
|
114
|
+
def _add_operator_field(self):
|
115
|
+
initial = self.initial.get('operator')
|
116
|
+
choices = [(o, OPERATOR_DESCRIPTION[o]) for o in self.field_plan.keys()]
|
117
|
+
field = forms.ChoiceField(label=_('Operator'), choices=choices, initial=initial)
|
118
|
+
field.widget.attrs.update({"class": self.reload_on_change_css_class})
|
119
|
+
self.fields['operator'] = field
|
120
|
+
|
121
|
+
def _add_value_field(self):
|
122
|
+
initial = self.initial.get('value')
|
123
|
+
field_class = self.field_plan[self.operator][0]
|
124
|
+
if getattr(field_class, 'choices', None) and getattr(self.model_field, 'choices', None):
|
125
|
+
field = field_class(choices=self.model_field.choices, initial=initial)
|
126
|
+
else:
|
127
|
+
field = field_class()
|
128
|
+
self.fields['value'] = field
|
129
|
+
|
130
|
+
|
131
|
+
class BaseSearchkitFormSet(CssClassMixin, forms.BaseFormSet):
|
132
|
+
"""
|
133
|
+
Formset holding all searchkit forms.
|
134
|
+
"""
|
135
|
+
template_name = "searchkit/searchkit.html"
|
136
|
+
template_name_div = "searchkit/searchkit.html"
|
137
|
+
model = None # Set by the formset factory.
|
138
|
+
|
139
|
+
def add_prefix(self, index):
|
140
|
+
return "%s-%s-%s-%s" % (self.prefix, self.model._meta.app_label, self.model._meta.model_name, index)
|
141
|
+
|
142
|
+
@classmethod
|
143
|
+
def get_default_prefix(self):
|
144
|
+
return "searchkit"
|
145
|
+
|
146
|
+
|
147
|
+
def searchkit_formset_factory(model, **kwargs):
|
148
|
+
form = type('SearchkitForm', (BaseSearchkitForm,), dict(model=model))
|
149
|
+
formset = type('SearchkitFormSet', (BaseSearchkitFormSet,), dict(model=model))
|
150
|
+
return forms.formset_factory(
|
151
|
+
form=form,
|
152
|
+
formset=formset,
|
153
|
+
**kwargs
|
154
|
+
)
|
@@ -1,8 +1,23 @@
|
|
1
|
+
from modeltree import ModelTree as BaseModelTree
|
2
|
+
from collections import OrderedDict
|
3
|
+
from django.apps import apps
|
4
|
+
from django.contrib import admin
|
5
|
+
from django.contrib.contenttypes.models import ContentType
|
1
6
|
from django.db import models
|
7
|
+
from django.db.utils import OperationalError
|
2
8
|
from django import forms
|
3
9
|
from django.utils.translation import gettext_lazy as _
|
4
|
-
from
|
5
|
-
from
|
10
|
+
from ..filters import SearchkitFilter
|
11
|
+
from . import fields as searchkit_fields
|
12
|
+
|
13
|
+
|
14
|
+
class ModelTree(BaseModelTree):
|
15
|
+
MAX_DEPTH = 3
|
16
|
+
FOLLOW_ACROSS_APPS = True
|
17
|
+
RELATION_TYPES = [
|
18
|
+
'one_to_one',
|
19
|
+
'many_to_one',
|
20
|
+
]
|
6
21
|
|
7
22
|
|
8
23
|
OPERATOR_DESCRIPTION = {
|
@@ -29,10 +44,6 @@ SUPPORTED_FIELDS = [
|
|
29
44
|
models.DateField,
|
30
45
|
models.DateTimeField,
|
31
46
|
]
|
32
|
-
SUPPORTED_RELATIONS = [
|
33
|
-
models.ForeignKey,
|
34
|
-
models.OneToOneField,
|
35
|
-
]
|
36
47
|
|
37
48
|
|
38
49
|
FIELD_PLAN = OrderedDict((
|
@@ -132,18 +143,36 @@ FIELD_PLAN = OrderedDict((
|
|
132
143
|
))
|
133
144
|
|
134
145
|
|
135
|
-
class
|
146
|
+
class CssClassMixin:
|
136
147
|
reload_on_change_css_class = "searchkit-reload-on-change"
|
137
148
|
reload_on_click_css_class = "searchkit-reload-on-click"
|
138
149
|
|
139
150
|
|
140
|
-
|
151
|
+
class MediaMixin:
|
152
|
+
class Media:
|
153
|
+
js = [
|
154
|
+
'admin/js/vendor/jquery/jquery.min.js',
|
155
|
+
'admin/js/jquery.init.js',
|
156
|
+
"searchkit/searchkit.js"
|
157
|
+
]
|
158
|
+
|
159
|
+
|
160
|
+
def is_searchable_model(model):
|
161
|
+
"""
|
162
|
+
Check if the model is searchable by Searchkit.
|
163
|
+
"""
|
164
|
+
return admin.site.is_registered(model) and SearchkitFilter in admin.site._registry[model].list_filter
|
165
|
+
|
166
|
+
|
167
|
+
def get_searchable_models():
|
141
168
|
"""
|
142
|
-
|
143
|
-
:param formset: Formset to extract filter rules from.
|
144
|
-
:return: OrderedDict with filter rule pairs: field__operator: value
|
169
|
+
Return a queryset of searchable models.
|
145
170
|
"""
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
171
|
+
# Before mirating the database we get an OperationalError when trying to
|
172
|
+
# access ContentType database table.
|
173
|
+
try:
|
174
|
+
models = [m for m in apps.get_models() if is_searchable_model(m)]
|
175
|
+
ids = [ContentType.objects.get_for_model(m).id for m in models]
|
176
|
+
return ContentType.objects.filter(pk__in=ids).order_by('app_label', 'model')
|
177
|
+
except OperationalError:
|
178
|
+
return ContentType.objects.all()
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# Generated by Django 5.1.3 on 2025-06-07 05:46
|
2
|
+
|
3
|
+
from django.db import migrations
|
4
|
+
|
5
|
+
|
6
|
+
class Migration(migrations.Migration):
|
7
|
+
|
8
|
+
dependencies = [
|
9
|
+
('contenttypes', '0002_remove_content_type_name'),
|
10
|
+
('searchkit', '0001_initial'),
|
11
|
+
]
|
12
|
+
|
13
|
+
operations = [
|
14
|
+
migrations.RenameModel(
|
15
|
+
old_name='SearchkitSearch',
|
16
|
+
new_name='Search',
|
17
|
+
),
|
18
|
+
]
|