Collapse into a single template tag

This commit is contained in:
Jeremy Stretch 2025-06-27 15:14:29 -04:00
parent 57e041fd1a
commit 63fd6ea1c5
8 changed files with 23 additions and 44 deletions

View File

@ -91,7 +91,8 @@ class BulkExport(ObjectAction):
permissions_required = {'view'} permissions_required = {'view'}
template_name = 'buttons/export.html' template_name = 'buttons/export.html'
def get_context(self, context, model): @classmethod
def get_context(cls, context, model):
object_type = ObjectType.objects.get_for_model(model) object_type = ObjectType.objects.get_for_model(model)
user = context['request'].user user = context['request'].user

View File

@ -152,7 +152,7 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
# Determine the available actions # Determine the available actions
actions = self.get_permitted_actions(request.user) actions = self.get_permitted_actions(request.user)
has_bulk_actions = any(action.bulk for action in actions.values()) has_bulk_actions = any(action.bulk for action in actions)
if 'export' in request.GET: if 'export' in request.GET:

View File

@ -59,9 +59,7 @@ class ActionsMixin:
if not required_permissions or user.has_perms(required_permissions): if not required_permissions or user.has_perms(required_permissions):
permitted_actions.append(action) permitted_actions.append(action)
return { return permitted_actions
action.name: action for action in permitted_actions
}
class TableMixin: class TableMixin:

View File

@ -143,7 +143,7 @@ class ObjectChildrenView(ObjectView, ActionsMixin, TableMixin):
# Determine the available actions # Determine the available actions
actions = self.get_permitted_actions(request.user, model=self.child_model) actions = self.get_permitted_actions(request.user, model=self.child_model)
has_bulk_actions = any(action.bulk for action in actions.values()) has_bulk_actions = any(action.bulk for action in actions)
table_data = self.prep_table_data(request, child_objects, instance) table_data = self.prep_table_data(request, child_objects, instance)
table = self.get_table(table_data, request, has_bulk_actions) table = self.get_table(table_data, request, has_bulk_actions)

View File

@ -83,9 +83,7 @@ Context:
{% if request.user|can_add:object %} {% if request.user|can_add:object %}
{% clone_button object %} {% clone_button object %}
{% endif %} {% endif %}
{% for name, action in actions.items %} {% action_buttons actions object %}
{% action_button action object %}
{% endfor %}
{% endblock control-buttons %} {% endblock control-buttons %}
</div> </div>

View File

@ -37,9 +37,7 @@ Context:
</div> </div>
<div class="d-print-none mt-2"> <div class="d-print-none mt-2">
{% block bulk_controls %} {% block bulk_controls %}
{% for name, action in actions.items %} {% action_buttons actions model bulk=True %}
{% bulk_action_button action model %}
{% endfor %}
{% block bulk_extra_controls %}{% endblock %} {% block bulk_extra_controls %}{% endblock %}
{% endblock bulk_controls %} {% endblock bulk_controls %}
</div> </div>

View File

@ -32,11 +32,7 @@ Context:
<div class="btn-list"> <div class="btn-list">
{% plugin_list_buttons model %} {% plugin_list_buttons model %}
{% block extra_controls %}{% endblock %} {% block extra_controls %}{% endblock %}
{% for name, action in actions.items %} {% action_buttons actions model %}
{% if not action.bulk %}
{% action_button action model %}
{% endif %}
{% endfor %}
</div> </div>
{% endblock controls %} {% endblock controls %}
@ -88,9 +84,7 @@ Context:
</label> </label>
</div> </div>
<div class="bulk-action-buttons"> <div class="bulk-action-buttons">
{% for name, action in actions.items %} {% action_buttons actions model bulk=True %}
{% bulk_action_button action model %}
{% endfor %}
</div> </div>
</div> </div>
</div> </div>
@ -118,13 +112,8 @@ Context:
<div class="btn-list d-print-none"> <div class="btn-list d-print-none">
{% block bulk_buttons %} {% block bulk_buttons %}
<div class="bulk-action-buttons"> <div class="bulk-action-buttons">
{# Extra bulk buttons #}
{% block extra_bulk_buttons %}{% endblock %} {% block extra_bulk_buttons %}{% endblock %}
{% action_buttons actions model bulk=True %}
{# Default bulk action buttons #}
{% for name, action in actions.items %}
{% bulk_action_button action model %}
{% endfor %}
</div> </div>
{% endblock %} {% endblock %}
</div> </div>

View File

@ -2,6 +2,7 @@ from django import template
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.template import loader from django.template import loader
from django.urls import NoReverseMatch, reverse from django.urls import NoReverseMatch, reverse
from django.utils.safestring import mark_safe
from core.models import ObjectType from core.models import ObjectType
from extras.models import Bookmark, ExportTemplate, Subscription from extras.models import Bookmark, ExportTemplate, Subscription
@ -10,10 +11,9 @@ from utilities.querydict import prepare_cloned_fields
from utilities.views import get_viewname from utilities.views import get_viewname
__all__ = ( __all__ = (
'action_button', 'action_buttons',
'add_button', 'add_button',
'bookmark_button', 'bookmark_button',
'bulk_action_button',
'bulk_delete_button', 'bulk_delete_button',
'bulk_edit_button', 'bulk_edit_button',
'clone_button', 'clone_button',
@ -28,8 +28,17 @@ __all__ = (
register = template.Library() register = template.Library()
@register.simple_tag(takes_context=True)
def action_buttons(context, actions, obj, bulk=False):
buttons = [
loader.render_to_string(action.template_name, action.get_context(context, obj))
for action in actions if action.bulk == bulk
]
return mark_safe(''.join(buttons))
# #
# Instance buttons # Legacy object buttons
# #
@register.inclusion_tag('buttons/bookmark.html', takes_context=True) @register.inclusion_tag('buttons/bookmark.html', takes_context=True)
@ -145,7 +154,7 @@ def sync_button(instance):
# #
# List buttons # Legacy list buttons
# #
@register.inclusion_tag('buttons/add.html') @register.inclusion_tag('buttons/add.html')
@ -220,17 +229,3 @@ def bulk_delete_button(context, model, action='bulk_delete', query_params=None):
'htmx_navigation': context.get('htmx_navigation'), 'htmx_navigation': context.get('htmx_navigation'),
'url': url, 'url': url,
} }
@register.simple_tag(takes_context=True)
def action_button(context, action, obj):
if action.bulk:
return ''
return loader.render_to_string(action.template_name, action.get_context(context, obj))
@register.simple_tag(takes_context=True)
def bulk_action_button(context, action, model):
if not action.bulk:
return ''
return loader.render_to_string(action.template_name, action.get_context(context, model))