Skip to content

Commit 5bc7baf

Browse files
committed
Rename list attrs and use self.filter
1 parent 212c47d commit 5bc7baf

File tree

5 files changed

+33
-27
lines changed

5 files changed

+33
-27
lines changed

bolt-admin/bolt/admin/templates/admin/list.html

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@
1414
Objects ({{ page.paginator.count }})
1515
</div>
1616
<div class="flex space-x-5">
17-
{% if list_filters %}
17+
{% if filters %}
1818
<form method="GET" class="inline-flex">
1919
<select data-autosubmit name="filter" class="text-sm border-gray-200 rounded-md">
2020
<option value="">Filters</option>
21-
{% for filter in list_filters %}
22-
<option {% if filter == list_filter %}selected{% endif %}>{{ filter }}</option>
21+
{% for filter in filters %}
22+
<option {% if filter == active_filter %}selected{% endif %}>{{ filter }}</option>
2323
{% endfor %}
2424
</select>
2525
</form>
2626
{% endif %}
2727

28-
{% if list_actions %}
28+
{% if actions %}
2929
<form method="POST" data-actions-form>
3030
{{ csrf_input }}
3131
<select name="action_key">
3232
<option value="">Actions</option>
33-
{% for action in list_actions %}
33+
{% for action in actions %}
3434
<option>{{ action }}</option>
3535
{% endfor %}
3636
</select>
@@ -69,9 +69,9 @@
6969
<table class="w-full mt-6 text-sm table-auto">
7070
<thead>
7171
<tr class="bg-stone-100 [&>th]:py-2 [&>:first-child]:rounded-l-md [&>:last-child]:rounded-r-md">
72-
{% if list_actions %}<th></th>{% endif %}
72+
{% if actions %}<th></th>{% endif %}
7373

74-
{% for field in list_fields %}
74+
{% for field in fields %}
7575
{% if order_by_field is defined %}
7676
<th>
7777
<a class="font-mono text-xs font-normal text-gray-600" href="?order_by={{ '-' if not order_by_direction else '' }}{{ field }}">
@@ -97,13 +97,13 @@
9797
{% for object in objects %}
9898
<tr>
9999

100-
{% if list_actions %}
100+
{% if actions %}
101101
<td class="p-0 pl-1">
102102
<input data-action-checkbox class="rounded-sm" type="checkbox" name="{{ get_object_pk(object) }}" />
103103
</td>
104104
{% endif %}
105105

106-
{% for field in list_fields %}
106+
{% for field in fields %}
107107
<td>
108108
<div class="flex">
109109
{% set value = get_object_field(object, field) %}

bolt-admin/bolt/admin/views/base.py

+20-14
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,28 @@ def render_card(self, card: "Card"):
117117

118118
class AdminListView(AdminView):
119119
template_name = "admin/list.html"
120-
list_fields: list
121-
list_actions: dict[str] = {}
122-
list_filters: list[str] = []
120+
fields: list
121+
actions: dict[str] = {}
122+
filters: list[str] = []
123123
page_size = 100
124124
show_search = False
125125

126126
def get_context(self):
127127
context = super().get_context()
128128

129-
list_filter = self.request.GET.get("filter", "")
129+
# Make this available on self for usage in get_objects and other methods
130+
self.filter = self.request.GET.get("filter", "")
130131

131132
objects = self.get_objects()
132-
objects = self.filter_objects(list_filter, objects)
133133

134134
context["paginator"] = Paginator(objects, self.page_size)
135135
context["page"] = context["paginator"].get_page(self.request.GET.get("page", 1))
136136
context["objects"] = context["page"] # alias
137-
context["list_fields"] = self.list_fields
138-
context["list_actions"] = self.list_actions
137+
context["fields"] = self.get_fields()
138+
context["actions"] = self.get_actions()
139+
context["filters"] = self.get_filters()
139140

140-
context["list_filters"] = self.list_filters
141-
context["list_filter"] = list_filter
141+
context["active_filter"] = self.filter
142142

143143
# Implement search yourself in get_objects
144144
context["search_query"] = self.request.GET.get("search", "")
@@ -155,8 +155,9 @@ def get_context(self):
155155

156156
def post(self) -> HttpResponse:
157157
action_key = self.request.POST.get("action_key")
158-
if action_key and action_key in self.list_actions:
159-
action_callable = self.list_actions[action_key]
158+
actions = self.get_actions()
159+
if action_key and action_key in actions:
160+
action_callable = actions[action_key]
160161
if isinstance(action_callable, str):
161162
action_callable = getattr(self, action_callable)
162163
return action_callable(self.request.POST.getlist("action_pks"))
@@ -166,9 +167,14 @@ def post(self) -> HttpResponse:
166167
def get_objects(self) -> list:
167168
return []
168169

169-
def filter_objects(self, filter_name: str, objects: list):
170-
"""Implement custom object filters here by looking at filter name"""
171-
return objects
170+
def get_fields(self) -> list:
171+
return self.fields
172+
173+
def get_actions(self) -> dict[str]:
174+
return self.actions
175+
176+
def get_filters(self) -> list[str]:
177+
return self.filters
172178

173179
def get_object_field(self, obj, field: str):
174180
# Try basic dict lookup first

bolt-admin/bolt/admin/views/models.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class AdminModelListView(AdminListView):
3939

4040
model: "models.Model"
4141

42-
list_fields: list = ["pk"]
42+
fields: list = ["pk"]
4343
list_order = []
4444
search_fields: list = ["pk"]
4545

bolt-cache/bolt/cache/admin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class CachedItemViewset(AdminModelViewset):
1212
class ListView(AdminModelListView):
1313
nav_section = "Cache"
1414
model = CachedItem
15-
list_fields = [
15+
fields = [
1616
"key",
1717
"created_at",
1818
"expires_at",

bolt-flags/bolt/flags/admin.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_text(self):
3131
class FlagAdmin(AdminModelViewset):
3232
class ListView(AdminModelListView):
3333
model = Flag
34-
list_fields = ["name", "enabled", "created_at__date", "used_at__date", "uuid"]
34+
fields = ["name", "enabled", "created_at__date", "used_at__date", "uuid"]
3535
search_fields = ["name", "description"]
3636
cards = [UnusedFlagsCard]
3737
nav_section = "Feature flags"
@@ -50,7 +50,7 @@ class Meta:
5050
class FlagResultAdmin(AdminModelViewset):
5151
class ListView(AdminModelListView):
5252
model = FlagResult
53-
list_fields = [
53+
fields = [
5454
"flag",
5555
"key",
5656
"value",

0 commit comments

Comments
 (0)