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> </div>
<p> <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 {{ box_name }} has been updated to version {{ version }}. See the
<a href="{{ url }}">release announcement</a>. <a href="{{ url }}">release announcement</a>.
{% endblocktrans %} {% endblocktrans %}
</p> </p>
<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"> role="button" class="btn btn-default">
{% trans "Dismiss" %} {% trans "Dismiss" %}
</a> </a>

View File

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