mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-03-11 09:04:54 +00:00
views: Implement a utility to easily show error message
Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
63fbd20692
commit
8a5a9d07ad
@ -4,6 +4,7 @@ Main FreedomBox views.
|
||||
"""
|
||||
|
||||
import datetime
|
||||
import random
|
||||
import time
|
||||
import urllib.parse
|
||||
|
||||
@ -14,6 +15,7 @@ from django.http import Http404, HttpResponseBadRequest, HttpResponseRedirect
|
||||
from django.shortcuts import redirect
|
||||
from django.template.response import TemplateResponse
|
||||
from django.urls import reverse
|
||||
from django.utils.html import format_html
|
||||
from django.utils.translation import gettext as _
|
||||
from django.views.decorators.http import require_POST
|
||||
from django.views.generic import TemplateView
|
||||
@ -59,6 +61,25 @@ def is_safe_url(url):
|
||||
return True
|
||||
|
||||
|
||||
def messages_error(request, message, exception):
|
||||
"""Show an error message using Django messages framework.
|
||||
|
||||
If an exception can show HTML message, handle is separately.
|
||||
"""
|
||||
if hasattr(exception, 'get_html_message'):
|
||||
collapse_id = 'error-details-' + str(random.randint(0, 10**9))
|
||||
message = format_html(
|
||||
'{message} <a href="#" class="dropdown-toggle" '
|
||||
'data-toggle="collapse" data-target="#{collapse_id}" '
|
||||
'aria-expanded="false" aria-controls="{collapse_id}">'
|
||||
'Details</a><pre class="collapse" '
|
||||
'id="{collapse_id}"><code>{html_message}</code></pre>',
|
||||
message=message, html_message=exception.get_html_message(),
|
||||
collapse_id=collapse_id)
|
||||
|
||||
messages.error(request, message)
|
||||
|
||||
|
||||
def _get_redirect_url_from_param(request):
|
||||
"""Return the redirect URL from 'next' GET/POST param."""
|
||||
redirect_to = request.GET.get(REDIRECT_FIELD_NAME, '')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user