Skip to content
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

Housekeeping/code cleanup #432

Merged
merged 2 commits into from
Oct 13, 2024
Merged
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
24 changes: 3 additions & 21 deletions netbox_dns/fields/address.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
from django import forms
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import gettext_lazy as _

from netaddr import AddrFormatError, IPAddress

from ipam.formfields import IPAddressFormField

__all__ = (
"AddressFormField",
"AddressField",
)


class AddressFormField(forms.Field):
def to_python(self, value):
if not value:
return None

if isinstance(value, IPAddress):
return value

try:
ip_address = IPAddress(value)
except AddrFormatError as exc:
raise ValidationError(exc)

return ip_address
__all__ = ("AddressField",)


class AddressField(models.Field):
Expand Down Expand Up @@ -58,7 +40,7 @@ def get_prep_value(self, value):
return str(self.to_python(value))

def form_class(self):
return AddressFormField
return IPAddressFormField

def formfield(self, **kwargs):
defaults = {"form_class": self.form_class()}
Expand Down
9 changes: 3 additions & 6 deletions netbox_dns/forms/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,16 @@ class RecordForm(TenancyForm, NetBoxModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

initial_zone_id = self.initial.get("zone")
if initial_zone_id is not None:
self.initial["view"] = Zone.objects.get(pk=initial_zone_id).view
else:
self.initial["view"] = View.get_default_view()

initial_name = self.initial.get("name")
if initial_name:
self.initial["name"] = name_to_unicode(initial_name)

view = DynamicModelChoiceField(
queryset=View.objects.all(),
required=False,
initial_params={
"zone": "$zone",
},
label=_p("DNS", "View"),
)
zone = DynamicModelChoiceField(
Expand Down