Refactor noisy getter methods into neat lambdas

This commit is contained in:
Per von Zweigbergk 2024-01-23 20:49:10 +01:00
parent c728d3c2e8
commit da7f67c359

View File

@ -52,46 +52,6 @@ def get_cabletermination_row_class(record):
return '' return ''
def get_interface_state_attribute(record):
"""
Get interface enabled state as string to attach to <tr/> DOM element.
"""
if record.enabled:
return "enabled"
else:
return "disabled"
def get_interface_virtual_attribute(record):
"""
Get interface virtual state as string to attach to <tr/> DOM element.
"""
if record.is_virtual:
return "true"
else:
return "false"
def get_interface_mark_connected_attribute(record):
"""
Get interface enabled state as string to attach to <tr/> DOM element.
"""
if record.mark_connected:
return "true"
else:
return "false"
def get_interface_cable_status_attribute(record):
"""
Get interface enabled state as string to attach to <tr/> DOM element.
"""
if record.cable:
return record.cable.status
else:
return ""
# #
# Device roles # Device roles
# #
@ -694,10 +654,10 @@ class DeviceInterfaceTable(InterfaceTable):
) )
row_attrs = { row_attrs = {
'data-name': lambda record: record.name, 'data-name': lambda record: record.name,
'data-enabled': get_interface_state_attribute, 'data-enabled': lambda record: "enabled" if record.enabled else "disabled",
'data-virtual': get_interface_virtual_attribute, 'data-virtual': lambda record: "true" if record.is_virtual else "false",
'data-mark-connected': get_interface_mark_connected_attribute, 'data-mark-connected': lambda record: "true" if record.mark_connected else "false",
'data-cable-status': get_interface_cable_status_attribute, 'data-cable-status': lambda record: record.cable.status if record.cable else "",
'data-type': lambda record: record.type, 'data-type': lambda record: record.type,
} }
cable_status_styles = [(slug, color) for slug, _, color in LinkStatusChoices.CHOICES] cable_status_styles = [(slug, color) for slug, _, color in LinkStatusChoices.CHOICES]