Repository: translate/pootle

Issue:
Init from templates should respect template mapping
# Steps to reproduce:

- Legacy gnu projects may have a template not called `templates.pot` or similar
- If you init_from_templates it should convert to `lang.po` which it does only if the templates file is actually called templates

# Results (Expected/Actual):

- new files dont use the lang code, they use the templates filename

# Environment (i.e. 'pootle --version', DB, OS, Browser):

2.9



Discussion:

Resolving patch:
diff --git a/pootle/apps/pootle_project/models.py b/pootle/apps/pootle_project/models.py
--- a/pootle/apps/pootle_project/models.py
+++ b/pootle/apps/pootle_project/models.py
@@ -410,6 +410,14 @@ def delete(self, *args, **kwargs):
 
         directory.delete()
 
+    @property
+    def is_gnustyle(self):
+        mapping = self.config[
+            "pootle_fs.translation_mappings"]["default"]
+        return bool(
+            "/" not in mapping[
+                mapping.rfind("<language_code>") + 15:])
+
     # # # TreeItem
 
     def get_children(self):
diff --git a/pootle/apps/pootle_project/views.py b/pootle/apps/pootle_project/views.py
--- a/pootle/apps/pootle_project/views.py
+++ b/pootle/apps/pootle_project/views.py
@@ -263,14 +263,11 @@ def get_context_data(self, **kwargs_):
         layout_style = None
         if not self.formset_extra:
             layout_style = "nongnu"
-            mapping = self.object.config[
-                "pootle_fs.translation_mappings"]["default"]
-            gnustyle = (
-                "/" not in mapping[
-                    mapping.rfind("<language_code>") + 15:])
             template_path = self.object.lang_mapper.get_upstream_code("templates")
-            if gnustyle:
+            if self.object.is_gnustyle:
                 layout_style = "gnu"
+                mapping = self.object.config[
+                    "pootle_fs.translation_mappings"]["default"]
                 mapping_root, __ = posixpath.splitext(posixpath.basename(mapping))
                 template_path = mapping_root.replace(
                     "<language_code>", template_path)
diff --git a/pootle/apps/pootle_translationproject/models.py b/pootle/apps/pootle_translationproject/models.py
--- a/pootle/apps/pootle_translationproject/models.py
+++ b/pootle/apps/pootle_translationproject/models.py
@@ -252,7 +252,7 @@ def init_store_from_template(self, template_store):
             [posixpath.splitext(pootle_path)[0],
              template_store.filetype.extension.name])
         name = posixpath.basename(pootle_path)
-        if name in ["template.po", "templates.po"]:
+        if self.project.is_gnustyle:
             # gnu-style layout
             # use language code instead of template name
             name = ".".join(


Test patch:
diff --git a/tests/models/translationproject.py b/tests/models/translationproject.py
--- a/tests/models/translationproject.py
+++ b/tests/models/translationproject.py
@@ -78,68 +78,25 @@ def test_tp_create_templates(project0_nongnu, project0,
 
 
 @pytest.mark.django_db
-def test_tp_init_from_template_po(project0_nongnu, project0,
-                                  templates, no_templates_tps, complex_ttk):
+def test_tp_init_from_template_po(project0, templates,
+                                  no_templates_tps, complex_ttk):
     # When initing a tp from a file called `template.pot` the resulting
-    # store should be called `langcode.po`
-    template_tp = TranslationProject.objects.create(
-        language=templates, project=project0)
-    template = Store.objects.create(
-        name="template.pot",
-        translation_project=template_tp,
-        parent=template_tp.directory)
-    template.update(complex_ttk)
-    tp = TranslationProject.objects.create(
-        project=project0, language=LanguageDBFactory())
-    tp.init_from_templates()
-    store = tp.stores.get()
-    assert store.name == "%s.po" % tp.language.code
+    # store should be called `langcode.po` if the project is gnuish
+    project0.config["pootle_fs.translation_mappings"] = dict(
+        default="/<dir_path>/<language_code>.<ext>")
 
-
-@pytest.mark.django_db
-def test_tp_init_from_templates_po(project0_nongnu, project0,
-                                   templates, no_templates_tps, complex_ttk):
-    # When initing a tp from a file called `templates.pot` the resulting
-    # store should be called `langcode.po`
     template_tp = TranslationProject.objects.create(
         language=templates, project=project0)
     template = Store.objects.create(
-        name="templates.pot",
-        translation_project=template_tp,
-        parent=template_tp.directory)
-    template.update(complex_ttk)
-    tp = TranslationProject.objects.create(
-        project=project0, language=LanguageDBFactory())
-    tp.init_from_templates()
-    store = tp.stores.get()
-    assert store.name == "%s.po" % tp.language.code
-
-
-@pytest.mark.django_db
-def test_tp_init_from_templates_both(project0_nongnu, project0,
-                                     templates, no_templates_tps,
-                                     complex_ttk, store0):
-    # When initing a tp and there is both `template.pot` and
-    # `templates.pot` the first one added is used
-    template_tp = TranslationProject.objects.create(
-        language=templates, project=project0)
-    template1 = Store.objects.create(
-        name="templates.pot",
-        translation_project=template_tp,
-        parent=template_tp.directory)
-    template1.update(complex_ttk)
-    template2 = Store.objects.create(
         name="template.pot",
         translation_project=template_tp,
         parent=template_tp.directory)
-    template2.update(store0.deserialize(store0.serialize()))
+    template.update(complex_ttk)
     tp = TranslationProject.objects.create(
         project=project0, language=LanguageDBFactory())
     tp.init_from_templates()
     store = tp.stores.get()
     assert store.name == "%s.po" % tp.language.code
-    assert complex_ttk.units[1].target == store.units.first().target
-    assert store.units.count() == len(complex_ttk.units) - 1
 
 
 @pytest.mark.django_db