Skip to content

Commit

Permalink
[fix] Fixed JS of history pages on config app #681
Browse files Browse the repository at this point in the history
Fixes #681
  • Loading branch information
pandafy authored Jan 8, 2024
1 parent 3cb308b commit 77858a4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
1 change: 0 additions & 1 deletion openwisp_controller/config/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ def __init__(self, org_id, **kwargs):


class DeviceAdmin(MultitenantAdminMixin, BaseConfigAdmin, UUIDAdmin):
recover_form_template = 'admin/config/device_recover_form.html'
list_display = [
'name',
'backend',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% extends "admin/config/change_form.html" %}
{% load i18n admin_urls %}

{% comment %}
The default "reversion/templates/reversion/recover_form.html" template extends
"admin/change_form.html". Since, the "config" app uses a custom "change_form.html",
we need to create a custom recovery form which extends that.
The following content is taken as-is from "reversion/templates/reversion/recover_form.html"
{% endcomment %}
{% block breadcrumbs %}
<div class="breadcrumbs">
<a href="{% url 'admin:index' %}">{% trans "Home" %}</a> &rsaquo;
<a href="{% url 'admin:app_list' opts.app_label %}">{{opts.app_config.verbose_name}}</a> &rsaquo;
<a href="{% url opts|admin_urlname:'changelist' %}">{{opts.verbose_name_plural|capfirst}}</a> &rsaquo;
<a href="{% url opts|admin_urlname:'change' object_id %}">{{original|truncatewords:"18"}}</a> &rsaquo;
<a href="{% url opts|admin_urlname:'history' object_id %}">{% trans "History" %}</a> &rsaquo;
{% blocktrans with opts.verbose_name as verbose_name %}Revert {{verbose_name}}{% endblocktrans %}
</div>
{% endblock %}


{% block object-tools %}{% endblock %}


{% block form_top %}
<p>{% blocktrans %}Press the save button below to revert to this version of the object.{% endblocktrans %}</p>
{% endblock %}


{% block submit_buttons_top %}{% with is_popup=1 %}{{block.super}}{% endwith %}{% endblock %}
{% block submit_buttons_bottom %}{% with is_popup=1 %}{{block.super}}{% endwith %}{% endblock %}
36 changes: 20 additions & 16 deletions openwisp_controller/connection/static/connection/js/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ var gettext = window.gettext || function (word) { return word; };
var interpolate = interpolate || function(){};

const deviceId = getObjectIdFromUrl();
const commandApiUrl = `${owControllerApiHost.origin}${owCommandApiEndpoint.replace('00000000-0000-0000-0000-000000000000', deviceId)}`;
const commandWebSocket = new ReconnectingWebSocket(
`${getWebSocketProtocol()}${owControllerApiHost.host}/ws/controller/device/${deviceId}/command`,
null, {
debug: false,
automaticOpen: false,
// The library re-connects if it fails to establish a connection in "timeoutInterval".
// On slow internet connections, the default value of "timeoutInterval" will
// keep terminating and re-establishing the connection.
timeoutInterval: 7000,
}
);

django.jQuery(function ($) {
if (isDeviceRecoverForm()) {
if((typeof(owControllerApiHost) !== 'undefined')|| isDeviceRecoverForm()) {
return;
}
const commandWebSocket = new ReconnectingWebSocket(
`${getWebSocketProtocol()}${owControllerApiHost.host}/ws/controller/device/${deviceId}/command`,
null, {
debug: false,
automaticOpen: false,
// The library re-connects if it fails to establish a connection in "timeoutInterval".
// On slow internet connections, the default value of "timeoutInterval" will
// keep terminating and re-establishing the connection.
timeoutInterval: 7000,
}
);
commandWebSocket.open();
let selector = $('#id_command_set-0-type'),
showFields = function () {
Expand All @@ -42,7 +41,7 @@ django.jQuery(function ($) {

initCommandDropdown($);
initCommandOverlay($);
initCommandWebSockets($);
initCommandWebSockets($, commandWebSocket);
});
});

Expand Down Expand Up @@ -336,7 +335,7 @@ function initCommandOverlay($) {
};
$.ajax({
type: 'POST',
url: commandApiUrl,
url: getCommandApiUrl(),
headers: {
'X-CSRFToken': $('input[name="csrfmiddlewaretoken"]').val()
},
Expand Down Expand Up @@ -482,7 +481,7 @@ function initCommandOverlay($) {
}
}

function initCommandWebSockets($) {
function initCommandWebSockets($, commandWebSocket) {
commandWebSocket.addEventListener('message', function (e) {
let data = JSON.parse(e.data);
// Done for keeping future use of these websocket
Expand Down Expand Up @@ -556,6 +555,11 @@ function getWebSocketProtocol() {
return protocol;
}

function getCommandApiUrl() {
return `${owControllerApiHost.origin}${owCommandApiEndpoint.replace('00000000-0000-0000-0000-000000000000', deviceId)}`;
}


function dateTimeStampToDateTimeLocaleString(dateTimeStamp) {
let userLanguage = navigator.language || navigator.userLanguage,
date = dateTimeStamp.toLocaleDateString(
Expand Down

0 comments on commit 77858a4

Please sign in to comment.