Skip to content

[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

Merged
merged 8 commits into from
Apr 16, 2021

Conversation

pandafy
Copy link
Member

@pandafy pandafy commented Apr 8, 2021

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]

@pandafy pandafy self-assigned this Apr 8, 2021
@pandafy
Copy link
Member Author

pandafy commented Apr 8, 2021

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

@pandafy pandafy force-pushed the issues/332/345-relevant-templates branch 2 times, most recently from 77894d7 to 264627d Compare April 9, 2021 11:06
@nemesifier nemesifier changed the title [enhancement] Show only relevant templates in device admin #345 [feature] Show only relevant templates in device admin #345 Apr 10, 2021
@nemesifier nemesifier changed the title [feature] Show only relevant templates in device admin #345 [change] Show only relevant templates in device admin #345 Apr 10, 2021
Copy link
Member

@nemesifier nemesifier left a 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)
Copy link
Member

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.

Copy link
Member

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?

@pandafy pandafy force-pushed the issues/332/345-relevant-templates branch from 264627d to bd6b8f3 Compare April 10, 2021 11:53
@coveralls
Copy link

coveralls commented Apr 10, 2021

Coverage Status

Coverage decreased (-0.02%) to 98.958% when pulling 2a05dce on issues/332/345-relevant-templates into da88633 on master.

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]>
@pandafy pandafy force-pushed the issues/332/345-relevant-templates branch from bd6b8f3 to 522519f Compare April 10, 2021 12:31
@pandafy pandafy requested a review from nemesifier April 12, 2021 03:34
Copy link
Member

@nemesifier nemesifier left a 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)
Copy link
Member

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])
)
Copy link
Member

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here for assertNumQueries

Copy link
Member

@nemesifier nemesifier left a 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.

@pandafy
Copy link
Member Author

pandafy commented Apr 13, 2021

I think we will need to introduce another endpoint to allow fetch currently applied templates from the backend.

The endpoint can be /admin/config/device/<device-id>/get-templates/ and it will return a list(Array) of template ids. This endpoint will only be triggered once, i.e. on the page load.

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.

@nemesifier
Copy link
Member

I think we will need to introduce another endpoint to allow fetch currently applied templates from the backend.

The endpoint can be /admin/config/device/<device-id>/get-templates/ and it will return a list(Array) of template ids. This endpoint will only be triggered once, i.e. on the page load.

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?

@pandafy
Copy link
Member Author

pandafy commented Apr 13, 2021

Yes, it will be available. But I was also thinking about #204 (comment). We can leave it for late, I guess.

@nemesifier
Copy link
Member

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.

@pandafy pandafy force-pushed the issues/332/345-relevant-templates branch from 8c8eb7b to 64a288e Compare April 14, 2021 01:35
@pandafy pandafy force-pushed the issues/332/345-relevant-templates branch from 64a288e to 253123a Compare April 14, 2021 01:41
self.assertEqual(len(templates), 2)
self.assertIn(str(t2.pk), templates)
self.assertIn(str(t3.pk), templates)
with self.assertNumQueries(4):
Copy link
Member Author

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(','),
Copy link
Member Author

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);
Copy link
Member Author

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.

Copy link
Member

@nemesifier nemesifier left a 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)

Screenshot from 2021-04-13 21-42-02

@pandafy
Copy link
Member Author

pandafy commented Apr 14, 2021

Manually tested device add and change view.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The add page now works well, but in the change page I am experiencing an issue: when the page loads, the right templates are not selected (I mean, they appear initially but then disappear), see below:

relevant-template-issues

@pandafy pandafy force-pushed the issues/332/345-relevant-templates branch from 5d8d033 to ab72ea9 Compare April 14, 2021 17:25
Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're still not there, the order of tempate (which is importat since templates coming after can override the ones coming before) is not retained correctly when operating on an existing device:

relevant-template-issues-2

On a new device, the drag and drop doesn't work for me:

drag-and-drop

@@ -373,7 +373,9 @@
// otherwise load immediately
bindLoadUi();
// fill device context field with default values of selected templates.
getDefaultValues(true);
setTimeout(function () {
Copy link
Member

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.
@pandafy
Copy link
Member Author

pandafy commented Apr 15, 2021

We're still not there, the order of tempate (which is importat since templates coming after can override the ones coming before) is not retained correctly when operating on an existing device:

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

On a new device, the drag and drop doesn't work for me:

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?

why the delayed call?

I was getting the following error occasionally when no templates were selected for the device(no default or required templates).

Screenshot from 2021-04-15 13-01-54

I have improved the implementation in 2a05dce. Instead of a delayed call. it will now be executed only when an event is fired.

Copy link
Member

@nemesifier nemesifier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@nemesifier nemesifier merged commit 79b9826 into master Apr 16, 2021
@nemesifier nemesifier deleted the issues/332/345-relevant-templates branch April 16, 2021 02:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
3 participants