notification: Pass full context when rendering body template

- Important information such as id of the notification should be available when
rendering the body template.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-07-30 15:37:26 -07:00 committed by James Valleroy
parent 5f8d3e30d0
commit fee21cba0d
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 11 additions and 8 deletions

View File

@ -10,14 +10,14 @@
</div>
<p>
{% blocktrans trimmed with url="https://discuss.freedombox.org/c/announcements/7" %}
{% blocktrans trimmed with url="https://discuss.freedombox.org/c/announcements/7" version=data.version %}
{{ box_name }} has been updated to version {{ version }}. See the
<a href="{{ url }}">release announcement</a>.
{% endblocktrans %}
</p>
<p>
<a href="{% url 'notification_dismiss' id='upgrades-new-release' %}?next={{ request.path|iriencode }}"
<a href="{% url 'notification_dismiss' id=id %}?next={{ request.path|iriencode }}"
role="button" class="btn btn-default">
{% trans "Dismiss" %}
</a>

View File

@ -316,12 +316,13 @@ class Notification(models.StoredNotification):
return new_dict
@staticmethod
def _render(request, template, data):
def _render(request, template, context):
"""Use the template name and render it."""
if not template:
return None
context = dict(data, box_name=gettext(cfg.box_name), request=request)
context = dict(context, box_name=gettext(cfg.box_name),
request=request)
try:
return SimpleTemplateResponse(template, context).render()
except TemplateDoesNotExist:
@ -345,14 +346,12 @@ class Notification(models.StoredNotification):
action['text'] = Notification._translate(
action['text'], data)
body = Notification._render(request, note.body_template, data)
notes.append({
note_context = {
'id': note.id,
'app_id': note.app_id,
'severity': note.severity,
'title': Notification._translate(note.title, data),
'message': Notification._translate(note.message, data),
'body': body,
'actions': actions,
'data': data,
'created_time': note.created_time,
@ -360,6 +359,10 @@ class Notification(models.StoredNotification):
'user': note.user,
'group': note.group,
'dismissed': note.dismissed,
})
}
body = Notification._render(request, note.body_template,
note_context)
note_context['body'] = body
notes.append(note_context)
return {'notifications': notes, 'max_severity': max_severity}