-
-
Notifications
You must be signed in to change notification settings - Fork 299
[change] Show only relevant templates in device admin #345 #398
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,14 @@ | ||
| 'use strict'; | ||
| django.jQuery(function ($) { | ||
| var firstRun = true, | ||
| addChangeEventToBackend = function (urls) { | ||
| addChangeEventToBackend = function (default_urls, relevant_urls) { | ||
| $('#id_config-0-backend').change(function () { | ||
| setTimeout(function () { | ||
| // ensures getDefaultTemplates execute only after other | ||
| // onChange event handlers attached this field has been | ||
| // executed. | ||
| getDefaultTemplates(urls); | ||
| getRelevantTemplates(relevant_urls); | ||
| getDefaultTemplates(default_urls); | ||
| }); | ||
| }); | ||
| }, | ||
|
|
@@ -27,7 +28,7 @@ django.jQuery(function ($) { | |
| return; | ||
| } | ||
| var url = urls[orgID], | ||
| isNew = $('#id_config-0-id').length == 0; | ||
| isNew = $('#id_config-0-id').length === 0; | ||
| // if device is not new, do not execute on page load | ||
| if (!isNew && firstRun) { | ||
| return; | ||
|
|
@@ -46,23 +47,81 @@ django.jQuery(function ($) { | |
| }); | ||
| }); | ||
| }, | ||
| bindDefaultTemplateLoading = function (urls) { | ||
| getRelevantTemplates = function (urls) { | ||
| var orgID = $('#id_organization').val(), | ||
| backend = $('#id_config-0-backend').val(); | ||
| if (!orgID || !backend) { | ||
| return; | ||
| } | ||
| // proceed only if an organization and a backend have been selected | ||
| if (orgID.length === 0 || backend.length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| var url = urls[orgID]; | ||
| // get relevant templates of selected org and backend | ||
| url = url + '?backend=' + backend; | ||
| $.get(url).done(function (data) { | ||
| var relevantTemplates = {}; | ||
| $.each(data.templates, function (i, uuid) { | ||
| relevantTemplates[uuid] = uuid; | ||
| }); | ||
| var disabledTemplates = | ||
| localStorage.getItem('config-disabled-templates', null); | ||
| disabledTemplates = JSON.parse(disabledTemplates) || {}; | ||
| $('input.sortedm2m').each(function () { | ||
| if (($(this).val() in relevantTemplates) === false) { | ||
| // hide templates which are not relevant | ||
| // also removed disabled attribute and uncheck it | ||
| if ($(this).prop('disabled')) { | ||
| disabledTemplates[$(this).val()] = $(this).val(); | ||
| $(this).prop('disabled', false); | ||
| $(this).prop('checked', false); | ||
| } | ||
| $(this).parent().parent().hide(); | ||
| } else { | ||
| // show templates which are relevant | ||
| if ($(this).val() in disabledTemplates) { | ||
| $(this).prop('disabled', true); | ||
| $(this).prop('checked', true); | ||
| delete disabledTemplates[$(this).val()]; | ||
| } | ||
| $(this).parent().parent().show(); | ||
| } | ||
| }); | ||
| disabledTemplates = JSON.stringify(disabledTemplates); | ||
| localStorage.setItem('config-disabled-templates', disabledTemplates); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just keep it in the memory instead of using localStorage? How making |
||
| // change help text if no relevant template | ||
| var msg = 'Choose items and order by drag & drop.'; | ||
| if (Object.keys(relevantTemplates).length === 0) { | ||
| msg = 'No Template available'; | ||
| } | ||
| if (gettext) { | ||
| msg = gettext(msg); | ||
| } | ||
| $('.sortedm2m-container > .help').text(msg); | ||
| }); | ||
| }, | ||
| bindDefaultTemplateLoading = function (default_urls, relevant_urls) { | ||
| var backendField = $('#id_config-0-backend'); | ||
| $('#id_organization').change(function () { | ||
| if ($('#id_config-0-backend').length > 0) { | ||
| getDefaultTemplates(urls); | ||
| getRelevantTemplates(relevant_urls); | ||
| getDefaultTemplates(default_urls); | ||
| } | ||
| }); | ||
| if (backendField.length > 0) { | ||
| addChangeEventToBackend(urls); | ||
| addChangeEventToBackend(default_urls, relevant_urls); | ||
| } else { | ||
| $('#config-group > fieldset.module').ready(function () { | ||
| $('div.add-row > a').click(function () { | ||
| addChangeEventToBackend(urls); | ||
| getDefaultTemplates(urls); | ||
| addChangeEventToBackend(default_urls, relevant_urls); | ||
| getRelevantTemplates(relevant_urls); | ||
| getDefaultTemplates(default_urls); | ||
| }); | ||
| }); | ||
| } | ||
| getRelevantTemplates(relevant_urls); | ||
| firstRun = false; | ||
| }; | ||
| window.bindDefaultTemplateLoading = bindDefaultTemplateLoading; | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -61,7 +61,10 @@ | |||||||||
| // enable default templates - do not remove this comment | ||||||||||
| (function ($) { | ||||||||||
| $(document).ready( function () { | ||||||||||
| window.bindDefaultTemplateLoading({{ default_template_urls| safe }}); | ||||||||||
| window.bindDefaultTemplateLoading( | ||||||||||
| {{ default_template_urls| safe }}, | ||||||||||
| {{relevant_template_urls| safe}} | ||||||||||
|
Comment on lines
+65
to
+66
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| ); | ||||||||||
| }) | ||||||||||
| }) (django.jQuery); | ||||||||||
| </script> | ||||||||||
|
|
||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,11 @@ | |
| from openwisp_users.models import Organization | ||
|
|
||
| from .settings import BACKENDS, VPN_BACKENDS | ||
| from .utils import get_default_templates_queryset, get_object_or_404 | ||
| from .utils import ( | ||
| get_default_templates_queryset, | ||
| get_object_or_404, | ||
| get_relevant_templates_queryset, | ||
| ) | ||
|
|
||
| Template = load_model('config', 'Template') | ||
|
|
||
|
|
@@ -34,6 +38,24 @@ def get_default_templates(request, organization_id): | |
| return JsonResponse({'default_templates': uuids}) | ||
|
|
||
|
|
||
| def get_relevant_templates(request, organization_id): | ||
| """ | ||
| returns relevant templates of specified organization | ||
| and config backend | ||
| """ | ||
| backend = request.GET.get("backend", None) | ||
| user = request.user | ||
| authenticated = user.is_authenticated | ||
| if not authenticated and not user.is_staff: | ||
| return HttpResponse(status=403) | ||
| org = get_object_or_404(Organization, pk=organization_id, is_active=True) | ||
| templates = get_relevant_templates_queryset(org.pk, backend, model=Template).only( | ||
| 'id' | ||
| ) | ||
| uuids = [str(t.pk) for t in templates] | ||
| return JsonResponse({'templates': uuids}) | ||
|
Comment on lines
+41
to
+56
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be better to do a check like Please also add a test for this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see this is a problem with
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #420
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. damn 🤦 |
||
|
|
||
|
|
||
| ALL_BACKENDS = BACKENDS + VPN_BACKENDS | ||
|
|
||
| # ``available_schemas`` and ``available_schemas_json`` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use do
relevantTemplates[$(this).val()]and put everything in try catch instead? The "in" operator will take more time if number of entries is more in relevantTemplates.