[damned-lies] Small code refactoring for setting stat numbers



commit f880b134a1623d135500e5177b6ba1709d0c5eae
Author: Claude Paroz <claude 2xlibre net>
Date:   Fri Nov 13 22:13:37 2009 +0100

    Small code refactoring for setting stat numbers

 stats/models.py         |   36 +++++++++++++++++++++---------------
 stats/tests/__init__.py |    4 ++--
 2 files changed, 23 insertions(+), 17 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index cd56859..535822a 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -372,10 +372,7 @@ class Branch(models.Model):
             if potfile != previous_pot and not utils.copy_file(potfile, previous_pot):
                 errors.append(('error', ugettext_noop("Can't copy new POT file to public location.")))
 
-            stat.untranslated = int(pot_stats['untranslated'])
-            stat.num_figures = int(pot_stats['num_figures'])
-            stat.date = datetime.now()
-            stat.save()
+            stat.set_translation_stats(previous_pot, untranslated=int(pot_stats['untranslated']), num_figures=int(pot_stats['num_figures']))
             stat.set_errors(errors)
 
             # 6. Update language po files and update DB
@@ -414,11 +411,6 @@ class Branch(models.Model):
                 # Save in DB
                 try:
                     stat = Statistics.objects.get(language__locale=lang, branch=self, domain=dom)
-                    stat.translated = int(langstats['translated'])
-                    stat.fuzzy = int(langstats['fuzzy'])
-                    stat.untranslated = int(langstats['untranslated'])
-                    stat.num_figures = int(langstats['num_figures'])
-                    stat.date = datetime.now()
                     Information.objects.filter(statistics=stat).delete()
                 except Statistics.DoesNotExist:
                     try:
@@ -430,9 +422,13 @@ class Branch(models.Model):
                         else:
                             # Do not create language (and therefore ignore stats) for an 'old' branch
                             continue
-                    stat = Statistics(language = language, branch = self, domain = dom, translated = int(langstats['translated']),
-                                      fuzzy = int(langstats['fuzzy']), untranslated = int(langstats['untranslated']))
-                stat.save()
+                    stat = Statistics(language = language, branch = self, domain = dom)
+                    stat.save()
+                stat.set_translation_stats(outpo,
+                                           translated = int(langstats['translated']),
+                                           fuzzy = int(langstats['fuzzy']),
+                                           untranslated = int(langstats['untranslated']),
+                                           num_figures = int(langstats['num_figures']))
                 for err in langstats['errors']:
                     stat.information_set.add(Information(type=err[0], description=err[1]))
 
@@ -1078,13 +1074,15 @@ class Statistics(models.Model):
 
     def pot_text(self):
         """ Return stat table header: 'POT file (n messages) - updated on ??-??-???? tz' """
-        msg_text = ungettext(u"%(count)s message", "%(count)s messages", self.pot_size()) % {'count': self.pot_size()}
+        pot_size = self.pot_size()
+        fig_count = self.fig_count()
+        msg_text = ungettext(u"%(count)s message", "%(count)s messages", pot_size) % {'count': pot_size}
         upd_text = _(u"updated on %(date)s") % {
                         # Date format syntax is similar to PHP http://www.php.net/date
                         'date': dateformat.format(self.date, _("Y-m-d g:i a O"))
                         }
-        if self.fig_count():
-            fig_text = ungettext(u"%(count)s figure", "%(count)s figures", self.fig_count()) % {'count': self.fig_count()}
+        if fig_count:
+            fig_text = ungettext(u"%(count)s figure", "%(count)s figures", fig_count) % {'count': fig_count}
             text = _(u"POT file (%(messages)s, %(figures)s) â?? %(updated)s") % \
                               {'messages': msg_text, 'figures': fig_text, 'updated': upd_text}
         else:
@@ -1151,6 +1149,14 @@ class Statistics(models.Model):
     def pot_url(self):
         return self.po_url(potfile=True)
 
+    def set_translation_stats(self, po_path, translated=0, fuzzy=0, untranslated=0, num_figures=0):
+        self.translated = translated
+        self.fuzzy = fuzzy
+        self.untranslated = untranslated
+        self.num_figures = num_figures
+        self.date = datetime.now()
+        self.save()
+
     def set_errors(self, errors):
         for err in errors:
             self.information_set.add(Information(type=err[0], description=err[1]))
diff --git a/stats/tests/__init__.py b/stats/tests/__init__.py
index 273e72d..fbbad58 100644
--- a/stats/tests/__init__.py
+++ b/stats/tests/__init__.py
@@ -75,7 +75,7 @@ class ModuleTestCase(TestCase):
         fr_po_stat = Statistics.objects.get(branch=self.b, domain__name='po', language__locale='fr')
         self.assertEquals(fr_po_stat.translated, 40)
         fr_doc_stat = Statistics.objects.get(branch=self.b, domain__name='help', language__locale='fr')
-        self.assertEquals(fr_doc_stat.translated, 36)
+        self.assertEquals(fr_doc_stat.translated, 22)
 
     def testCreateAndDeleteBranch(self):
         Branch.checkout_on_creation = True
@@ -149,7 +149,7 @@ class ModuleTestCase(TestCase):
         self.b.update_stats(force=True) # At least POT stats needed
         c = Client()
         response = c.get('/module/po/gnome-hello.po.master.ta.po')
-        self.assertContains(response, """# Tamil translation of gnome-hello.
+        self.assertContains(response, """# Tamil translation for gnome-hello.
 # Copyright (C) 2009 gnome-hello's COPYRIGHT HOLDER
 # This file is distributed under the same license as the gnome-hello package.
 # FIRST AUTHOR <EMAIL ADDRESS>, YEAR.""")



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