[damned-lies] Moved template tags to vertimus lib



commit e323da41455dd2f6bede76c00931cbb3c951b3cf
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Feb 16 11:52:40 2019 +0100

    Moved template tags to vertimus lib

 stats/templatetags/stats_extras.py      | 32 --------------------------
 templates/vertimus/vertimus_detail.html |  2 +-
 vertimus/templatetags/vertimus.py       | 40 +++++++++++++++++++++++++++++++++
 3 files changed, 41 insertions(+), 33 deletions(-)
---
diff --git a/stats/templatetags/stats_extras.py b/stats/templatetags/stats_extras.py
index ed83bc61..af23b6c3 100644
--- a/stats/templatetags/stats_extras.py
+++ b/stats/templatetags/stats_extras.py
@@ -211,38 +211,6 @@ def is_video(fig):
     return fig['path'].endswith('.ogv')
 
 
-@register.filter
-def as_tr(field):
-    help_html = ''
-    if field.help_text:
-        help_html = '<br><span class="help">%s</span>' % field.help_text
-    help_link = ''
-    # This is a custom attribute possibly set in forms.py
-    if hasattr(field.field, 'help_link'):
-        help_link = '<span class="help_link">'
-        help_link += '<a href="%s" data-target="#modal-container" role="button" data-toggle="modal">'
-        help_link += '<img src="%simg/help.png" alt="help icon">'
-        help_link += '</a></span>'
-        help_link = help_link % (field.field.help_link, settings.STATIC_URL)
-    errors_html = ''
-    if field.errors:
-        errors = ''.join(["%s" % err for err in field.errors])
-        errors_html = '%s' % errors
-
-    return mark_safe('<tr class="tr_%s"><th>%s</th><td>%s%s%s%s</td></tr>' % (
-        field.name, field.label_tag(), errors_html, field.as_widget(), help_link, help_html)
-    )
-
-@register.filter
-def as_tr_cb(field):
-    label = field.label
-    if field.help_text:
-        label = mark_safe('%s <span class="help">(%s)</span>' % (label, field.help_text))
-    return format_html(
-        '<tr class="tr_{}"><td></td><td>{} <label for="id_{}">{}</label></td></tr>',
-        field.name, field.as_widget(), field.name, label
-    )
-
 @register.filter(is_safe=True)
 def markdown(value, arg=''):
     """
diff --git a/templates/vertimus/vertimus_detail.html b/templates/vertimus/vertimus_detail.html
index a97c6b7a..7aec786f 100644
--- a/templates/vertimus/vertimus_detail.html
+++ b/templates/vertimus/vertimus_detail.html
@@ -1,5 +1,5 @@
 {% extends "base.html" %}
-{% load i18n humanize people stats_extras %}
+{% load i18n humanize people stats_extras vertimus %}
 
 {% block title %}
 {% blocktrans with module.get_description as name %}Module Translation: {{ name }}{% endblocktrans %}
diff --git a/vertimus/templatetags/vertimus.py b/vertimus/templatetags/vertimus.py
new file mode 100644
index 00000000..bca4588b
--- /dev/null
+++ b/vertimus/templatetags/vertimus.py
@@ -0,0 +1,40 @@
+from django import template
+from django.conf import settings
+from django.utils.html import format_html
+
+register = template.Library()
+
+
+@register.filter
+def as_tr(field):
+    help_html = ''
+    if field.help_text:
+        help_html = format_html('<br><span class="help">{}</span>', field.help_text)
+    help_link = ''
+    # This is a custom attribute possibly set in forms.py
+    if hasattr(field.field, 'help_link'):
+        help_link = format_html(
+            '<span class="help_link">'
+            '<a href="{}" data-target="#modal-container" role="button" data-toggle="modal">'
+            '<img src="{}img/help.png" alt="help icon">'
+            '</a></span>', field.field.help_link, settings.STATIC_URL
+        )
+    errors_html = ''
+    if field.errors:
+        errors_html = ''.join(["%s" % err for err in field.errors])
+
+    return format_html(
+        '<tr class="tr_{}"><th>{}</th><td>{}{}{}{}</td></tr>',
+        field.name, field.label_tag(), errors_html, field.as_widget(), help_link, help_html
+    )
+
+
+@register.filter
+def as_tr_cb(field):
+    label = field.label
+    if field.help_text:
+        label = format_html('{} <span class="help">({})</span>', label, field.help_text)
+    return format_html(
+        '<tr class="tr_{}"><td></td><td>{} <label for="id_{}">{}</label></td></tr>',
+        field.name, field.as_widget(), field.name, label
+    )


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]