Skip to content

Fix cable serialize #662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions pynetbox/core/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ def __iter__(self):
if isinstance(cur_attr, Record):
yield i, dict(cur_attr)
elif isinstance(cur_attr, list) and all(
isinstance(i, Record) for i in cur_attr
isinstance(i, (Record, GenericListObject)) for i in cur_attr
):
yield i, [dict(x) for x in cur_attr]
else:
Expand Down Expand Up @@ -373,10 +373,9 @@ def generic_list_parser(key_name, list_item):
and "object" in list_item
):
lookup = list_item["object_type"]
model = None
model = CONTENT_TYPE_MAPPER.get(lookup)
if model:
return model(list_item["object"], self.api, self.endpoint)
if model := CONTENT_TYPE_MAPPER.get(lookup, None):
record = model(list_item["object"], self.api, self.endpoint)
return GenericListObject(record)

return list_item

Expand Down Expand Up @@ -412,7 +411,7 @@ def list_parser(key_name, list_item):
# check if GFK
if len(v) and isinstance(v[0], dict) and "object_type" in v[0]:
v = [generic_list_parser(k, i) for i in v]
to_cache = list(v)
to_cache = [i.serialize() for i in v]
elif k == "constraints":
# Permissions constraints can be either dict or list
to_cache = copy.deepcopy(v)
Expand Down Expand Up @@ -470,6 +469,7 @@ def serialize(self, nested=False, init=False):
If an attribute's value is a ``Record`` type it's replaced with
the ``id`` field of that object.


.. note::

Using this to get a dictionary representation of the record
Expand All @@ -485,6 +485,7 @@ def serialize(self, nested=False, init=False):
init_vals = dict(self._init_cache)

ret = {}

for i in dict(self):
current_val = getattr(self, i) if not init else init_vals.get(i)
if i == "custom_fields":
Expand All @@ -494,15 +495,21 @@ def serialize(self, nested=False, init=False):
current_val = getattr(current_val, "serialize")(nested=True)

if isinstance(current_val, list):
current_val = [
v.id if isinstance(v, Record) else v for v in current_val
]
serialized_list = []
for v in current_val:
if isinstance(v, GenericListObject):
v = v.serialize()
elif isinstance(v, Record):
v = v.id
serialized_list.append(v)
current_val = serialized_list
if i in LIST_AS_SET and (
all([isinstance(v, str) for v in current_val])
or all([isinstance(v, int) for v in current_val])
):
current_val = list(OrderedDict.fromkeys(current_val))
ret[i] = current_val

return ret

def _diff(self):
Expand Down Expand Up @@ -615,3 +622,29 @@ def delete(self):
http_session=self.api.http_session,
)
return True if req.delete() else False


class GenericListObject:
def __init__(self, record):
from pynetbox.models.mapper import TYPE_CONTENT_MAPPER

self.object = record
self.object_id = record.id
self.object_type = TYPE_CONTENT_MAPPER.get(record.__class__)

def __repr__(self):
return str(self.object)

def serialize(self):
ret = {k: getattr(self, k) for k in ["object_id", "object_type"]}
return ret

def __getattr__(self, k):
return getattr(self.object, k)

def __iter__(self):
for i in ["object_id", "object_type", "object"]:
cur_attr = getattr(self, i)
if isinstance(cur_attr, Record):
cur_attr = dict(cur_attr)
yield i, cur_attr
2 changes: 2 additions & 0 deletions pynetbox/models/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,5 @@
"wireless.WirelessLANGroup": None,
"wireless.wirelesslink": None,
}

TYPE_CONTENT_MAPPER = {v: k for k, v in CONTENT_TYPE_MAPPER.items() if v is not None}
Loading