Skip to content

Commit

Permalink
Adjust action_links to return dict and move rendering to partial (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas authored Jan 10, 2024
1 parent ae24b45 commit 0d5fe12
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/neapolitan/templates/neapolitan/partial/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
">{{ field.value }}</td>
{% endfor %}
<td class="py-3.5 px-3 text-right text-sm font-medium [&_a]:text-indigo-600 [&_a:hover]:text-indigo-900">
{{ object.actions }}
{% for action in object.actions %}
<a href="{{ action.url }}">{{ action.text }}</a>{% if not forloop.last %} | {% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
Expand Down
23 changes: 15 additions & 8 deletions src/neapolitan/templatetags/neapolitan.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from django import template
from django.urls import reverse
from django.utils.safestring import mark_safe

register = template.Library()


def action_links(object):
model_name = object._meta.model_name
actions = [
(reverse(f"{model_name}-detail", kwargs={"pk": object.pk}), "View"),
(reverse(f"{model_name}-update", kwargs={"pk": object.pk}), "Edit"),
(reverse(f"{model_name}-delete", kwargs={"pk": object.pk}), "Delete"),
]
links = [f"<a href='{url}'>{anchor_text}</a>" for url, anchor_text in actions]
return mark_safe(" | ".join(links))
actions = {
"detail": {
"url": reverse(f"{model_name}-detail", kwargs={"pk": object.pk}),
"text": "View",
},
"update": {
"url": reverse(f"{model_name}-update", kwargs={"pk": object.pk}),
"text": "Edit",
},
"delete": {
"url": reverse(f"{model_name}-delete", kwargs={"pk": object.pk}),
"text": "Delete",
},
}
return actions


@register.inclusion_tag("neapolitan/partial/detail.html")
Expand Down

0 comments on commit 0d5fe12

Please sign in to comment.