Skip to content

[change] Show only relevant templates in device admin #345 - #398

Closed
niteshsinha17 wants to merge 4 commits into
openwisp:masterfrom
niteshsinha17:issues/345-show-relevant-templates
Closed

[change] Show only relevant templates in device admin #345#398
niteshsinha17 wants to merge 4 commits into
openwisp:masterfrom
niteshsinha17:issues/345-show-relevant-templates

Conversation

@niteshsinha17

Copy link
Copy Markdown
Member

@nemesisdesign I have implemented it. Please check it once. After that I will include test in it.
closes #345

@niteshsinha17
niteshsinha17 requested a review from nemesifier March 4, 2021 13:30
@niteshsinha17 niteshsinha17 self-assigned this Mar 4, 2021
@coveralls

coveralls commented Mar 4, 2021

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.008%) to 99.093% when pulling 72f0ac4 on niteshsinha17:issues/345-show-relevant-templates into 460c86c on openwisp:master.

Comment thread openwisp_controller/config/admin.py Outdated
url(
r'^config/get-relevant-templates/(?P<organization_id>[^/]+)/$',
get_relevant_templates,
name='get_relavant_templates',

@niteshsinha17 niteshsinha17 Mar 4, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I will correct this spelling mistake

@nemesifier nemesifier left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

looks good, can you:

  • add missing tests for the functions added
  • make sure that if a template is not relevant but is flagged as required, hence its checkbox is checked by default and disabled, we enable again the chekbox and remove the check?

@nemesifier nemesifier changed the title [enhancement] Show only relevant templates in device admin #345 [change] Show only relevant templates in device admin #345 Mar 6, 2021
@niteshsinha17
niteshsinha17 requested a review from nemesifier March 7, 2021 18:10

@atb00ker atb00ker left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Just one question. 😄

if (orgID.length === 0 || backend.length === 0) {
return;
}
var url = '/admin/config/device/config/get-relevant-templates/';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we try to avoid hardcoding the url here? 🤔
Perhaps a variable entered from the template.
@nemesisdesign What do you think?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, we can use a variable as we are using for get_default_templates. Should I add it?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, it's better because the URL will be automatically updated if we make changes in the python code. Thanks @atb00ker.

@nemesifier nemesifier left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Only this remaining: #398 (review)

@niteshsinha17

niteshsinha17 commented Mar 12, 2021

Copy link
Copy Markdown
Member Author

looks good, can you:

  • add missing tests for the functions added
  • make sure that if a template is not relevant but is flagged as required, hence its checkbox is checked by default and disabled, we enable again the chekbox and remove the check?

@nemesisdesign I have a doubt. I think we need to again disable it and need to make checkbox checked when the template become relevant on changing org or config backend. I have not added it yet but I have added it today in the code and waiting for your input. I will push the code as you agree.

@nemesifier

Copy link
Copy Markdown
Member

looks good, can you:

  • add missing tests for the functions added
  • make sure that if a template is not relevant but is flagged as required, hence its checkbox is checked by default and disabled, we enable again the chekbox and remove the check?

@nemesisdesign I have a doubt. I think we need to again disable it and need to make checkbox checked when the template become relevant on changing org or config backend. I have not added it yet but I have added it today in the code and waiting for your input. I will push the code as you agree.

@niteshsinha17 yes, I think you're right, go ahead, thanks

@niteshsinha17

Copy link
Copy Markdown
Member Author

@nemesisdesign Its ready now

@niteshsinha17
niteshsinha17 force-pushed the issues/345-show-relevant-templates branch from cae9306 to 72f0ac4 Compare March 13, 2021 06:34

@pandafy pandafy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Good work @niteshsinha17! I tested the PR manually and it works as expected.

I have suggested some minor code improvements and asked few queries. It will be great if you can answer them.

Comment on lines +41 to +56
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})

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to do a check like if organization_id in user.organization_managed or user.is_superuser? It will make sure that templates can be only be used by users of the same organization.

Please also add a test for this.

@pandafy pandafy Apr 6, 2021

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see this is a problem with get_default_templates view also. I will open a separate issue for this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Opened #420

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

damn 🤦

Comment on lines +65 to +66
{{ default_template_urls| safe }},
{{relevant_template_urls| safe}}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
{{ default_template_urls| safe }},
{{relevant_template_urls| safe}}
{{ default_template_urls | safe }},
{{ relevant_template_urls | safe }}

localStorage.getItem('config-disabled-templates', null);
disabledTemplates = JSON.parse(disabledTemplates) || {};
$('input.sortedm2m').each(function () {
if (($(this).val() in relevantTemplates) === false) {

Copy link
Copy Markdown
Member

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.

}
});
disabledTemplates = JSON.stringify(disabledTemplates);
localStorage.setItem('config-disabled-templates', disabledTemplates);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 disabledTemplates persistent helpful?

@pandafy

pandafy commented Apr 8, 2021

Copy link
Copy Markdown
Member

Hey @niteshsinha17!

I am working on a similar issue #332 and I believe that I can base it on your work.

Thank you very much for creating this PR, I will continue it from here. 🙌🏼

@niteshsinha17

niteshsinha17 commented Apr 8, 2021

Copy link
Copy Markdown
Member Author

Hey @niteshsinha17!

I am working on a similar issue #332 and I believe that I can base it on your work.

Thank you very much for creating this PR, I will continue it from here. 🙌🏼

@pandafy What should I do with this PR? Should I close it? 🤔

@pandafy

pandafy commented Apr 8, 2021

Copy link
Copy Markdown
Member

@pandafy What should I do with this PR? Should I continue it?

No, please leave it. I will continue the development on this. If you made any changes locally you can push them. Don't spend time on refining them since I will modify the implementation.

@pandafy

pandafy commented Apr 8, 2021

Copy link
Copy Markdown
Member

Closing in favour of #428.

@pandafy pandafy closed this Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

[change] Show only relevant templates in device admin

5 participants