Skip to content
This repository has been archived by the owner on May 19, 2020. It is now read-only.

Commit

Permalink
Merge pull request #103 from nepython/master
Browse files Browse the repository at this point in the history
[subnet-visualizer] Allow to click on used ip to change them
  • Loading branch information
nemesifier committed Mar 13, 2020
2 parents 046b559 + 97741cc commit 19592ce
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
28 changes: 28 additions & 0 deletions django_ipam/base/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import csv
from collections import OrderedDict

import swapper
from django import forms
from django.contrib import messages
from django.contrib.admin import ModelAdmin
from django.db.models import TextField
from django.db.models.functions import Cast
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.urls import path, re_path, reverse
Expand Down Expand Up @@ -55,12 +58,24 @@ def change_view(self, request, object_id, form_url='', extra_context=None):
collection_depth += 1

used = instance.ipaddress_set.count()

# Storing UUID corresponding to respective IP address in a dictionary
ip_id_list = IpAddress.objects.filter(subnet=instance) \
.annotate(str_id=Cast('id', output_field=TextField())) \
.values_list('ip_address', 'str_id')

# Converting UUIdField to String and then modifying to convert back to uuid form
ip_id_list = OrderedDict(ip_id_list)
ip_uuid = {}
for ip_addr, Ip in ip_id_list.items():
ip_uuid[ip_addr] = f'{Ip[0:8]}-{Ip[8:12]}-{Ip[12:16]}-{Ip[16:20]}-{Ip[20:]}'
available = HostsSet(instance).count() - used
labels = ['Used', 'Available']
values = [used, available]
extra_context = {'labels': labels,
'values': values,
'original': instance,
'ip_uuid': ip_uuid,
'ipaddress_add_url': ipaddress_add_url,
'ipaddress_change_url': ipaddress_change_url,
'subnet_change_url': subnet_change_url,
Expand Down Expand Up @@ -164,3 +179,16 @@ def response_add(self, request, *args, **kwargs):
</script>
""")
return response

def response_change(self, request, *args, **kwargs):
"""
Custom reponse to dismiss a change form popup for IP address.
"""
response = super().response_change(request, *args, **kwargs)
if request.POST.get('_popup'):
return HttpResponse("""
<script type='text/javascript'>
opener.dismissAddAnotherPopup(window);
</script>
""")
return response
1 change: 0 additions & 1 deletion django_ipam/static/django-ipam/css/admin.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
.subnet-visual a.used{
background: rgba(149, 10, 10, 0.9);
border: 1px solid rgba(0, 0, 0, 0.2);
cursor: default;
}

.subnet-visual a.used:hover{
Expand Down
9 changes: 7 additions & 2 deletions django_ipam/static/django-ipam/js/subnet.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ django.jQuery(function ($) {
});


function initHostsInfiniteScroll($, current_subnet, address_add_url) {
function initHostsInfiniteScroll($, current_subnet, address_add_url, address_change_url, ip_uuid) {
'use strict';
var renderedPages = 5,
fetchedPages = [],
Expand All @@ -39,7 +39,12 @@ function initHostsInfiniteScroll($, current_subnet, address_add_url) {
function addressListItem(addr) {
var id = normalizeIP(addr.address);
if (addr.used) {
return '<a class="used" id="addr_' + id + '">' + addr.address + '</a>';
var uuid = ip_uuid[addr.address];
//note 1234 was passed as a dummy to be later on replaced in the script
return '<a class = "used" href=\"' + address_change_url.replace('1234', uuid) +
'?_to_field=id&amp;_popup=1&amp;ip_address=' + addr.address +
'&amp;subnet=' + current_subnet + '"onclick="return showAddAnotherPopup(this);">' +
addr.address + '</a>';
}
return '<a href=\"' + address_add_url + '?_to_field=id&amp;_popup=1&amp;ip_address=' +
addr.address + '&amp;subnet=' + current_subnet + '"onclick="return showAddAnotherPopup(this);" ' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ <h3>{% trans 'Subnet Visual Display' %}</h3>
{% if original and not is_popup %}
<script type="text/javascript">
var current_subnet = '{{ original.pk }}';
var IpAddUrl = '{% url ipaddress_add_url %}'
var IpChangeUrl = '{% url ipaddress_change_url "1234" %}'
django.jQuery(document).ready(function(){
initHostsInfiniteScroll(django.jQuery, current_subnet, '{% url ipaddress_add_url %}')
initHostsInfiniteScroll(django.jQuery, current_subnet, IpAddUrl, IpChangeUrl, {{ip_uuid|safe}})
});
// Nested Subnet Tree
subnet_tree = [];
Expand Down
31 changes: 29 additions & 2 deletions django_ipam/tests/base/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def test_subnet_popup_response(self):
follow=True)
self.assertContains(response, 'ok')

def test_ipaddress_response(self):
def test_add_ipaddress_response(self):
subnet = self._create_subnet(subnet='10.0.0.0/24', description='Sample Subnet')
post_data = self._post_data(ip_address='10.0.0.1',
subnet=str(subnet.id),
Expand All @@ -85,7 +85,20 @@ def test_ipaddress_response(self):
json.loads(post_data), follow=True)
self.assertContains(response, 'ok')

def test_ipaddress_popup_response(self):
def test_change_ipaddress_response(self):
subnet = self._create_subnet(subnet='10.0.0.0/24', description='Sample Subnet')
obj = self._create_ipaddress(ip_address='10.0.0.2', subnet=subnet)
post_data = self._post_data(ip_address='10.0.0.2',
subnet=str(subnet.id),
created_0='2017-08-08',
created_1='15:16:10',
modified_0='2019-08-08',
modified_1='15:16:10')
response = self.client.post(reverse(f'admin:{self.app_name}_ipaddress_change', args=[obj.pk]),
json.loads(post_data), follow=True)
self.assertContains(response, 'ok')

def test_add_ipaddress_popup_response(self):
subnet = self._create_subnet(subnet='10.0.0.0/24', description='Sample Subnet')
post_data = self._post_data(ip_address='10.0.0.1',
subnet=str(subnet.id),
Expand All @@ -98,6 +111,20 @@ def test_ipaddress_popup_response(self):
json.loads(post_data))
self.assertContains(response, 'opener.dismissAddAnotherPopup(window, \'10.0.0.1\');')

def test_change_ipaddress_popup_response(self):
subnet = self._create_subnet(subnet='10.0.0.0/24', description='Sample Subnet')
obj = self._create_ipaddress(ip_address='10.0.0.2', subnet=subnet)
post_data = self._post_data(ip_address='10.0.0.2',
subnet=str(subnet.id),
created_0='2017-08-08',
created_1='15:16:10',
modified_0='2019-08-08',
modified_1='15:16:10',
_popup=1)
response = self.client.post(reverse(f'admin:{self.app_name}_ipaddress_change', args=[obj.pk]),
json.loads(post_data))
self.assertContains(response, 'opener.dismissAddAnotherPopup(window);')

def test_csv_upload(self):
csv_data = """Monachers - Matera,
10.27.1.0/24,
Expand Down

0 comments on commit 19592ce

Please sign in to comment.