diff --git a/plinth/context_processors.py b/plinth/context_processors.py index a8ff94709..7793d9a64 100644 --- a/plinth/context_processors.py +++ b/plinth/context_processors.py @@ -23,7 +23,8 @@ def common(request): ugettext_noop('FreedomBox') from plinth.notification import Notification - notifications_context = Notification.get_display_context(user=request.user) + notifications_context = Notification.get_display_context( + request, user=request.user) slash_indices = [match.start() for match in re.finditer('/', request.path)] active_menu_urls = [request.path[:index + 1] for index in slash_indices] diff --git a/plinth/notification.py b/plinth/notification.py index 26c9c5858..77004ada0 100644 --- a/plinth/notification.py +++ b/plinth/notification.py @@ -316,12 +316,12 @@ class Notification(models.StoredNotification): return new_dict @staticmethod - def _render(template, data): + def _render(request, template, data): """Use the template name and render it.""" if not template: return None - context = dict(data, box_name=ugettext(cfg.box_name)) + context = dict(data, box_name=ugettext(cfg.box_name), request=request) try: return SimpleTemplateResponse(template, context).render() except TemplateDoesNotExist: @@ -329,7 +329,7 @@ class Notification(models.StoredNotification): return {'content': f'Template {template} does not exist.'.encode()} @staticmethod - def get_display_context(user): + def get_display_context(request, user): """Return a list of notifications meant for display to a user.""" notifications = Notification.list(user=user) max_severity = max(notifications, default=None, @@ -345,13 +345,14 @@ class Notification(models.StoredNotification): action['text'] = Notification._translate( action['text'], data) + body = Notification._render(request, note.body_template, data) notes.append({ 'id': note.id, 'app_id': note.app_id, 'severity': note.severity, 'title': Notification._translate(note.title, data), 'message': Notification._translate(note.message, data), - 'body': Notification._render(note.body_template, data), + 'body': body, 'actions': actions, 'data': data, 'created_time': note.created_time, diff --git a/plinth/tests/data/templates/test-notification.html b/plinth/tests/data/templates/test-notification.html index d966c62df..11e974ec7 100644 --- a/plinth/tests/data/templates/test-notification.html +++ b/plinth/tests/data/templates/test-notification.html @@ -1 +1 @@ -Test notification body +Test notification body {{request.path}} diff --git a/plinth/tests/test_notification.py b/plinth/tests/test_notification.py index b83d6d3b5..e7ba76582 100644 --- a/plinth/tests/test_notification.py +++ b/plinth/tests/test_notification.py @@ -335,8 +335,10 @@ def test_list_filter_user_and_group(note, user): @patch('plinth.notification.ugettext') -def test_display_context(ugettext, note, user): +def test_display_context(ugettext, note, user, rf): """Test display context for a notification.""" + request = rf.get('/plinth/help/about/') + data = { 'test-key1': 'test-value1', 'test-key2': 'translate:test-value2', @@ -370,7 +372,7 @@ def test_display_context(ugettext, note, user): note.data = data note.save() - context = Notification.get_display_context(user) + context = Notification.get_display_context(request, user) assert len(context['notifications']) == 1 assert context['max_severity'] == 'error' @@ -392,12 +394,14 @@ def test_display_context(ugettext, note, user): assert not context_note['dismissed'] -def test_display_context_body_template(note, user, load_cfg): +def test_display_context_body_template(note, user, load_cfg, rf): """Test display context for a notification with body template.""" + request = rf.get('/plinth/help/about/') + note.body_template = 'invalid-template.html' note.save() - context = Notification.get_display_context(user) + context = Notification.get_display_context(request, user) assert context['notifications'][0]['body'] == { 'content': b'Template invalid-template.html does not exist.' } @@ -405,6 +409,7 @@ def test_display_context_body_template(note, user, load_cfg): note.body_template = 'test-notification.html' note.save() - context = Notification.get_display_context(user) + context = Notification.get_display_context(request, user) context_note = context['notifications'][0] - assert context_note['body'].content == b'Test notification body\n' + assert context_note['body'].content == \ + b'Test notification body /plinth/help/about/\n'