Skip to content

Commit

Permalink
Rename list attrs and use self.filter
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jan 10, 2024
1 parent 212c47d commit 5bc7baf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
18 changes: 9 additions & 9 deletions bolt-admin/bolt/admin/templates/admin/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@
Objects ({{ page.paginator.count }})
</div>
<div class="flex space-x-5">
{% if list_filters %}
{% if filters %}
<form method="GET" class="inline-flex">
<select data-autosubmit name="filter" class="text-sm border-gray-200 rounded-md">
<option value="">Filters</option>
{% for filter in list_filters %}
<option {% if filter == list_filter %}selected{% endif %}>{{ filter }}</option>
{% for filter in filters %}
<option {% if filter == active_filter %}selected{% endif %}>{{ filter }}</option>
{% endfor %}
</select>
</form>
{% endif %}

{% if list_actions %}
{% if actions %}
<form method="POST" data-actions-form>
{{ csrf_input }}
<select name="action_key">
<option value="">Actions</option>
{% for action in list_actions %}
{% for action in actions %}
<option>{{ action }}</option>
{% endfor %}
</select>
Expand Down Expand Up @@ -69,9 +69,9 @@
<table class="w-full mt-6 text-sm table-auto">
<thead>
<tr class="bg-stone-100 [&>th]:py-2 [&>:first-child]:rounded-l-md [&>:last-child]:rounded-r-md">
{% if list_actions %}<th></th>{% endif %}
{% if actions %}<th></th>{% endif %}

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

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

{% for field in list_fields %}
{% for field in fields %}
<td>
<div class="flex">
{% set value = get_object_field(object, field) %}
Expand Down
34 changes: 20 additions & 14 deletions bolt-admin/bolt/admin/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,28 @@ def render_card(self, card: "Card"):

class AdminListView(AdminView):
template_name = "admin/list.html"
list_fields: list
list_actions: dict[str] = {}
list_filters: list[str] = []
fields: list
actions: dict[str] = {}
filters: list[str] = []
page_size = 100
show_search = False

def get_context(self):
context = super().get_context()

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

objects = self.get_objects()
objects = self.filter_objects(list_filter, objects)

context["paginator"] = Paginator(objects, self.page_size)
context["page"] = context["paginator"].get_page(self.request.GET.get("page", 1))
context["objects"] = context["page"] # alias
context["list_fields"] = self.list_fields
context["list_actions"] = self.list_actions
context["fields"] = self.get_fields()
context["actions"] = self.get_actions()
context["filters"] = self.get_filters()

context["list_filters"] = self.list_filters
context["list_filter"] = list_filter
context["active_filter"] = self.filter

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

def post(self) -> HttpResponse:
action_key = self.request.POST.get("action_key")
if action_key and action_key in self.list_actions:
action_callable = self.list_actions[action_key]
actions = self.get_actions()
if action_key and action_key in actions:
action_callable = actions[action_key]
if isinstance(action_callable, str):
action_callable = getattr(self, action_callable)
return action_callable(self.request.POST.getlist("action_pks"))
Expand All @@ -166,9 +167,14 @@ def post(self) -> HttpResponse:
def get_objects(self) -> list:
return []

def filter_objects(self, filter_name: str, objects: list):
"""Implement custom object filters here by looking at filter name"""
return objects
def get_fields(self) -> list:
return self.fields

def get_actions(self) -> dict[str]:
return self.actions

def get_filters(self) -> list[str]:
return self.filters

def get_object_field(self, obj, field: str):
# Try basic dict lookup first
Expand Down
2 changes: 1 addition & 1 deletion bolt-admin/bolt/admin/views/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AdminModelListView(AdminListView):

model: "models.Model"

list_fields: list = ["pk"]
fields: list = ["pk"]
list_order = []
search_fields: list = ["pk"]

Expand Down
2 changes: 1 addition & 1 deletion bolt-cache/bolt/cache/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CachedItemViewset(AdminModelViewset):
class ListView(AdminModelListView):
nav_section = "Cache"
model = CachedItem
list_fields = [
fields = [
"key",
"created_at",
"expires_at",
Expand Down
4 changes: 2 additions & 2 deletions bolt-flags/bolt/flags/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def get_text(self):
class FlagAdmin(AdminModelViewset):
class ListView(AdminModelListView):
model = Flag
list_fields = ["name", "enabled", "created_at__date", "used_at__date", "uuid"]
fields = ["name", "enabled", "created_at__date", "used_at__date", "uuid"]
search_fields = ["name", "description"]
cards = [UnusedFlagsCard]
nav_section = "Feature flags"
Expand All @@ -50,7 +50,7 @@ class Meta:
class FlagResultAdmin(AdminModelViewset):
class ListView(AdminModelListView):
model = FlagResult
list_fields = [
fields = [
"flag",
"key",
"value",
Expand Down

0 comments on commit 5bc7baf

Please sign in to comment.