[damned-lies] Make stats_extras.py more readable
- From: Gil Forcada Codinachs <gforcada src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [damned-lies] Make stats_extras.py more readable
- Date: Sun, 14 Oct 2012 17:32:57 +0000 (UTC)
commit 6b5a83a5ecbfd33f63d87b77fc9bc2b9d55d96ff
Author: Gil Forcada <gforcada gnome org>
Date: Sun Oct 14 13:16:53 2012 +0200
Make stats_extras.py more readable
stats/templatetags/stats_extras.py | 169 ++++++++++++++++++++++++------------
1 files changed, 113 insertions(+), 56 deletions(-)
---
diff --git a/stats/templatetags/stats_extras.py b/stats/templatetags/stats_extras.py
index a56837a..9913c69 100644
--- a/stats/templatetags/stats_extras.py
+++ b/stats/templatetags/stats_extras.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
import re
from django import template
@@ -5,16 +6,39 @@ from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.translation import get_language_bidi
-from stats.models import PoFile, Statistics, FakeLangStatistics, FakeSummaryStatistics
+from stats.models import (PoFile,
+ Statistics,
+ FakeLangStatistics,
+ FakeSummaryStatistics, )
+
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>
+"""
+
+
@register.filter
def linked_with(value, arg):
- """ This filter returns an object (passed in value) enclosed with his absolute url
- arg is the linked text """
+ """ This filter returns an object (passed in value) enclosed with its
+ absolute url arg is the linked text """
return "<a href='%s'>%s</a>" % (value.get_absolute_url(), arg)
+
@register.filter
def support_class(value):
""" Returns a class depending on the coverage of the translation stats.
@@ -25,6 +49,7 @@ def support_class(value):
return "partially"
return "not_supported"
+
@register.filter
def support_class_total(stats):
""" Retuns a class depending on the trend. """
@@ -38,33 +63,39 @@ def support_class_total(stats):
else:
return "not_supported"
+
@register.filter
def escapeat(value):
"""Replace '@' with '__', accepted sequence in JS ids."""
return value.replace('@', '__')
+
@register.filter
def domain_type(stat):
return stat.domain.get_type(stat.branch)
+
@register.filter
def browse_bugs(module, content):
return module.get_bugs_i18n_url(content)
+
@register.filter
def num_stats(stat, scope='full'):
""" Produce stat numbers as in: 85% (1265/162/85) """
- if isinstance(stat, (Statistics, FakeLangStatistics, FakeSummaryStatistics)):
+ if isinstance(stat, (Statistics,
+ FakeLangStatistics,
+ FakeSummaryStatistics)):
stats = {
- 'prc': stat.tr_percentage(scope),
- 'translated': stat.translated(scope),
- 'fuzzy': stat.fuzzy(scope),
+ 'prc': stat.tr_percentage(scope),
+ 'translated': stat.translated(scope),
+ 'fuzzy': stat.fuzzy(scope),
'untranslated': stat.untranslated(scope),
}
elif isinstance(stat, PoFile):
stats = {
- 'translated': stat.translated,
- 'fuzzy': stat.fuzzy,
+ 'translated': stat.translated,
+ 'fuzzy': stat.fuzzy,
'untranslated': stat.untranslated,
}
if scope != 'short':
@@ -73,27 +104,32 @@ def num_stats(stat, scope='full'):
stats = stat
if 'translated_perc' in stats:
stats['prc'] = stats['translated_perc']
+
if 'prc' in stats:
- result = '<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>' % stats
+ result = STATISTICS % stats
result = result.replace(' 0</span>', ' </span>')
else:
result = "%(translated)s/%(fuzzy)s/%(untranslated)s" % stats
+
return mark_safe(result)
+
@register.filter
def num_word_stats(stat, scope='full'):
""" Produce stat numbers as in: 85% (1265/162/85) """
- if isinstance(stat, (Statistics, FakeLangStatistics, FakeSummaryStatistics)):
+ if isinstance(stat, (Statistics,
+ FakeLangStatistics,
+ FakeSummaryStatistics)):
stats = {
- 'prc': stat.tr_word_percentage(scope),
- 'translated': stat.translated_words(scope),
- 'fuzzy': stat.fuzzy_words(scope),
+ 'prc': stat.tr_word_percentage(scope),
+ 'translated': stat.translated_words(scope),
+ 'fuzzy': stat.fuzzy_words(scope),
'untranslated': stat.untranslated_words(scope),
}
elif isinstance(stat, PoFile):
stats = {
- 'translated': stat.translated_words,
- 'fuzzy': stat.fuzzy_words,
+ 'translated': stat.translated_words,
+ 'fuzzy': stat.fuzzy_words,
'untranslated': stat.untranslated_words,
}
if scope != 'short':
@@ -103,62 +139,77 @@ def num_word_stats(stat, scope='full'):
if 'translated_perc' in stats:
stats['prc'] = stats['translated_perc']
if 'prc' in stats:
- result = '<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>' % stats
+ result = STATISTICS % stats
result = result.replace(' 0</span>', ' </span>')
else:
result = "%(translated)s/%(fuzzy)s/%(untranslated)s" % stats
return mark_safe(result)
+
@register.filter
def vis_stats(stat, scope='full'):
""" Produce visual stats with green/red bar """
- if isinstance(stat, (Statistics, FakeLangStatistics, FakeSummaryStatistics)):
- trans, fuzzy, untrans = stat.tr_percentage(scope), stat.fu_percentage(scope), stat.un_percentage(scope)
+ if isinstance(stat, (Statistics,
+ FakeLangStatistics,
+ FakeSummaryStatistics)):
+ trans = stat.tr_percentage(scope)
+ fuzzy = stat.fu_percentage(scope)
+ untrans = stat.un_percentage(scope)
elif isinstance(stat, PoFile):
- trans, fuzzy, untrans = stat.tr_percentage(), stat.fu_percentage(), stat.un_percentage()
+ trans = stat.tr_percentage()
+ fuzzy = stat.fu_percentage()
+ untrans = stat.un_percentage()
elif isinstance(stat, dict):
- trans, fuzzy, untrans = stat['translated_perc'], stat['fuzzy_perc'], stat['untranslated_perc']
+ trans = stat['translated_perc']
+ fuzzy = stat['fuzzy_perc']
+ untrans = stat['untranslated_perc']
else:
- return mark_safe("""<div class="untranslated" style="%(dir)s:0px; width: 100px;"></div>""" % {
- 'dir' : get_language_bidi() and "right" or "left"
- })
- 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': trans,
- 'fuzzy': fuzzy,
- 'tr_fu': trans + fuzzy,
- 'untrans': untrans,
- })
+ 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, })
+
+ return mark_safe(PROGRESS_BAR % {
+ 'dir': get_language_bidi() and "right" or "left",
+ 'trans': trans,
+ 'fuzzy': fuzzy,
+ 'tr_fu': trans + fuzzy,
+ 'untrans': untrans,
+ })
+
@register.filter
def vis_word_stats(stat, scope='full'):
""" Produce visual stats with green/red bar """
- if isinstance(stat, (Statistics, FakeLangStatistics, FakeSummaryStatistics)):
- trans, fuzzy, untrans = stat.tr_word_percentage(scope), stat.fu_word_percentage(scope), stat.un_word_percentage(scope)
+ if isinstance(stat, (Statistics,
+ FakeLangStatistics,
+ FakeSummaryStatistics)):
+ trans = stat.tr_word_percentage(scope)
+ fuzzy = stat.fu_word_percentage(scope)
+ untrans = stat.un_word_percentage(scope)
elif isinstance(stat, PoFile):
- trans, fuzzy, untrans = stat.tr_word_percentage(), stat.fu_word_percentage(), stat.un_word_percentage()
+ trans = stat.tr_word_percentage()
+ fuzzy = stat.fu_word_percentage()
+ untrans = stat.un_word_percentage()
else:
- trans, fuzzy, untrans = stat['translated_perc'], stat['fuzzy_perc'], stat['untranslated_perc']
- 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': trans,
- 'fuzzy': fuzzy,
- 'tr_fu': trans + fuzzy,
- 'untrans': untrans,
- })
+ trans = stat['translated_perc']
+ 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,
+ })
+
@register.filter
def is_video(fig):
return fig['path'].endswith('.ogv')
+
@register.filter
def as_tr(field):
help_html = u''
@@ -167,19 +218,25 @@ def as_tr(field):
help_link = u''
# This is a custom attribute possibly set in forms.py
if hasattr(field.field, 'help_link'):
- help_link = '<span class="help_link"><a class="show-overlay" href="%s"><img src="%simg/help.png" alt="help icon"></a></span>' % (
- field.field.help_link, settings.STATIC_URL)
+ help_link = '<span class="help_link">'
+ help_link += '<a class="show-overlay" href="%s">'
+ 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 = u''
if field.errors:
- errors_html = u'<ul class="errorlist">%s</ul>' % u''.join(["<li>%s</li>" % err for err in field.errors])
+ errors = u''.join(["<li>%s</li>" % err for err in field.errors])
+ errors_html = u'<ul class="errorlist">%s</ul>' % errors
+
return mark_safe(u'<tr><th>%s:</th><td>%s%s%s%s</td></tr>' % (
- field.label_tag(), errors_html, field.as_widget(), help_link, help_html)
+ field.label_tag(), errors_html, field.as_widget(), link, help_html)
)
+
@register.filter
def bugzilla_linkify(text):
- """Searches within the given text if a text matching a bugzilla bug reference
- is found and transforms it to a link to that bug"""
+ """Searches within the given text if a text matching a bugzilla bug
+ reference is found and transforms it to a link to that bug"""
# Should not catch encoded entities (')
bug_id = re.compile('(?<!&)(?P<id>#[0-9]+)')
repl = u'<a rel="nofollow" href="https://bugzilla.gnome.org/show_bug.cgi?id=\g<id>">\g<id></a>'
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]