[damned-lies] Allow module branches to filter the domains they can handle



commit 47600623ff396a3a6b78b33e81b4f3b9d294f5dc
Author: Claude Paroz <claude 2xlibre net>
Date:   Thu Oct 1 21:13:40 2015 +0200

    Allow module branches to filter the domains they can handle

 stats/admin.py                                     |   25 ++++++++++++++++-
 stats/fixtures/sample_data.json                    |   12 ++++++++
 stats/migrations/0006_add_domain_branch_from_to.py |   30 ++++++++++++++++++++
 stats/models.py                                    |   19 ++++++++++--
 stats/tests/tests.py                               |   23 +++++++++++++++
 5 files changed, 104 insertions(+), 5 deletions(-)
---
diff --git a/stats/admin.py b/stats/admin.py
index 46db227..b7a31bf 100644
--- a/stats/admin.py
+++ b/stats/admin.py
@@ -31,8 +31,25 @@ from stats.models import (
 class BranchInline(admin.TabularInline):
     model = Branch
 
-class DomainInline(admin.TabularInline):
+
+class DomainInline(admin.StackedInline):
     model = Domain
+    fieldsets = (
+        (None, {
+            'fields': (('name', 'description', 'dtype', 'directory'),)
+        }),
+        ('Advanced', {
+            'fields': ('pot_method', 'linguas_location', 'red_filter',
+                       ('branch_from', 'branch_to'),),
+            'classes': ('collapse',),
+        }),
+    )
+
+    def get_formset(self, request, obj=None, **kwargs):
+        # Hack! Store parent obj for formfield_for_foreignkey
+        self.parent_obj = obj
+        return super(DomainInline, self).get_formset(request, obj, **kwargs)
+
     def formfield_for_dbfield(self, db_field, **kwargs):
         if db_field.name == 'description':
             kwargs['widget'] = forms.Textarea(attrs={'rows':'1', 'cols':'20'})
@@ -42,6 +59,12 @@ class DomainInline(admin.TabularInline):
             kwargs['widget'] = forms.Textarea(attrs={'rows':'1', 'cols':'40'})
         return super(DomainInline, self).formfield_for_dbfield(db_field, **kwargs)
 
+    def formfield_for_foreignkey(self, db_field, request, **kwargs):
+        if db_field.name in ('branch_from', 'branch_to') and hasattr(self, 'parent_obj') and self.parent_obj:
+            kwargs['queryset'] = self.parent_obj.branch_set.all()
+        return super(DomainInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
+
+
 class ModuleAdmin(admin.ModelAdmin):
     fieldsets = (
         (None, {
diff --git a/stats/fixtures/sample_data.json b/stats/fixtures/sample_data.json
index 90b33e7..2f2aace 100644
--- a/stats/fixtures/sample_data.json
+++ b/stats/fixtures/sample_data.json
@@ -297,12 +297,14 @@
 },
 {
  "fields": {
+  "branch_from": null,
   "name": "po",
   "dtype": "ui",
   "pot_method": null,
   "module": 1,
   "linguas_location": null,
   "red_filter": null,
+  "branch_to": null,
   "directory": "po",
   "description": "UI Translations"
  },
@@ -311,12 +313,14 @@
 },
 {
  "fields": {
+  "branch_from": null,
   "name": "help",
   "dtype": "doc",
   "pot_method": null,
   "module": 1,
   "linguas_location": null,
   "red_filter": null,
+  "branch_to": null,
   "directory": "help",
   "description": "User Guide"
  },
@@ -325,12 +329,14 @@
 },
 {
  "fields": {
+  "branch_from": null,
   "name": "po",
   "dtype": "ui",
   "pot_method": null,
   "module": 2,
   "linguas_location": null,
   "red_filter": null,
+  "branch_to": null,
   "directory": "po",
   "description": "UI Translations"
  },
@@ -339,12 +345,14 @@
 },
 {
  "fields": {
+  "branch_from": null,
   "name": "help",
   "dtype": "doc",
   "pot_method": null,
   "module": 2,
   "linguas_location": null,
   "red_filter": null,
+  "branch_to": null,
   "directory": "help",
   "description": "User Guide"
  },
@@ -353,12 +361,14 @@
 },
 {
  "fields": {
+  "branch_from": null,
   "name": "po",
   "dtype": "ui",
   "pot_method": null,
   "module": 3,
   "linguas_location": null,
   "red_filter": null,
+  "branch_to": null,
   "directory": "po",
   "description": "UI Translations"
  },
@@ -367,12 +377,14 @@
 },
 {
  "fields": {
+  "branch_from": null,
   "name": "help",
   "dtype": "doc",
   "pot_method": null,
   "module": 3,
   "linguas_location": null,
   "red_filter": null,
+  "branch_to": null,
   "directory": "help",
   "description": "User Guide"
  },
diff --git a/stats/migrations/0006_add_domain_branch_from_to.py 
b/stats/migrations/0006_add_domain_branch_from_to.py
new file mode 100644
index 0000000..288d88b
--- /dev/null
+++ b/stats/migrations/0006_add_domain_branch_from_to.py
@@ -0,0 +1,30 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('stats', '0005_update_module_name_field'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='domain',
+            name='branch_from',
+            field=models.ForeignKey(related_name='+', on_delete=django.db.models.deletion.PROTECT, 
blank=True, to='stats.Branch', null=True),
+        ),
+        migrations.AddField(
+            model_name='domain',
+            name='branch_to',
+            field=models.ForeignKey(related_name='+', on_delete=django.db.models.deletion.PROTECT, 
blank=True, to='stats.Branch', null=True),
+        ),
+        migrations.AlterField(
+            model_name='domain',
+            name='pot_method',
+            field=models.CharField(help_text="Leave blank for standard method (intltool for UI and 
gnome-doc-utils for DOC), or '<gettext>' for the pure xgettext-based extraction", max_length=100, null=True, 
blank=True),
+        ),
+    ]
diff --git a/stats/models.py b/stats/models.py
index 608146b..21fa040 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -344,6 +344,17 @@ class Branch(models.Model):
             branch_dir = self.module.name + "." + self.name
         return os.path.join(settings.SCRATCHDIR, self.module.vcs_type, branch_dir)
 
+    def get_domains(self):
+        """
+        Return all domains that this branch applies to.
+        """
+        domains = []
+        for dom in Domain.objects.filter(module=self.module).select_related('branch_from', 
'branch_to').all():
+            if (dom.branch_from and self > dom.branch_from) or (dom.branch_to and self < dom.branch_to):
+                continue
+            domains.append(dom)
+        return domains
+
     def domain_path(self, domain):
         return os.path.join(self.co_path(), domain.directory)
 
@@ -410,10 +421,7 @@ class Branch(models.Model):
         with ModuleLock(self.module):
             if checkout:
                 self.checkout()
-            if domain is None:
-                domains = Domain.objects.filter(module=self.module).all()
-            else:
-                domains = [domain]
+            domains = [domain] if domain is not None else self.get_domains()
             string_frozen = self.has_string_frozen()
             for dom in domains:
                 # 1. Initial settings
@@ -708,6 +716,9 @@ class Domain(models.Model):
             Leave blank for standard location (ALL_LINGUAS in LINGUAS/configure.ac/.in for UI and 
DOC_LINGUAS in Makefile.am for DOC)""")
     red_filter = models.TextField(null=True, blank=True,
         help_text="""pogrep filter to strip po file from unprioritized strings (format: location|string, "-" 
for no filter)""")
+    # Allow to specify the branches to which this domain applies
+    branch_from = models.ForeignKey(Branch, null=True, blank=True, on_delete=models.PROTECT, 
related_name='+')
+    branch_to = models.ForeignKey(Branch, null=True, blank=True, on_delete=models.PROTECT, related_name='+')
 
     class Meta:
         db_table = 'domain'
diff --git a/stats/tests/tests.py b/stats/tests/tests.py
index f24f6fb..dac3e38 100644
--- a/stats/tests/tests.py
+++ b/stats/tests/tests.py
@@ -117,6 +117,29 @@ class ModuleTestCase(TestCase):
         self.assertEqual(self.branch.get_vcs_url(), "git://git.gnome.org/gnome-hello")
         self.assertEqual(self.branch.get_vcs_web_url(), "https://git.gnome.org/browse/gnome-hello/";)
 
+    def test_branch_domains(self):
+        """
+        Test that domains can be applied to a subset of all module branches,
+        based on the domain branch_form/branch_to fields.
+        """
+        domains = self.branch.get_domains()
+        self.assertEqual(set([d.name for d in domains]), {'po', 'help'})
+
+        b3 = Branch(name='gnome-3-3', module=self.mod)
+        b3.save(update_statistics=False)
+        b5 = Branch(name='gnome-3-5', module=self.mod)
+        b5.save(update_statistics=False)
+        b7 = Branch(name='gnome-3-7', module=self.mod)
+        b7.save(update_statistics=False)
+        help_domain = domains[0] if domains[0].name == 'help' else domains[1]
+        help_domain.branch_from = b5
+        help_domain.save()
+        self.assertEqual(set([d.name for d in b3.get_domains()]), {'po'})
+        self.assertEqual(set([d.name for d in b5.get_domains()]), {'po', 'help'})
+        help_domain.branch_to = b7
+        help_domain.save()
+        self.assertEqual(set([d.name for d in self.branch.get_domains()]), {'po'})
+
     @test_scratchdir
     def test_branch_stats(self):
         lang = Language.objects.create(name='xxx', locale='xxx')


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