-
-
Notifications
You must be signed in to change notification settings - Fork 214
[change] Show only relevant templates in device admin #345 #428
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
Conversation
The build is failing for some reason I don't understand right away. I will try to debug it tomorrow. The feature work as intended in the UI |
77894d7
to
264627d
Compare
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.
Great work @niteshsinha17 and @pandafy, this is going to be a great improvement! 👍
Tested it and works good!
I found a minor code issue, see below.
I have an additional minor request: can we hide all the templates when no organization is selected? We can do so with CSS if needed, so initially the templates are hidden and our JS shows them, or in any other way you feel it would work.
I regarding the commit prefix, I would probably use change
since it makes sense here, we're changing the behavior from auto-selecting default templates, to showing only relevant templates, auto selecting default templates and auto-checking required templates. What do you think?
@@ -241,6 +241,7 @@ def manage_vpn_clients(cls, action, instance, pk_set, **kwargs): | |||
|
|||
@classmethod | |||
def clean_templates_org(cls, action, instance, pk_set, **kwargs): | |||
templates = cls._get_templates_from_pk_set(pk_set) |
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.
I don't understand, this looks repeated (after 3 lines we call this again), why is that?
Looks like a mistake to me.
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.
now I see, but why are you doing this before the pre_add
check?
I don't see a good reason for this change. If I remember correctly, calling this could generate DB queries so we should avoid doing it unless it's really necessary (that is, we should do it after the if below).
Why do you think it should be done earlier?
264627d
to
bd6b8f3
Compare
Improved implementation for fetching relevant templates. Instead of having separate views to get default and required templats, their values are added in response of relevant templates views. Removed sortedm2m code duplication for required templates Closes #332 Closes #345 Closes #342 Co-authored-by: Nitesh Sinha <[email protected]>
bd6b8f3
to
522519f
Compare
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.
I have found an issue during testing.
If you use the search feature of the sorted m2m widget, when you clear the search field, all the options for all the backends and all the orgs appear again. Maybe we should remove/add elements instead of hiding/showing? Or what other solution do you think we can find?
@@ -241,6 +241,7 @@ def manage_vpn_clients(cls, action, instance, pk_set, **kwargs): | |||
|
|||
@classmethod | |||
def clean_templates_org(cls, action, instance, pk_set, **kwargs): | |||
templates = cls._get_templates_from_pk_set(pk_set) |
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.
now I see, but why are you doing this before the pre_add
check?
I don't see a good reason for this change. If I remember correctly, calling this could generate DB queries so we should avoid doing it unless it's really necessary (that is, we should do it after the if below).
Why do you think it should be done earlier?
@@ -70,45 +70,57 @@ def test_get_default_templates(self): | |||
) = self._create_template_test_data() | |||
self._login() | |||
response = self.client.get( | |||
reverse('admin:get_default_templates', args=[org1.pk]) | |||
reverse('admin:get_relevant_templates', args=[org1.pk]) | |||
) |
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.
can you please add an assertNumQueries
here to ensure the amount of queries is under control?
) | ||
self._login() | ||
|
||
r = self.client.get( | ||
reverse('admin:get_default_templates', args=[org1.pk]), | ||
response = self.client.get( |
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.
same here for assertNumQueries
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.
Another issue I found during testing:
- create a default template in the system
- assign it to a device and save, this works normally
- now unassign it, suppose you don't need that default template to be assigned to that specific device, hit save
- when page reloads the default template which was just unassigned gets reassigned again
The automatic default template assignment should be done only when switching org, or backend.
I think we will need to introduce another endpoint to allow fetch currently applied templates from the backend. The endpoint can be Or, would you like to add this information in the current endpoint? But here additional database queries will be made each time. I am in favour of creating a separate endpoint. |
I think the information about the currently applied templates should be available in the form and we can get it from there, what do you think? |
Yes, it will be available. But I was also thinking about #204 (comment). We can leave it for late, I guess. |
Ok I understand what you mean, yes let's leave it for later and proceed incrementally. |
8c8eb7b
to
64a288e
Compare
64a288e
to
253123a
Compare
self.assertEqual(len(templates), 2) | ||
self.assertIn(str(t2.pk), templates) | ||
self.assertIn(str(t3.pk), templates) | ||
with self.assertNumQueries(4): |
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.
2 queries are made for authentication, 1 for getting organization and 1 for getting templates
@@ -0,0 +1,116 @@ | |||
'use strict'; | |||
django.jQuery(function ($) { | |||
var selectedTemplates = django._owcInitialValues["config-0-templates"].split(','), |
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.
Getting selectedTemplates
using jQuery selector was resulting in "unsaved changes" dialog box appearing even when no changes were made due to order of template IDs. Using this here, resolved that issue.
} | ||
|
||
$('fieldset ul.sortedm2m-items:visible').append(element); | ||
$('fieldset ul.sortedm2m-items:not(:visible)').append(prefixElement); |
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.
Prefix elements __prefix__
are used by Django internally for form validation. It was working fine without adding them here, but I insisted on adding them to avoid any future bugs.
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.
I pulled the latest changes (also tried destroying and recreating the branch), but I get the following JS errors when I open http://localhost:8000/admin/config/device/add/
jquery.js:4055 Uncaught TypeError: Cannot read property 'split' of undefined
at HTMLDocument.<anonymous> (relevant_templates.js:3)
at mightThrow (jquery.js:3762)
at process (jquery.js:3830)
(anonymous) @ relevant_templates.js:3
mightThrow @ jquery.js:3762
process @ jquery.js:3830
setTimeout (async)
jQuery.readyException @ jquery.js:4054
(anonymous) @ jquery.js:4074
mightThrow @ jquery.js:3762
process @ jquery.js:3830
setTimeout (async)
(anonymous) @ jquery.js:3868
fire @ jquery.js:3496
fireWith @ jquery.js:3626
fire @ jquery.js:3634
fire @ jquery.js:3496
fireWith @ jquery.js:3626
process @ jquery.js:3850
setTimeout (async)
(anonymous) @ jquery.js:3868
fire @ jquery.js:3496
fireWith @ jquery.js:3626
fire @ jquery.js:3634
fire @ jquery.js:3496
fireWith @ jquery.js:3626
ready @ jquery.js:4106
completed @ jquery.js:4116
jquery.js:4055 Uncaught TypeError: window.bindDefaultTemplateLoading is not a function
at HTMLDocument.<anonymous> ((index):1706)
at mightThrow (jquery.js:3762)
at process (jquery.js:3830)
Manually tested device add and change view. |
Build was failing due to openwisp/django-loci@041a82f
Build was failing due to openwisp/django-loci@041a82f
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.
5d8d033
to
ab72ea9
Compare
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.
@@ -373,7 +373,9 @@ | |||
// otherwise load immediately | |||
bindLoadUi(); | |||
// fill device context field with default values of selected templates. | |||
getDefaultValues(true); | |||
setTimeout(function () { |
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 the delayed call?
Unsaved changes will fire an event after loading initial values. This event will be used to trigger the initial loading of default values(configuration variables) of templates.
I broke it unintentionally. I ordered the queryset because after reloading the page the template options was getting jumbled. I have removed the order_by clause from the query
I checked it on current master and on all 0.8.x releases. It is not possible to order templates on device add view. Can we create a separate issue for this?
I was getting the following error occasionally when no templates were selected for the device(no default or required templates). I have improved the implementation in 2a05dce. Instead of a delayed call. it will now be executed only when an event is fired. |
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.
Thanks!
Improved implementation for fetching relevant templates. Instead of
having separate views to get default and required templats, their
values are added in response of relevant templates views.
Closes #342
Closes #332
Closes #345
Co-authored-by: Nitesh Sinha [email protected]