integrate nonce argument into register_notify_callbacks

This commit is contained in:
Andrew Hall 2021-01-15 14:41:43 +11:00 committed by Alvaro Leonel
parent 8d82857ff4
commit 00f0c8540e
3 changed files with 9 additions and 4 deletions

View file

@ -41,7 +41,9 @@ def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disabl
refresh_period=15,
callbacks='',
api_name='list',
fetch=5):
fetch=5,
nonce=None
):
refresh_period = int(refresh_period) * 1000
if api_name == 'list':
@ -68,7 +70,10 @@ def register_notify_callbacks(badge_class='live_notify_badge', # pylint: disabl
fetch_count=fetch
)
script = "<script>" + definitions
# add a nonce value to the script tag if one is provided
nonce_str = f' nonce="{nonce}"' if nonce is not None else ""
script = f'<script type="text/javascript"{nonce_str}>' + definitions
for callback in callbacks.split(','):
script += "register_notifier(" + callback + ");"
script += "</script>"

View file

@ -2,7 +2,7 @@
<script src="{% static 'notifications/notify.js' %}" type="text/javascript"></script>
<script src="{% static 'notifications/live-test.js' %}" type="text/javascript"></script>
{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' fetch=20 refresh_period=5 %}
{% register_notify_callbacks callbacks='fill_notification_list,fill_notification_badge' fetch=20 refresh_period=5 nonce='{{nonce}}' %}
There are this many notifications pending: {% live_notify_badge %}

View file

@ -436,7 +436,7 @@ class NotificationTestPages(TestCase):
request = factory.get('/notification/live_updater')
request.user = self.to_user
render(request, 'notifications/test_tags.html', {'request': request})
render(request, 'notifications/test_tags.html', {'request': request, 'nonce': 'nonce-T5esDNXMnDe5lKMQ6ZzTUw=='})
# TODO: Add more tests to check what is being output.