Skip to content

Commit

Permalink
[chores] Improve management ip field
Browse files Browse the repository at this point in the history
  • Loading branch information
purhan committed Aug 11, 2021
1 parent fc396db commit 02755ae
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 33 deletions.
1 change: 1 addition & 0 deletions openwisp_controller/config/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ class DeviceAdmin(MultitenantAdminMixin, BaseConfigAdmin, UUIDAdmin):
class Media(BaseConfigAdmin.Media):
js = BaseConfigAdmin.Media.js + [
f'{prefix}js/tabs.js',
f'{prefix}js/device.js',
f'{prefix}js/relevant_templates.js',
]

Expand Down
7 changes: 6 additions & 1 deletion openwisp_controller/config/base/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,12 @@ class AbstractDevice(OrgMixin, BaseModel):
blank=True,
null=True,
db_index=True,
help_text=_('ip address of the management interface, if available'),
help_text=_(
'IP address used by OpenWISP to reach the device when performing '
'any type of push operation or active check. The value of this field is '
'generally sent by the device and hence does not need to be changed, '
'but can be changed or cleared manually if needed.'
),
)
hardware_id = models.CharField(**(app_settings.HARDWARE_ID_OPTIONS))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ class Migration(migrations.Migration):
name='management_ip',
field=models.GenericIPAddressField(
blank=True,
help_text='ip address of the management interface, if available',
help_text='IP address used by OpenWISP to reach the device when '
'performing any type of push operation or active check. The value '
'of this field is generally sent by the device and hence does not '
'need to be changed, but can be changed or cleared manually if needed.',
null=True,
),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ class Migration(migrations.Migration):
field=models.GenericIPAddressField(
blank=True,
db_index=True,
help_text='ip address of the management interface, if available',
help_text='This is the IP address used by OpenWISP to reach the '
'device when performing any type of push operation or active '
'check. The value of this field is generally sent by the device '
'and hence does not need to be changed, but can be changed or '
'cleared manually if needed.',
null=True,
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.1.13 on 2021-08-11 16:12

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('config', '0036_device_group'),
]

operations = [
migrations.AlterField(
model_name='device',
name='management_ip',
field=models.GenericIPAddressField(
blank=True,
db_index=True,
help_text='IP address used by OpenWISP to reach the device when '
'performing any type of push operation or active check. The '
'value of this field is generally sent by the device and hence '
'does not need to be changed, but can be changed or cleared '
'manually if needed.',
null=True,
),
),
]
37 changes: 37 additions & 0 deletions openwisp_controller/config/static/config/js/device.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"use strict";
django.jQuery(function ($) {
if ($(".add-form").length || !$("#device_form").length) {
return;
}

// replaces the management ip field with text with the option to edit it
var ip_input = $(".field-management_ip > div > input");
var initial_ip = ip_input.prop("value");
ip_input.after(function () {
ip_input.hide();
return (
'<span class="readonly" id="management_ip_text">' +
(initial_ip === "" ? "-n/a-" : initial_ip) +
"</span>"
);
});
$("#management_ip_text").after(
'<a id="edit_management_ip"> <img src="/static/admin/img/icon-changelink.svg" alt="Edit"></a>'
);
$("#edit_management_ip").click(function () {
var ip_text = $("#management_ip_text");
var img_element = $("#edit_management_ip > img");
if (img_element.attr("alt") === "Edit") {
ip_input.show();
ip_text.hide();
img_element.attr("src", "/static/admin/img/icon-deletelink.svg");
img_element.attr("alt", "Cancel");
} else {
ip_text.show();
ip_input.hide();
ip_input.val(initial_ip);
img_element.attr("src", "/static/admin/img/icon-changelink.svg");
img_element.attr("alt", "Edit");
}
});
});
28 changes: 0 additions & 28 deletions openwisp_controller/config/static/config/js/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,33 +76,5 @@ django.jQuery(function ($) {
}
}

// replaces the management ip field with text with the option to edit it
$(".field-management_ip > div").append(
'<a id="edit_management_ip" class="button">Edit</a>'
);
var ip_input = $(".field-management_ip > div > input");
var initial_ip = ip_input.prop("value");
ip_input.after(function () {
ip_input.hide();
return (
'<div class="readonly" id="management_ip_text">' +
(initial_ip === "" ? "n/a" : initial_ip) +
"</div>"
);
});
$("#edit_management_ip").click(function () {
var ip_text = $("#management_ip_text");
if (this.text === "Edit") {
ip_input.show();
ip_text.hide();
this.text = "Cancel";
} else {
ip_text.show();
ip_input.hide();
ip_input.val(initial_ip);
this.text = "Edit";
}
});

$('#loading-overlay').fadeOut(400);
});
1 change: 0 additions & 1 deletion openwisp_controller/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def update_last_ip(device, request):
device.last_ip = ip
update_fields.append('last_ip')
if device.management_ip != management_ip:
# do not update in case user uses a custom ip
device.management_ip = management_ip
update_fields.append('management_ip')
if update_fields:
Expand Down
6 changes: 5 additions & 1 deletion tests/openwisp2/sample_config/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,11 @@ class Migration(migrations.Migration):
blank=True,
db_index=True,
help_text=(
'ip address of the management interface, if available'
'IP address used by OpenWISP to reach the device when '
'performing any type of push operation or active check. '
'The value of this field is generally sent by the device '
'and hence does not need to be changed, but can be '
'changed or cleared manually if needed.'
),
null=True,
),
Expand Down

0 comments on commit 02755ae

Please sign in to comment.