16238 map custom-link button colors to tabler

This commit is contained in:
Arthur 2025-04-23 10:28:26 -07:00
parent 2c3fe9700f
commit 9065667ddf

View File

@ -3,6 +3,7 @@ from django.utils.html import escape
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from core.models import ObjectType from core.models import ObjectType
from extras.choices import CustomLinkButtonClassChoices
from extras.models import CustomLink from extras.models import CustomLink
from netbox.choices import ButtonColorChoices from netbox.choices import ButtonColorChoices
@ -29,6 +30,22 @@ GROUP_BUTTON = """
GROUP_LINK = '<li><a class="dropdown-item" href="{}"{}>{}</a></li>\n' GROUP_LINK = '<li><a class="dropdown-item" href="{}"{}>{}</a></li>\n'
def map_button_class(button_color):
if button_color == ButtonColorChoices.DEFAULT:
return 'outline-secondary'
elif button_color == ButtonColorChoices.WHITE:
return 'light'
elif button_color == ButtonColorChoices.BLACK:
return 'dark'
elif button_color == ButtonColorChoices.GRAY or button_color == ButtonColorChoices.GREY:
return 'secondary'
elif button_color == CustomLinkButtonClassChoices.LINK:
# note: using ghost-secondary as ghost-dark doesn't display in dark mode
# with current tabler version.
return 'ghost-secondary'
return button_color
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def custom_links(context, obj): def custom_links(context, obj):
""" """
@ -64,7 +81,7 @@ def custom_links(context, obj):
try: try:
if rendered := cl.render(link_context): if rendered := cl.render(link_context):
template_code += LINK_BUTTON.format( template_code += LINK_BUTTON.format(
rendered['link'], rendered['link_target'], button_class, rendered['text'] rendered['link'], rendered['link_target'], map_button_class(button_class), rendered['text']
) )
except Exception as e: except Exception as e:
template_code += f'<a class="btn btn-sm btn-outline-secondary" disabled="disabled" title="{e}">' \ template_code += f'<a class="btn btn-sm btn-outline-secondary" disabled="disabled" title="{e}">' \
@ -89,7 +106,7 @@ def custom_links(context, obj):
if links_rendered: if links_rendered:
template_code += GROUP_BUTTON.format( template_code += GROUP_BUTTON.format(
links[0].button_class, escape(group), ''.join(links_rendered) map_button_class(links[0].button_class), escape(group), ''.join(links_rendered)
) )
return mark_safe(template_code) return mark_safe(template_code)