-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbase.py
78 lines (56 loc) · 2.36 KB
/
base.py
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
"""Base class for lib filters."""
from django.contrib import admin
class Base:
"""Mixin class for filters with title, apply button and collapsed state."""
parameter_name = 'filter'
title = None
FILTER_LABEL = "Admin filter"
BUTTON_LABEL = "Apply"
def set_title(self):
"""Init title values."""
self.title = {
'parameter_name': self.parameter_name,
'filter_name': self.FILTER_LABEL,
'button_label': self.BUTTON_LABEL,
}
class Filter(admin.FieldListFilter, Base):
"""Base class for filters applied to field with title, apply button and collapsed state."""
parameter_name_mask = 'adminfilter_'
def __init__(self, field, request, params, model, model_admin, field_path):
"""Customize FieldListFilter functionality."""
self.parameter_name = self.parameter_name_mask + field_path
super().__init__(field, request, params, model, model_admin, field_path)
self.set_title()
def get_facet_counts(self, _pk_attname, _filtered_qs):
"""Django5 amin site Facets.
https://docs.djangoproject.com/en/5.0/ref/contrib/admin/filters/#facet-filters
"""
return {}
def value(self):
"""Return the string provided in the request's query string.
None if the value wasn't provided.
"""
val = self.used_parameters.get(self.parameter_name)
if isinstance(val, list):
val = val[0]
return val
def expected_parameters(self):
"""Parameter list for chice filter."""
return [self.parameter_name]
def choices(self, changelist):
"""Must be implemented in childs."""
raise NotImplementedError('Method choices')
class FilterSimple(admin.SimpleListFilter, Base):
"""Base class for filters without field with title, apply button and collapsed state."""
parameter_name = 'adminfilter'
title = 'Filter'
def __init__(self, request, params, model, model_admin):
"""Combine parents init."""
super().__init__(request, params, model, model_admin)
self.set_title()
def lookups(self, request, model_admin):
"""Must be implemented in childs."""
raise NotImplementedError('Method lookups')
def queryset(self, request, queryset):
"""Must be implemented in childs."""
raise NotImplementedError('Method queryset')