tables.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from netbox.registry import registry
  2. __all__ = (
  3. 'get_table_ordering',
  4. 'linkify_phone',
  5. 'register_table_column'
  6. )
  7. def get_table_ordering(request, table):
  8. """
  9. Given a request, return the prescribed table ordering, if any. This may be necessary to determine prior to rendering
  10. the table itself.
  11. """
  12. # Check for an explicit ordering
  13. if 'sort' in request.GET:
  14. return request.GET['sort'] or None
  15. # Check for a configured preference
  16. if request.user.is_authenticated:
  17. if preference := request.user.config.get(f'tables.{table.__name__}.ordering'):
  18. return preference
  19. def linkify_phone(value):
  20. """
  21. Render a telephone number as a hyperlink.
  22. """
  23. if value is None:
  24. return None
  25. return f"tel:{value}"
  26. def register_table_column(column, name, *tables):
  27. """
  28. Register a custom column for use on one or more tables.
  29. Args:
  30. column: The column instance to register
  31. name: The name of the table column
  32. tables: One or more table classes
  33. """
  34. for table in tables:
  35. reg = registry['tables'][table]
  36. if name in reg:
  37. raise ValueError(f"A column named {name} is already defined for table {table.__name__}")
  38. reg[name] = column