[damned-lies] Reworked a bit the stats template tags



commit 21db88d821394aa0a9c6779c2e2fdd8ecd3004d9
Author: Claude Paroz <claude 2xlibre net>
Date:   Sat Feb 16 11:27:44 2019 +0100

    Reworked a bit the stats template tags

 common/static/css/template.css     |  6 +--
 stats/templatetags/stats_extras.py | 79 ++++++++++++++++++++------------------
 2 files changed, 44 insertions(+), 41 deletions(-)
---
diff --git a/common/static/css/template.css b/common/static/css/template.css
index 978112cf..670b90de 100644
--- a/common/static/css/template.css
+++ b/common/static/css/template.css
@@ -161,13 +161,13 @@ pre.stats {
     background-color: transparent;
     border: none;
 }
-pre.stats .num1 {
+.num1 {
     color: #4e9a06;
 }
-pre.stats .num2 {
+.num2 {
     color: #ce5c00;
 }
-pre.stats .num3 {
+.num3 {
     color: #a40000;
 }
 #stats_po {
diff --git a/stats/templatetags/stats_extras.py b/stats/templatetags/stats_extras.py
index e8f9f08d..ed83bc61 100644
--- a/stats/templatetags/stats_extras.py
+++ b/stats/templatetags/stats_extras.py
@@ -15,22 +15,26 @@ from stats.models import (PoFile,
 
 register = template.Library()
 
-STATISTICS = """
-<pre class="stats">
-<b>%(prc)3s%%</b>
-<span class="num1"> %(translated)6s</span>
-<span class="num2"> %(fuzzy)5s</span>
-<span class="num3"> %(untranslated)5s</span>
-</pre>
-"""
-# <pre> tags and line breaks don't play well, sigh...
-STATISTICS = STATISTICS.replace("\n", "")
-
-PROGRESS_BAR = """
-<div class="translated" style="width: %(trans)spx;"></div>
-<div class="fuzzy" style="%(dir)s:%(trans)spx; width:%(fuzzy)spx;"></div>
-<div class="untranslated" style="%(dir)s:%(tr_fu)spx; width: %(untrans)spx;"></div>
-"""
+STATISTICS_FULL = (
+    '<pre class="stats">'
+    '<b>{prc:>3}%</b>'
+    '<span class="num1"> {translated:>6}</span>'
+    '<span class="num2"> {fuzzy:>5}</span>'
+    '<span class="num3"> {untranslated:>5}</span>'
+    '</pre>'
+)
+
+STATISTICS_SHORT = (
+    '<span class="num1">{translated}</span>/'
+    '<span class="num2">{fuzzy}</span>/'
+    '<span class="num3">{untranslated}</span>'
+)
+
+PROGRESS_BAR = (
+    '<div class="translated" style="width: {trans}px;"></div>'
+    '<div class="fuzzy" style="{dir}: {trans}px; width: {fuzzy}px;"></div>'
+    '<div class="untranslated" style="{dir}: {tr_fu}px; width: {untrans}px;"></div>'
+)
 
 
 @register.filter
@@ -136,17 +140,18 @@ def num_stats_helper(stat, scope='full', strings=True):
     if 'translated_perc' in stats:
         stats['prc'] = stats['translated_perc']
     if 'prc' in stats:
-        result = STATISTICS % stats
+        template = STATISTICS_FULL
         if not show_zeros:
-            result = result.replace(' 0</span>', ' &nbsp;</span>')
+            stats = {k: (' ' if v == 0 and k != 'prc' else v) for k, v in stats.items()}
     else:
-        result = "%(translated)s/%(fuzzy)s/%(untranslated)s" % stats
-    return mark_safe(result)
+        template = STATISTICS_SHORT
+    return format_html(template, **stats)
 
 
 @register.filter
 def vis_stats(stat, scope='full'):
     """ Produce visual stats with green/red bar """
+    bidi = get_language_bidi() and "right" or "left"
     if isinstance(stat, (Statistics,
                          FakeLangStatistics,
                          FakeSummaryStatistics)):
@@ -162,18 +167,16 @@ def vis_stats(stat, scope='full'):
         fuzzy = stat['fuzzy_perc']
         untrans = stat['untranslated_perc']
     else:
-        text = '<div class="untranslated" style="%(dir)s:0px; width: 100px;">'
-        text += '</div>'
-        bidi = get_language_bidi() and "right" or "left"
-        return mark_safe(text % {'dir': bidi, })
+        text = '<div class="untranslated" style="{dir}: 0px; width: 100px;"></div>'
+        return format_html(text, dir=bidi)
 
-    return mark_safe(PROGRESS_BAR % {
-                     'dir': get_language_bidi() and "right" or "left",
-                     'trans': trans,
-                     'fuzzy': fuzzy,
-                     'tr_fu': trans + fuzzy,
-                     'untrans': untrans,
-                     })
+    return format_html(PROGRESS_BAR, **{
+        'dir': bidi,
+        'trans': trans,
+        'fuzzy': fuzzy,
+        'tr_fu': trans + fuzzy,
+        'untrans': untrans,
+    })
 
 
 @register.filter
@@ -194,13 +197,13 @@ def vis_word_stats(stat, scope='full'):
         fuzzy = stat['fuzzy_perc']
         untrans = stat['untranslated_perc']
 
-    return mark_safe(PROGRESS_BAR % {
-                     'dir': get_language_bidi() and "right" or "left",
-                     'trans': trans,
-                     'fuzzy': fuzzy,
-                     'tr_fu': trans + fuzzy,
-                     'untrans': untrans,
-                     })
+    return format_html(PROGRESS_BAR, **{
+        'dir': get_language_bidi() and "right" or "left",
+        'trans': trans,
+        'fuzzy': fuzzy,
+        'tr_fu': trans + fuzzy,
+        'untrans': untrans,
+    })
 
 
 @register.filter


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