|
|
@@ -34,22 +34,18 @@ class ObjectAttribute:
|
|
|
Base class for representing an attribute of an object.
|
|
|
|
|
|
Attributes:
|
|
|
- template_name: The name of the template to render
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- placeholder: HTML to render for empty/null values
|
|
|
+ template_name (str): The name of the template to render
|
|
|
+ placeholder (str): HTML to render for empty/null values
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ accessor (str): The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
+ label (str): Human-friendly label for the rendered attribute
|
|
|
"""
|
|
|
template_name = None
|
|
|
label = None
|
|
|
placeholder = mark_safe(PLACEHOLDER_HTML)
|
|
|
|
|
|
def __init__(self, accessor, label=None):
|
|
|
- """
|
|
|
- Instantiate a new ObjectAttribute.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- """
|
|
|
self.accessor = accessor
|
|
|
if label is not None:
|
|
|
self.label = label
|
|
|
@@ -59,7 +55,7 @@ class ObjectAttribute:
|
|
|
Return the value of the attribute.
|
|
|
|
|
|
Parameters:
|
|
|
- obj: The object for which the attribute is being rendered
|
|
|
+ obj (object): The object for which the attribute is being rendered
|
|
|
"""
|
|
|
return resolve_attr_path(obj, self.accessor)
|
|
|
|
|
|
@@ -68,8 +64,8 @@ class ObjectAttribute:
|
|
|
Return any additional template context used to render the attribute value.
|
|
|
|
|
|
Parameters:
|
|
|
- obj: The object for which the attribute is being rendered
|
|
|
- context: The root template context
|
|
|
+ obj (object): The object for which the attribute is being rendered
|
|
|
+ context (dict): The root template context
|
|
|
"""
|
|
|
return {}
|
|
|
|
|
|
@@ -90,21 +86,15 @@ class ObjectAttribute:
|
|
|
class TextAttr(ObjectAttribute):
|
|
|
"""
|
|
|
A text attribute.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ style (str): CSS class to apply to the rendered attribute
|
|
|
+ format_string (str): If specified, the value will be formatted using this string when rendering
|
|
|
+ copy_button (bool): Set to True to include a copy-to-clipboard button
|
|
|
"""
|
|
|
template_name = 'ui/attrs/text.html'
|
|
|
|
|
|
def __init__(self, *args, style=None, format_string=None, copy_button=False, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new TextAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- style: CSS class to apply to the rendered attribute
|
|
|
- format_string: If specified, the value will be formatted using this string when rendering
|
|
|
- copy_button: Set to True to include a copy-to-clipboard button
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.style = style
|
|
|
self.format_string = format_string
|
|
|
@@ -127,20 +117,14 @@ class TextAttr(ObjectAttribute):
|
|
|
class NumericAttr(ObjectAttribute):
|
|
|
"""
|
|
|
An integer or float attribute.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ unit_accessor (str): Accessor for the unit of measurement to display alongside the value (if any)
|
|
|
+ copy_button (bool): Set to True to include a copy-to-clipboard button
|
|
|
"""
|
|
|
template_name = 'ui/attrs/numeric.html'
|
|
|
|
|
|
def __init__(self, *args, unit_accessor=None, copy_button=False, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new NumericAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- unit_accessor: Accessor for the unit of measurement to display alongside the value (if any)
|
|
|
- copy_button: Set to True to include a copy-to-clipboard button
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.unit_accessor = unit_accessor
|
|
|
self.copy_button = copy_button
|
|
|
@@ -181,19 +165,13 @@ class ChoiceAttr(ObjectAttribute):
|
|
|
class BooleanAttr(ObjectAttribute):
|
|
|
"""
|
|
|
A boolean attribute.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ display_false (bool): If False, a placeholder will be rendered instead of the "False" indication
|
|
|
"""
|
|
|
template_name = 'ui/attrs/boolean.html'
|
|
|
|
|
|
def __init__(self, *args, display_false=True, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new BooleanAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- display_false: If False, a placeholder will be rendered instead of the "False" indication
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.display_false = display_false
|
|
|
|
|
|
@@ -222,21 +200,15 @@ class ImageAttr(ObjectAttribute):
|
|
|
class RelatedObjectAttr(ObjectAttribute):
|
|
|
"""
|
|
|
An attribute representing a related object.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ linkify (bool): If True, the rendered value will be hyperlinked to the related object's detail view
|
|
|
+ grouped_by (str): A second-order object to annotate alongside the related object; for example, an attribute
|
|
|
+ representing the dcim.Site model might specify grouped_by="region"
|
|
|
"""
|
|
|
template_name = 'ui/attrs/object.html'
|
|
|
|
|
|
def __init__(self, *args, linkify=None, grouped_by=None, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new RelatedObjectAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- linkify: If True, the rendered value will be hyperlinked to the related object's detail view
|
|
|
- grouped_by: A second-order object to annotate alongside the related object; for example, an attribute
|
|
|
- representing the dcim.Site model might specify grouped_by="region"
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.linkify = linkify
|
|
|
self.grouped_by = grouped_by
|
|
|
@@ -254,20 +226,14 @@ class NestedObjectAttr(ObjectAttribute):
|
|
|
"""
|
|
|
An attribute representing a related nested object. Similar to `RelatedObjectAttr`, but includes the ancestors of the
|
|
|
related object in the rendered output.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ linkify (bool): If True, the rendered value will be hyperlinked to the related object's detail view
|
|
|
+ max_depth (int): Maximum number of ancestors to display (default: all)
|
|
|
"""
|
|
|
template_name = 'ui/attrs/nested_object.html'
|
|
|
|
|
|
def __init__(self, *args, linkify=None, max_depth=None, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new NestedObjectAttr. Shows a related object as well as its ancestors.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- linkify: If True, the rendered value will be hyperlinked to the related object's detail view
|
|
|
- max_depth: Maximum number of ancestors to display (default: all)
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.linkify = linkify
|
|
|
self.max_depth = max_depth
|
|
|
@@ -286,19 +252,13 @@ class NestedObjectAttr(ObjectAttribute):
|
|
|
class AddressAttr(ObjectAttribute):
|
|
|
"""
|
|
|
A physical or mailing address.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ map_url (bool): If true, the address will render as a hyperlink using settings.MAPS_URL
|
|
|
"""
|
|
|
template_name = 'ui/attrs/address.html'
|
|
|
|
|
|
def __init__(self, *args, map_url=True, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new AddressAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- map_url: If true, the address will render as a hyperlink using settings.MAPS_URL
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
if map_url is True:
|
|
|
self.map_url = get_config().MAPS_URL
|
|
|
@@ -316,21 +276,16 @@ class AddressAttr(ObjectAttribute):
|
|
|
class GPSCoordinatesAttr(ObjectAttribute):
|
|
|
"""
|
|
|
A GPS coordinates pair comprising latitude and longitude values.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ latitude_attr (float): The name of the field containing the latitude value
|
|
|
+ longitude_attr (float): The name of the field containing the longitude value
|
|
|
+ map_url (bool): If true, the address will render as a hyperlink using settings.MAPS_URL
|
|
|
"""
|
|
|
template_name = 'ui/attrs/gps_coordinates.html'
|
|
|
label = _('GPS coordinates')
|
|
|
|
|
|
def __init__(self, latitude_attr='latitude', longitude_attr='longitude', map_url=True, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new GPSCoordinatesAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- latitude_attr: The name of the field containing the latitude value
|
|
|
- longitude_attr: The name of the field containing the longitude value
|
|
|
- map_url: If true, the address will render as a hyperlink using settings.MAPS_URL
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(accessor=None, **kwargs)
|
|
|
self.latitude_attr = latitude_attr
|
|
|
self.longitude_attr = longitude_attr
|
|
|
@@ -365,18 +320,12 @@ class TimezoneAttr(ObjectAttribute):
|
|
|
class TemplatedAttr(ObjectAttribute):
|
|
|
"""
|
|
|
Renders an attribute using a custom template.
|
|
|
+
|
|
|
+ Parameters:
|
|
|
+ template_name (str): The name of the template to render
|
|
|
+ context (dict): Additional context to pass to the template when rendering
|
|
|
"""
|
|
|
def __init__(self, *args, template_name, context=None, **kwargs):
|
|
|
- """
|
|
|
- Instantiate a new TemplatedAttr.
|
|
|
-
|
|
|
- Parameters:
|
|
|
- accessor: The dotted path to the attribute being rendered (e.g. "site.region.name")
|
|
|
- template_name: The name of the template to render
|
|
|
- context: Additional context to pass to the template when rendering
|
|
|
- label: Human-friendly label for the rendered attribute
|
|
|
- template_name: The name of the template to render
|
|
|
- """
|
|
|
super().__init__(*args, **kwargs)
|
|
|
self.template_name = template_name
|
|
|
self.context = context or {}
|