Add extra button functionality to list and detail views

This commit is contained in:
Mark Veidemanis 2023-03-09 12:08:21 +00:00
parent 3d7c3504aa
commit 96dc21019b
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
3 changed files with 43 additions and 0 deletions

View File

@ -39,6 +39,23 @@
</span> </span>
</button> </button>
{% endif %} {% endif %}
{% for button in extra_buttons %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{{ button.url }}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
{% if button.confirm %}hx-confirm="Are you sure you wish to {{ button.action }}?"{% endif %}
class="button">
<span class="icon-text">
<span class="icon">
<i class="{{ button.icon }}"></i>
</span>
<span>{{ button.label }}</span>
</span>
</button>
{% endfor %}
</div> </div>
{% include detail_template %} {% include detail_template %}

View File

@ -39,6 +39,23 @@
</span> </span>
</button> </button>
{% endif %} {% endif %}
{% for button in extra_buttons %}
<button
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
hx-delete="{{ button.url }}"
hx-trigger="click"
hx-target="#modals-here"
hx-swap="innerHTML"
{% if button.confirm %}hx-confirm="Are you sure you wish to {{ button.action }}?"{% endif %}
class="button">
<span class="icon-text">
<span class="icon">
<i class="{{ button.icon }}"></i>
</span>
<span>{{ button.label }}</span>
</span>
</button>
{% endfor %}
</div> </div>
{% include list_template %} {% include list_template %}

View File

@ -50,6 +50,8 @@ class ObjectList(RestrictedViewMixin, ObjectNameMixin, ListView):
delete_all_url_name = None delete_all_url_name = None
widget_options = None widget_options = None
extra_buttons = None
def queryset_mutate(self, queryset): def queryset_mutate(self, queryset):
pass pass
@ -130,6 +132,8 @@ class ObjectList(RestrictedViewMixin, ObjectNameMixin, ListView):
context["delete_all_url"] = reverse(self.delete_all_url_name) context["delete_all_url"] = reverse(self.delete_all_url_name)
if self.widget_options: if self.widget_options:
context["widget_options"] = self.widget_options context["widget_options"] = self.widget_options
if self.extra_buttons is not None:
context["extra_buttons"] = self.extra_buttons
# Return partials for HTMX # Return partials for HTMX
if self.request.htmx: if self.request.htmx:
@ -261,6 +265,8 @@ class ObjectRead(RestrictedViewMixin, ObjectNameMixin, DetailView):
request = None request = None
extra_buttons = None
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
type = kwargs.get("type", None) type = kwargs.get("type", None)
if not type: if not type:
@ -306,6 +312,9 @@ class ObjectRead(RestrictedViewMixin, ObjectNameMixin, DetailView):
self.detail_url_name, kwargs=detail_url_args self.detail_url_name, kwargs=detail_url_args
) )
if self.extra_buttons is not None:
context["extra_buttons"] = self.extra_buttons
# Return partials for HTMX # Return partials for HTMX
if self.request.htmx: if self.request.htmx:
if request.headers["HX-Target"] == self.context_object_name + "-info": if request.headers["HX-Target"] == self.context_object_name + "-info":