[damned-lies] Delete stats when they exist no more on the file system (Fixes #657685)



commit 6b3df0e0bbd5bf847a7711f854ee5c073f31c9df
Author: Claude Paroz <claude 2xlibre net>
Date:   Wed Aug 31 11:05:14 2011 +0200

    Delete stats when they exist no more on the file system (Fixes #657685)

 stats/models.py         |    7 ++++++-
 stats/tests/__init__.py |    3 +++
 2 files changed, 9 insertions(+), 1 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index cdd2a38..269bbc9 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -470,7 +470,8 @@ class Branch(models.Model):
                 command = "msgmerge --previous -o %(outpo)s %(pofile)s %(potfile)s"
                 stats_with_ext_errors = Statistics.objects.filter(branch=self, domain=dom, information__type__endswith='-ext')
                 langs_with_ext_errors = [stat.language.locale for stat in stats_with_ext_errors]
-                for lang, pofile in dom.get_lang_files(self.co_path()):
+                dom_langs = dom.get_lang_files(self.co_path())
+                for lang, pofile in dom_langs:
                     outpo = os.path.join(self.output_dir(dom.dtype), dom.potbase() + "." + self.name + "." + lang + ".po")
 
                     if not force and changed_status in (utils.NOT_CHANGED, utils.CHANGED_ONLY_FORMATTING) and os.access(outpo, os.R_OK) \
@@ -520,6 +521,10 @@ class Branch(models.Model):
                                                figstats=fig_stats)
                     for err in langstats['errors']:
                         stat.information_set.add(Information(type=err[0], description=err[1]))
+                # Delete stats for unexisting langs
+                Statistics.objects.filter(branch=self, domain=dom
+                    ).exclude(models.Q(language__isnull=True) | models.Q(language__locale__in=[dl[0] for dl in dom_langs])
+                    ).delete()
             # Check if doap file changed
             if self.is_head() and self.file_changed("%s.doap" % self.module.name):
                 update_doap_infos(self.module)
diff --git a/stats/tests/__init__.py b/stats/tests/__init__.py
index 461716a..595e6a9 100644
--- a/stats/tests/__init__.py
+++ b/stats/tests/__init__.py
@@ -82,6 +82,8 @@ class ModuleTestCase(TestCase):
         self.assertEqual(self.branch.get_vcs_web_url(), "http://git.gnome.org/browse/gnome-hello/";)
 
     def testBranchStats(self):
+        lang = Language.objects.create(name='xxx', locale='xxx')
+        ghost_stat = Statistics.objects.create(branch=self.branch, domain=self.mod.domain_set.get(name='po'), language=lang)
         # Check stats
         self.branch.update_stats(force=True)
         fr_po_stat = Statistics.objects.get(branch=self.branch, domain__name='po', language__locale='fr')
@@ -91,6 +93,7 @@ class ModuleTestCase(TestCase):
         self.assertEqual(fr_po_stat.po_url(), u"/POT/gnome-hello.master/gnome-hello.master.fr.po")
         self.assertEqual(fr_po_stat.pot_url(), u"/POT/gnome-hello.master/gnome-hello.master.pot")
         self.assertEqual(fr_doc_stat.po_url(), u"/POT/gnome-hello.master/docs/gnome-hello-help.master.fr.po")
+        self.assertRaises(Statistics.DoesNotExist, Statistics.objects.get, pk=ghost_stat.pk)
 
     def testCreateAndDeleteBranch(self):
         Branch.checkout_on_creation = True



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