Skip to content

Commit

Permalink
Merge pull request #190 from nautobot/fix-custom-fields
Browse files Browse the repository at this point in the history
Fix custom fields
  • Loading branch information
abates authored Sep 27, 2024
2 parents 7ea643d + 387f2da commit 283f581
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
4 changes: 2 additions & 2 deletions nautobot_design_builder/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,8 +580,8 @@ def _update_fields(self):
elif hasattr(self.instance, field_name):
setattr(self.instance, field_name, value)

for key, value in self.metadata.custom_fields.items():
self.set_custom_field(key, value)
for key, value in self.metadata.custom_fields.items():
self.set_custom_field(key, value)

def save(self):
"""Save the model instance to the database.
Expand Down
29 changes: 28 additions & 1 deletion nautobot_design_builder/tests/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Test object creator methods."""

import importlib
from operator import attrgetter
import os
from unittest.mock import patch
import yaml
Expand Down Expand Up @@ -83,6 +82,34 @@ def check_not_in(test, check, index):
test.assertNotIn(value0, value1, msg=f"Check {index}")


class attrgetter: # pylint:disable=invalid-name,too-few-public-methods
"""Return a callable object that fetches attr or key from its operand.
The attribute names can also contain dots
"""

def __init__(self, attr):
if not isinstance(attr, str):
raise TypeError("attribute name must be a string")
self._attrs = (attr,)
names = attr.split(".")

def func(obj):
for name in names:
if hasattr(obj, name):
obj = getattr(obj, name)
elif name in obj:
obj = obj[name]
else:
raise AttributeError(f"'{type(obj).__name__}' has no attribute or item '{name}'")
return obj

self._call = func

def __call__(self, obj):
return self._call(obj)


def _get_value(check_info):
if "value" in check_info:
value = check_info["value"]
Expand Down
54 changes: 54 additions & 0 deletions nautobot_design_builder/tests/testdata/custom_fields.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
designs:
- custom_fields:
- label: "Test Boolean"
key: "test_boolean"
type: "boolean"
default: false
content_types:
- "!get:app_label": "ipam"
"!get:model": "vlan"
vlans:
- vid: 100
name: "TEST-VLAN-100"
status__name: "Active"
custom_fields:
"test_boolean": true
- vid: 101
name: "TEST-VLAN-101"
status__name: "Active"
custom_fields:
"test_boolean": false
- vid: 102
name: "TEST-VLAN-102"
status__name: "Active"
- vid: 103
name: "TEST-VLAN-103"
status__name: "Active"
- "!update:vid": 103
custom_fields:
"test_boolean": true
checks:
- equal:
- model: "nautobot.ipam.models.VLAN"
query: {vid: 100}
attribute: "cf.test_boolean"
- value: true

- equal:
- model: "nautobot.ipam.models.VLAN"
query: {vid: 101}
attribute: "cf.test_boolean"
- value: false

- equal:
- model: "nautobot.ipam.models.VLAN"
query: {vid: 102}
attribute: "cf.test_boolean"
- value: false

- equal:
- model: "nautobot.ipam.models.VLAN"
query: {vid: 103}
attribute: "cf.test_boolean"
- value: true

0 comments on commit 283f581

Please sign in to comment.