-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
66 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,25 @@ | ||
from django.utils.safestring import mark_safe | ||
from django.utils.html import format_html | ||
from django.utils.translation import gettext as _ | ||
|
||
from .css import merge_css_classes | ||
from .html import render_tag | ||
from .text import text_value | ||
|
||
ALERT_TYPES = ["primary", "secondary", "succes", "danger", "warning", "info", "light", "dark"] | ||
|
||
def render_alert(content, alert_type=None, dismissible=True): | ||
|
||
def render_alert(content, alert_type="info", dismissible=True): | ||
"""Render a Bootstrap alert.""" | ||
button = "" | ||
if not alert_type: | ||
alert_type = "info" | ||
css_classes = ["alert", "alert-" + text_value(alert_type)] | ||
if alert_type not in ALERT_TYPES: | ||
raise ValueError(f"Value {alert_type} is not a valid alert type. Please choose from {', '.join(ALERT_TYPES)}.") | ||
css_classes = [f"alert alert-{alert_type}"] | ||
if dismissible: | ||
css_classes.append("alert-dismissible") | ||
css_classes.append("alert-dismissible fade show") | ||
close = _("close") | ||
button = ( | ||
'<button type="button" class="close" data-dismiss="alert" aria-label="{close}">×</button>' | ||
).format(close=close) | ||
button_placeholder = "__BUTTON__" | ||
return mark_safe( | ||
render_tag( | ||
"div", | ||
attrs={"class": " ".join(css_classes), "role": "alert"}, | ||
content=mark_safe(button_placeholder) + text_value(content), | ||
).replace(button_placeholder, button) | ||
button = f'<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="{close}"></button>' | ||
css_classes = merge_css_classes(*css_classes) | ||
return render_tag( | ||
"div", | ||
attrs={"class": css_classes, "role": "alert"}, | ||
content=format_html("{content}" + button, content=content), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from django.test import TestCase | ||
|
||
from .test_templates import render_template_with_bootstrap | ||
|
||
|
||
class BootstrapAlertTest(TestCase): | ||
def test_bootstrap_alert(self): | ||
self.assertEqual( | ||
render_template_with_bootstrap('{% bootstrap_alert "content" dismissible=False %}'), | ||
'<div class="alert alert-info" role="alert">content</div>', | ||
) | ||
|
||
def test_bootstrap_alert_dismissible(self): | ||
self.assertHTMLEqual( | ||
render_template_with_bootstrap('{% bootstrap_alert "content" %}'), | ||
( | ||
'<div class="alert alert-info alert-dismissible fade show" role="alert">' | ||
"content" | ||
'<button aria-label="close" class="btn-close" data-bs-dismiss="alert" type="button">' | ||
"</div>" | ||
), | ||
) | ||
|
||
def test_bootstrap_alert_html_content(self): | ||
self.assertEqual( | ||
render_template_with_bootstrap('{% bootstrap_alert "foo<br>bar" dismissible=False %}'), | ||
'<div class="alert alert-info" role="alert">foo<br>bar</div>', | ||
) | ||
self.assertEqual( | ||
render_template_with_bootstrap( | ||
"{% bootstrap_alert value dismissible=False %}", context={"value": "foo<br>bar"} | ||
), | ||
'<div class="alert alert-info" role="alert">foo<br>bar</div>', | ||
) | ||
self.assertEqual( | ||
render_template_with_bootstrap( | ||
"{% bootstrap_alert value|safe dismissible=False %}", context={"value": "foo<br>bar"} | ||
), | ||
'<div class="alert alert-info" role="alert">foo<br>bar</div>', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters