[damned-lies] Put graphic statistic display in a new vis_stats template tag



commit 40428c2a52f5de08ff1f90968602f065e8f60974
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Apr 7 18:21:07 2011 +0200

    Put graphic statistic display in a new vis_stats template tag
    
    Some stat display do still not use the template tag because they
    use a different base structure to get figures from (custom dicts).

 stats/models.py                                 |   18 ++-------------
 stats/templatetags/stats_extras.py              |   25 ++++++++++++++++++++++-
 templates/languages/language_release_stats.html |   15 ++++---------
 templates/stats_show.html                       |    8 +-----
 templates/vertimus/vertimus_detail.html         |   17 ++++++---------
 5 files changed, 41 insertions(+), 42 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index 58f1760..025fc36 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -1211,9 +1211,6 @@ class PoFile(models.Model):
         else:
             return int(100*self.untranslated/self.pot_size())
 
-    def translation_stat(self):
-        return "%d%%&nbsp;(%d/%d/%d)" % (self.tr_percentage(), self.translated, self.fuzzy, self.untranslated)
-
 
 class Statistics(models.Model):
     branch = models.ForeignKey(Branch)
@@ -1305,15 +1302,9 @@ class Statistics(models.Model):
             self.moddescription = self.branch.module.description or self.branch.module.name
         return self.moddescription
 
-    def get_translationstat(self):
-        return self.full_po.translation_stat()
-
     def has_reducedstat(self):
         return bool(self.part_po is not None and self.part_po != self.full_po)
 
-    def get_reducedstat(self):
-        return self.part_po.translation_stat()
-
     def filename(self, potfile=False, reduced=False):
         if not self.is_pot_stats() and not potfile:
             return "%s.%s.%s.%spo" % (self.domain.potbase(), self.branch.name, self.language.locale, reduced and "reduced." or "")
@@ -1656,9 +1647,6 @@ class FakeStatistics(object):
         else:
             return "pot file"
 
-    def get_translationstat(self):
-        return "%d%%&nbsp;(%d/%d/%d)" % (self.tr_percentage(), self._translated, self._fuzzy, self._untranslated)
-
     def fig_stats(self):
         stats = {'fuzzy':0, 'translated':0, 'untranslated':0, 'total':0, 'prc':0}
         for fig in self.get_figures():
@@ -1694,17 +1682,17 @@ class FakeStatistics(object):
 
     def pot_size(self):
         return int(self._translated) + int(self._fuzzy) + int(self._untranslated)
-    def tr_percentage(self):
+    def tr_percentage(self, scope='full'):
         if self.pot_size() == 0:
             return 0
         else:
             return int(100*self._translated/self.pot_size())
-    def fu_percentage(self):
+    def fu_percentage(self, scope='full'):
         if self.pot_size() == 0:
             return 0
         else:
             return int(100*self._fuzzy/self.pot_size())
-    def un_percentage(self):
+    def un_percentage(self, scope='full'):
         if self.pot_size() == 0:
             return 0
         else:
diff --git a/stats/templatetags/stats_extras.py b/stats/templatetags/stats_extras.py
index 8c717aa..699d1d8 100644
--- a/stats/templatetags/stats_extras.py
+++ b/stats/templatetags/stats_extras.py
@@ -1,5 +1,8 @@
 from django import template
 from django.utils.safestring import mark_safe
+from django.utils.translation import get_language_bidi
+
+from stats.models import PoFile
 
 register = template.Library()
 
@@ -33,9 +36,29 @@ def browse_bugs(module, content):
     return module.get_bugs_i18n_url(content)
 
 @register.filter
-def num_stats(stat, scope):
+def num_stats(stat, scope='full'):
     """ Produce stat numbers as in: 85% (1265/162/85) """
     return mark_safe("%s%%&nbsp;(%s/%s/%s)" % (
         stat.tr_percentage(scope), stat.translated(scope),
         stat.fuzzy(scope), stat.untranslated(scope))
     )
+
+ register filter
+def vis_stats(stat, scope='full'):
+    """ Produce visual stats with green/red bar """
+    if isinstance(stat, PoFile):
+        trans, fuzzy, untrans = stat.tr_percentage(), stat.fu_percentage(), stat.un_percentage()
+    else:
+        trans, fuzzy, untrans = stat.tr_percentage(scope), stat.fu_percentage(scope), stat.un_percentage(scope)
+    return mark_safe("""
+        <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>
+        """ % {
+          'dir'  : get_language_bidi() and "right" or "left",
+          'trans': stat.tr_percentage(scope),
+          'fuzzy': stat.fu_percentage(scope),
+          'tr_fu': stat.tr_percentage(scope) + stat.fu_percentage(scope),
+          'untrans': stat.un_percentage(scope),
+        })
+
diff --git a/templates/languages/language_release_stats.html b/templates/languages/language_release_stats.html
index 4c762cf..e031e90 100644
--- a/templates/languages/language_release_stats.html
+++ b/templates/languages/language_release_stats.html
@@ -67,15 +67,10 @@
         </td>
           <td><span class="branch">{{ branch }}</span></td>
           <td><span style="display:none;">{{ stat.tr_percentage }}</span>{{ stat|num_stats:scope }}</td>
-          <td style="width: 108px; text-align: center;"><div class="graph">
-              <div class="translated" style="width: {{ stat.tr_percentage }}px;"></div>
-              <div class="fuzzy" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ stat.tr_percentage }}px; width:{{ stat.fu_percentage }}px;"></div>
-              {% with stat.tr_percentage|add:stat.fu_percentage as upos %}
-              <div class="untranslated" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ upos }}px; width:{{ stat.un_percentage }}px;"></div>
-              {% endwith %}
-             </div>
-           </td>
-           <td>
+          <td style="width: 108px; text-align: center;">
+             <div class="graph">{{ stat|vis_stats:scope }}</div>
+          </td>
+          <td>
            {% if stat.state.name %}
              {% if stat.state.name != "None" %}
              <em><small>{{ stat.state.get_state }}</small></em></td>
@@ -88,7 +83,7 @@
              {% endif %}
            {% else %} </td><td>
            {% endif %}
-           </td>
+          </td>
         </tr>
       {% endif %}
       {% endwith %}
diff --git a/templates/stats_show.html b/templates/stats_show.html
index 75c4f74..d14fe52 100644
--- a/templates/stats_show.html
+++ b/templates/stats_show.html
@@ -61,18 +61,14 @@
         {% endif %}
         {% endwith %}
       </td>
-      <td>{{ line.get_translationstat|safe }}</td>
+      <td>{{ line|num_stats }}</td>
       {% if pot_stat.full_po.fig_count %}
       <td><a href="{% url stats.views.docimages module_name=module.name,potbase=pot_stat.domain.name,branch_name=branch.name,langcode=line.language.locale %}">
           <img src="{{ MEDIA_URL }}img/figure.png" alt="{% trans "Display document figures" %}"></a>
       </td>
       {% endif %}
       <td style="width: 108px; text-align: center;">
-        <div class="graph">
-          <div class="translated" style="width: {{ line.tr_percentage }}px;"></div>
-          <div class="fuzzy" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ line.tr_percentage }}px; width:{{ line.fu_percentage }}px;"></div>
-          <div class="untranslated" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ line.tr_percentage|add:line.fu_percentage }}px; width: {{ line.un_percentage }}px;"></div>
-        </div>
+        <div class="graph">{{ line|vis_stats }}</div>
       </td>
     </tr>
     {% endif %}
diff --git a/templates/vertimus/vertimus_detail.html b/templates/vertimus/vertimus_detail.html
index 6db56d3..566cc30 100644
--- a/templates/vertimus/vertimus_detail.html
+++ b/templates/vertimus/vertimus_detail.html
@@ -1,6 +1,7 @@
 {% extends "base.html" %}
 {% load i18n %}
 {% load humanize %}
+{% load stats_extras %}
 
 {% block title %}
 {% blocktrans with module.get_description as name %}Module Translation: {{ name }}{% endblocktrans %}
@@ -62,22 +63,18 @@ $(document).ready(function() {
     {{ pot_stats.pot_text }}
   </em></div>
   <div style="float:left">
-    <a href="{{ po_url }}"><img src="{{ MEDIA_URL }}img/download.png" alt="{% trans "Download PO file" %}" /></a> {% trans "Translated:" %}
-    {{ stats.get_translationstat|safe }}&nbsp;
+    <a href="{{ po_url }}"><img src="{{ MEDIA_URL }}img/download.png" alt="{% trans "Download PO file" %}" /></a>
+    {% trans "Translated:" %} {{ stats|num_stats:"full" }}&nbsp;
     <div class="graph graphinline">
-      <div class="translated" style="width: {{ stats.tr_percentage }}px;"></div>
-      <div class="fuzzy" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ stats.tr_percentage }}px; width:{{ stats.fu_percentage }}px;"></div>
-      <div class="untranslated" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ stats.tr_percentage|add:stats.fu_percentage }}px; width: {{ stats.un_percentage }}px;"></div>
+      {{ stats|vis_stats:"full" }}
     </div>
   </div>
   {% if po_url_reduced %}
   <div style="float:left; margin-left: 150px;">
-    <a href="{{ po_url_reduced }}"><img src="{{ MEDIA_URL }}img/download.png" alt="{% trans "Download PO file" %}" /></a> {% trans "Translated (reduced):" %}
-    {{ stats.get_reducedstat|safe }}&nbsp;
+    <a href="{{ po_url_reduced }}"><img src="{{ MEDIA_URL }}img/download.png" alt="{% trans "Download PO file" %}" /></a>
+    {% trans "Translated (reduced):" %} {{ stats|num_stats:"part" }}&nbsp;
     <div class="graph graphinline">
-      <div class="translated" style="width: {{ stats.part_po.tr_percentage }}px;"></div>
-      <div class="fuzzy" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ stats.part_po.tr_percentage }}px; width:{{ stats.part_po.fu_percentage }}px;"></div>
-      <div class="untranslated" style="{{ LANGUAGE_BIDI|yesno:"right,left" }}:{{ stats.part_po.tr_percentage|add:stats.part_po.fu_percentage }}px; width: {{ stats.part_po.un_percentage }}px;"></div>
+      {{ stats|vis_stats:"part" }}
     </div>
   </div>
   {% endif %}



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