2024-02-20 06:44:02 -08:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2023-11-16 12:16:35 -05:00
|
|
|
from netbox.registry import registry
|
|
|
|
|
2023-04-14 10:33:53 -04:00
|
|
|
__all__ = (
|
2023-09-13 09:51:24 -05:00
|
|
|
'get_table_ordering',
|
2023-04-14 10:33:53 -04:00
|
|
|
'linkify_phone',
|
2023-11-16 12:16:35 -05:00
|
|
|
'register_table_column'
|
2023-04-14 10:33:53 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-09-13 09:51:24 -05:00
|
|
|
def get_table_ordering(request, table):
|
|
|
|
"""
|
|
|
|
Given a request, return the prescribed table ordering, if any. This may be necessary to determine prior to rendering
|
|
|
|
the table itself.
|
|
|
|
"""
|
|
|
|
# Check for an explicit ordering
|
|
|
|
if 'sort' in request.GET:
|
|
|
|
return request.GET['sort'] or None
|
|
|
|
|
|
|
|
# Check for a configured preference
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
if preference := request.user.config.get(f'tables.{table.__name__}.ordering'):
|
|
|
|
return preference
|
|
|
|
|
|
|
|
|
2022-01-27 15:00:10 -05:00
|
|
|
def linkify_phone(value):
|
|
|
|
"""
|
|
|
|
Render a telephone number as a hyperlink.
|
|
|
|
"""
|
|
|
|
if value is None:
|
|
|
|
return None
|
|
|
|
return f"tel:{value}"
|
2023-11-16 12:16:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
def register_table_column(column, name, *tables):
|
|
|
|
"""
|
|
|
|
Register a custom column for use on one or more tables.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
column: The column instance to register
|
|
|
|
name: The name of the table column
|
|
|
|
tables: One or more table classes
|
|
|
|
"""
|
|
|
|
for table in tables:
|
|
|
|
reg = registry['tables'][table]
|
|
|
|
if name in reg:
|
2024-02-20 06:44:02 -08:00
|
|
|
raise ValueError(_("A column named {name} is already defined for table {table_name}").format(
|
|
|
|
name=name, table_name=table.__name__
|
|
|
|
))
|
2023-11-16 12:16:35 -05:00
|
|
|
reg[name] = column
|