[damned-lies] Allow displaying doc images also for FakeStatistics



commit a6f049d500ad46ebea1f7a27048c34d1885c681e
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Mar 31 14:30:10 2010 +0200

    Allow displaying doc images also for FakeStatistics

 stats/models.py |   43 +++++++++++++++++++++++++++++++++++++++----
 stats/views.py  |   15 +++++++++------
 2 files changed, 48 insertions(+), 10 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index 71f5a17..382817d 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -265,7 +265,7 @@ class Branch(models.Model):
         for lang in mandatory_langs:
             for domain in stats.keys():
                 if lang not in stats_langs[domain]:
-                    fake_stat = FakeStatistics(self.module, typ, lang)
+                    fake_stat = FakeStatistics(self.module, self, typ, lang)
                     fake_stat.untranslated = stats[domain][0].untranslated
                     stats[domain].append(fake_stat)
         # Sort
@@ -1304,7 +1304,7 @@ class Statistics(models.Model):
                 # Here we add the 2nd or more stat to the same module-branch
                 if len(stats['categs'][categdescr]['modules'][modname][branchname]) == 2:
                     # Create a fake statistics object for module summary
-                    stats['categs'][categdescr]['modules'][modname][branchname][0][1] = FakeStatistics(stat.domain.module, dtype)
+                    stats['categs'][categdescr]['modules'][modname][branchname][0][1] = FakeStatistics(stat.domain.module, stat.branch, dtype)
                     stats['categs'][categdescr]['modules'][modname][branchname][0][1].trans(stats['categs'][categdescr]['modules'][modname][branchname][1][1])
                 stats['categs'][categdescr]['modules'][modname][branchname].append((domname, stat))
                 stats['categs'][categdescr]['modules'][modname][branchname][0][1].trans(stat)
@@ -1335,14 +1335,16 @@ class Statistics(models.Model):
 class FakeStatistics(object):
     """ This is a fake statistics class where a summary value is needed for a multi-domain module
         This is used in get_lang_stats for the language-release-stats template """
-    def __init__(self, module, dtype, lang=None):
+    def __init__(self, module, branch, dtype, lang=None):
         self.module = module
-        self.dtype = dtype
+        self.branch = branch
+        self.domain = module.domain_set.filter(dtype=dtype)[0]
         self.language = lang
         self.translated = 0
         self.fuzzy = 0
         self.untranslated = 0
         self.partial_po = False
+        self.figures = None
 
     def trans(self, stat):
         self.translated += stat.translated
@@ -1365,6 +1367,39 @@ class FakeStatistics(object):
     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():
+            stats['total'] += 1
+            stats['untranslated'] += 1
+        return stats
+
+    def get_figures(self):
+        """ self.figures is a list of dicts:
+            [{'path':, 'hash':, 'fuzzy':, 'translated':, 'translated_file':}, ...] """
+        if self.figures is None and self.domain.dtype == 'doc':
+            self.figures = utils.get_fig_stats(self.po_path())
+            # something like: "http://git.gnome.org/cgit/vinagre / plain / help / %s / %s ?h=master"
+            url_model = utils.url_join(self.branch.get_vcs_web_url(), self.branch.img_url_prefix,
+                                       self.domain.directory, '%s', '%s') + self.branch.img_url_suffix
+            for fig in self.figures:
+                fig['orig_remote_url'] = url_model % ('C', fig['path'])
+                fig['trans_remote_url'] = url_model % (self.language.locale, fig['path'])
+                fig['translated_file'] = False
+                if self.language:
+                    # Check if a translated figure really exists or if the English one is used
+                    if os.path.exists(os.path.join(self.branch.co_path(), self.domain.directory, self.language.locale, fig['path'])):
+                        fig['translated_file'] = True
+        return self.figures
+
+    def po_path(self):
+        """ Return path of pot file on local filesystem """
+        subdir = ""
+        if self.domain.dtype == "doc":
+            subdir = "docs"
+        filename = "%s.%s.pot" % (self.domain.potbase(), self.branch.name)
+        return os.path.join(settings.POTDIR, self.module_name()+'.'+self.branch.name, subdir, filename)
+
     def pot_size(self):
         return int(self.translated) + int(self.fuzzy) + int(self.untranslated)
     def tr_percentage(self):
diff --git a/stats/views.py b/stats/views.py
index 44d118b..a3ba4ef 100644
--- a/stats/views.py
+++ b/stats/views.py
@@ -29,7 +29,7 @@ from django.template import RequestContext
 from django.utils.translation import ugettext as _
 
 from common.utils import MIME_TYPES
-from stats.models import Statistics, Module, Branch, Category, Release
+from stats.models import Statistics, FakeStatistics, Module, Branch, Category, Release
 from stats.forms import ModuleBranchForm
 from stats import utils
 from languages.models import Language
@@ -143,11 +143,14 @@ def module_edit_branches(request, module_name):
 
 def docimages(request, module_name, potbase, branch_name, langcode):
     mod = get_object_or_404(Module, name=module_name)
-    stat = get_object_or_404(Statistics,
-                             branch__module=mod.id,
-                             branch__name=branch_name,
-                             domain__name=potbase,
-                             language__locale=langcode)
+    try:
+        stat = Statistics.objects.get(branch__module=mod.id,
+                                      branch__name=branch_name,
+                                      domain__name=potbase,
+                                      language__locale=langcode)
+    except Statistics.DoesNotExist:
+        lang = get_object_or_404(Language, locale=langcode)
+        stat = FakeStatistics(mod, mod.branch_set.get(name=branch_name), 'doc', lang)
     context = {
         'pageSection':  "module",
         'module': mod,



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