Commit 0dbc3afe by Őry Máté

network: fix ipv6 conversion

parent bb9f818b
......@@ -414,6 +414,8 @@ class Vlan(AclBase, models.Model):
def convert_ipv4_to_ipv6(self, ipv4):
"""Convert IPv4 address string to IPv6 address string."""
if isinstance(ipv4, basestring):
ipv4 = IPAddress(ipv4, 4)
nums = {ascii_letters[i]: int(ipv4.words[i]) for i in range(4)}
return IPAddress(self.ipv6_template % nums)
......
......@@ -444,19 +444,19 @@ class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin,
def _get_ajax(self, *args, **kwargs):
GET = self.request.GET
result = {}
if "vlan" in GET:
vlan = get_object_or_404(Vlan.objects, pk=GET["vlan"])
vlan = get_object_or_404(Vlan.objects, pk=GET.get("vlan", ""))
if "ipv4" in GET:
try:
result["ipv6"] = vlan.convert_ipv4_to_ipv6(GET["ipv4"])
except:
result["ipv6"] = ""
else:
try:
result.update(vlan.get_new_address())
except ValidationError:
result["ipv4"] = ""
result["ipv6"] = ""
if "ipv4" in GET:
try:
result["ipv6"] = vlan.convert_ipv4_to_ipv6(GET["ipv4"])
except:
result["ipv6"] = ""
return JsonResponse({k: unicode(result[k] or "") for k in result})
return JsonResponse({k: unicode(result[k] or "") for k in result})
def get(self, *args, **kwargs):
if self.request.is_ajax():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment