notifications: Fix issue with redirection on dismiss

The request variable is not available when a custom template is used to render a
notification. Fix this by passing the template rendering context additional
request variable.

Closes: #1887.

Tests:

- Reduce the version number in data for 'upgrades-new-release' notification
in the plinth_storednotification table in the DB. Start FreedomBox. New release
message will appear. Go to page other than home page. The dismiss button has
next= parameter filled properly with current URL. Dismiss the notification and
notice that page URL stays the same.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2020-06-25 15:47:14 -07:00 committed by James Valleroy
parent c3eac2c02e
commit 5a126e62a8
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 19 additions and 12 deletions

View File

@ -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]

View File

@ -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,

View File

@ -1 +1 @@
Test notification body
Test notification body {{request.path}}

View File

@ -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'