Skip to content

Commit

Permalink
[fix] Fixed ignore notification widget JS loading on add view #128
Browse files Browse the repository at this point in the history
Ensured ignore notification widget JS written in
"admin/change_form.html" only loads for change view of ModelAdmin

Closes #128
  • Loading branch information
pandafy committed Sep 18, 2020
1 parent 68e2b97 commit 1d9c8de
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
32 changes: 17 additions & 15 deletions openwisp_notifications/templates/admin/change_form.html
Expand Up @@ -11,23 +11,25 @@

{% block admin_change_form_document_ready %}
{{ block.super }}
<script>
const owNotifyAppLabel = '{{ opts.app_label }}',
owNotifyModelName = '{{ opts.model_name }}',
uuidRegex = /\/((\w{4,12}-?)){5}\//,
owIsChangeForm = true;
let objectId;
try{
objectId = uuidRegex.exec(window.location)[0];
}
catch(error){
{% if change %}
<script>
const owNotifyAppLabel = '{{ opts.app_label }}',
owNotifyModelName = '{{ opts.model_name }}',
uuidRegex = /\/((\w{4,12}-?)){5}\//,
owIsChangeForm = true;
let objectId;
try{
objectId = /\/(\d+)\//.exec(window.location)[0];
objectId = uuidRegex.exec(window.location)[0];
}
catch(error){
throw error;
try{
objectId = /\/(\d+)\//.exec(window.location)[0];
}
catch(error){
throw error;
}
}
}
const owNotifyObjectId = objectId.replace(/\//g,'');
</script>
const owNotifyObjectId = objectId.replace(/\//g,'');
</script>
{% endif %}
{% endblock %}
5 changes: 5 additions & 0 deletions openwisp_notifications/tests/test_admin.py
Expand Up @@ -282,3 +282,8 @@ def test_object_notification_setting_configured(self):
reverse('admin:openwisp_users_user_change', args=(self.admin.pk,))
)
UserAdmin.Media = None

def test_ignore_notification_widget_add_view(self):
url = reverse('admin:openwisp_users_organization_add')
response = self.client.get(url)
self.assertNotContains(response, 'owIsChangeForm')

0 comments on commit 1d9c8de

Please sign in to comment.