[change] Show only relevant templates in device admin #345 - #398
[change] Show only relevant templates in device admin #345#398niteshsinha17 wants to merge 4 commits into
Conversation
| url( | ||
| r'^config/get-relevant-templates/(?P<organization_id>[^/]+)/$', | ||
| get_relevant_templates, | ||
| name='get_relavant_templates', |
There was a problem hiding this comment.
I will correct this spelling mistake
nemesifier
left a comment
There was a problem hiding this comment.
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?
| if (orgID.length === 0 || backend.length === 0) { | ||
| return; | ||
| } | ||
| var url = '/admin/config/device/config/get-relevant-templates/'; |
There was a problem hiding this comment.
Can we try to avoid hardcoding the url here? 🤔
Perhaps a variable entered from the template.
@nemesisdesign What do you think?
There was a problem hiding this comment.
Yes, we can use a variable as we are using for get_default_templates. Should I add it?
There was a problem hiding this comment.
Yes, it's better because the URL will be automatically updated if we make changes in the python code. Thanks @atb00ker.
nemesifier
left a comment
There was a problem hiding this comment.
Only this remaining: #398 (review)
@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 |
@niteshsinha17 yes, I think you're right, go ahead, thanks |
|
@nemesisdesign Its ready now |
cae9306 to
72f0ac4
Compare
pandafy
left a comment
There was a problem hiding this comment.
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.
| 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}) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I see this is a problem with get_default_templates view also. I will open a separate issue for this.
| {{ default_template_urls| safe }}, | ||
| {{relevant_template_urls| safe}} |
There was a problem hiding this comment.
| {{ 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) { |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
Can't we just keep it in the memory instead of using localStorage? How making disabledTemplates persistent helpful?
|
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? 🤔 |
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. |
|
Closing in favour of #428. |
@nemesisdesign I have implemented it. Please check it once. After that I will include test in it.
closes #345