Skip to content

Commit

Permalink
Fix host set_comment with empty string (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
pederhan authored Jul 25, 2024
1 parent 228c45b commit 5fbc7be
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions mreg_cli/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2493,7 +2493,7 @@ class Host(FrozenModelWithTimestamps, WithTTL, WithHistory, APIMixin):
bacnetid: int | None = None
contact: str
ttl: int | None = None
comment: str | None = None
comment: str

# Note, we do not use WithZone here as this is optional and we resolve it differently.
zone: int | None = None
Expand All @@ -2506,12 +2506,6 @@ def validate_name(cls, value: str) -> HostT:
"""Validate the hostname."""
return HostT(hostname=value)

@field_validator("comment", mode="before")
@classmethod
def empty_string_to_none(cls, v: str) -> str | None:
"""Convert empty strings to None."""
return v or None

@field_validator("bacnetid", mode="before")
@classmethod
def convert_bacnetid(cls, v: dict[str, int] | None) -> int | None:
Expand Down Expand Up @@ -3027,7 +3021,7 @@ def output(self, names: bool = False, traverse_hostgroups: bool = False):
output_manager.add_line(f"{'Name:':<{padding}}{self.name}")
output_manager.add_line(f"{'Contact:':<{padding}}{self.contact}")

if self.comment is not None and self.comment != "":
if self.comment:
output_manager.add_line(f"{'Comment:':<{padding}}{self.comment}")

self.output_ipaddresses(padding=padding, names=names)
Expand Down Expand Up @@ -3186,7 +3180,7 @@ def _format(name: str, contact: str, comment: str) -> None:

_format("Name", "Contact", "Comment")
for i in self.results:
_format(str(i.name), i.contact, i.comment or "")
_format(str(i.name), i.contact, i.comment)


class HostGroup(FrozenModelWithTimestamps, WithName, WithHistory, APIMixin):
Expand Down

0 comments on commit 5fbc7be

Please sign in to comment.