-
-
Notifications
You must be signed in to change notification settings - Fork 214
[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
[change] Show only relevant templates in device admin #345 #398
Conversation
openwisp_controller/config/admin.py
Outdated
url( | ||
r'^config/get-relevant-templates/(?P<organization_id>[^/]+)/$', | ||
get_relevant_templates, | ||
name='get_relavant_templates', |
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 will correct this spelling mistake
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.
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?
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.
Just one question. 😄
if (orgID.length === 0 || backend.length === 0) { | ||
return; | ||
} | ||
var url = '/admin/config/device/config/get-relevant-templates/'; |
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 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.
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?
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.
Yes, it's better because the URL will be automatically updated if we make changes in the python code. Thanks @atb00ker.
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.
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
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.
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.
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.
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 see this is a problem with get_default_templates
view also. I will open a separate issue for this.
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.
Opened #420
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.
damn 🤦
{{ default_template_urls| safe }}, | ||
{{relevant_template_urls| safe}} |
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.
{{ 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.
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); |
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'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