Skip to content

Commit

Permalink
Allow Serveradmin apps to extend the Servershell (Javascript) (#348)
Browse files Browse the repository at this point in the history
Include JavaScript files from Serveradmin apps

Automatically include JavaScript files from Serveradmin apps following a
naming convention. This allows Serveradmin apps to extend or plugin into
the Serverhshell and for example add custom commands.
  • Loading branch information
kofrezo authored Mar 19, 2024
1 parent 058d58c commit 2e3ac30
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 4.14.0
* Allow Serveradmin apps to extend the Servershell (Javascript) [PR!348](https://github.com/innogames/serveradmin/pull/348)

## 4.13.0
* Extend inet attributes and improve ip address type validation [PR!346](https://github.com/innogames/serveradmin/pull/346)
* Bump cryptography from 42.0.2 to 42.0.4 (#347)
Expand Down
32 changes: 30 additions & 2 deletions serveradmin/servershell/static/js/servershell/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ function validate_selected(min=1, max=-1) {
return true;
}

// Serveradmin Servershell plugins can provide help for custom commands by
// extending the servershell.commands_help array like in the example below in
// their static/js/$app.servershell.plugin.js
servershell.commands_help = [
// Example:
// {
// "command": "foo",
// "arguments": "attr_name",
// "description": "A foo that bars",
// }
]

servershell.commands = {
pin: function() {
let selected_ids = servershell.get_selected();
Expand Down Expand Up @@ -770,7 +782,23 @@ function execute_command() {
}

$(document).ready(function() {
$('#command_form').submit(function(event) {

// Gather description for custom commands from Servershell plugins and add
// them to the help modal.
servershell.commands_help.forEach(function(command_help) {
let row = document.createElement("tr");
row.id = `cmd-${command_help["command"]}`;

for (let index of ["command", "arguments", "description"]) {
let cell = document.createElement("td");
cell.innerText = command_help[index];
row.appendChild(cell);
}

$("#help_command").append(row);
});

$('#command_form').submit(function(event) {
event.preventDefault();

// Workaround:
Expand All @@ -786,5 +814,5 @@ $(document).ready(function() {
if (execute_command()) {
servershell.command = '';
}
})
});
});
4 changes: 4 additions & 0 deletions serveradmin/servershell/templates/servershell/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,8 @@
servershell.sync_attrlist_visibility();
});
</script>
{% for plugin in servershell_plugins %}
{# Load javascript files from serveradmin apps for easy extension #}
<script src="{{ STATIC_URL }}{{ plugin }}"></script>
{% endfor %}
{% endblock %}
22 changes: 22 additions & 0 deletions serveradmin/servershell/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from django.apps import apps
from django.contrib.staticfiles import finders


def servershell_plugins():
"""Find $app.servershell.plugin.js files from Serveradmin apps
Scans all apps beginning with serveradmin_ for files that follow the pattern
$app.servershell.plugin.js and return them. This allows Serveradmin apps to
extend the Servershell for example with custom commands.
@return:
"""
js_files = list()
app_names = [app.name for app in apps.get_app_configs() if app.name.startswith("serveradmin_")]
for app_name in app_names:
path = f"js/{app_name}.servershell.plugin.js"
js_file = finders.find(path)
if js_file:
js_files.append(path)

return js_files
2 changes: 2 additions & 0 deletions serveradmin/servershell/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
attribute_startswith
)
from serveradmin.servershell.merged_query_iterator import MergedQuery
from serveradmin.servershell.utils import servershell_plugins

MAX_DISTINGUISHED_VALUES = 50
NUM_SERVERS_DEFAULT = 25
Expand Down Expand Up @@ -106,6 +107,7 @@ def index(request):
'order_by': 'hostname',
'filters': sorted([(f.__name__, f.__doc__) for f in filter_classes]),
'search_settings': search_settings,
'servershell_plugins': servershell_plugins(),
})


Expand Down

0 comments on commit 2e3ac30

Please sign in to comment.