[damned-lies] Set ability to commit new files depending on LINGUAS presence



commit 6b8c6c92f57194e35e49521e94bb37731f1ed6ff
Author: Claude Paroz <claude 2xlibre net>
Date:   Tue Sep 4 18:50:33 2018 +0200

    Set ability to commit new files depending on LINGUAS presence

 stats/models.py    | 24 ++++++++++++++++--------
 vertimus/models.py | 14 ++++++++++----
 2 files changed, 26 insertions(+), 12 deletions(-)
---
diff --git a/stats/models.py b/stats/models.py
index a20fd23e..76a6b5e5 100644
--- a/stats/models.py
+++ b/stats/models.py
@@ -640,8 +640,12 @@ class Branch(models.Model):
             self.update_repo()
             if vcs_type == "git":
                 already_exist = dest_full_path.exists()
-                if not already_exist and domain.dtype != 'ui':
-                    raise Exception("Sorry, adding new translations for documentation is not yet supported.")
+                if not already_exist:
+                    linguas_path = domain.get_linguas_path(self)
+                    if linguas_path is None:
+                        raise Exception(
+                            _("Sorry, adding new translations when the LINGUAS file is not known is not 
supported.")
+                        )
 
                 # Copy file in repo
                 shutil.copyfile(str(po_file), str(dest_full_path))
@@ -651,12 +655,11 @@ class Branch(models.Model):
                     ['git', 'add', dest_path], raise_on_error=True, cwd=base_path)
                 if not already_exist:
                     # Add locale to LINGUAS
-                    linguas_path = os.path.join(domain.base_dir, "LINGUAS")
-                    linguas_file = base_path / linguas_path
-                    if linguas_file.exists():
-                        utils.insert_locale_in_linguas(linguas_file, locale)
-                        run_shell_command(
-                            ['git', 'add', linguas_path], raise_on_error=True, cwd=base_path)
+                    utils.insert_locale_in_linguas(linguas_path, locale)
+                    run_shell_command(
+                        ['git', 'add', str(linguas_path.relative_to(base_path))],
+                        raise_on_error=True, cwd=base_path
+                    )
                     msg = "Add %s translation" % language.name
                 else:
                     msg = "Update %s translation" % language.name
@@ -972,6 +975,11 @@ class Domain(models.Model):
             pot_command.extend(['--msgid-bugs-address', bugs_url])
         return pot_command, env
 
+    def get_linguas_path(self, branch):
+        """Return a LINGUAS file path, or None."""
+        path = branch.co_path / self.base_dir / 'LINGUAS'
+        return path if path.exists() else None
+
     def get_linguas(self, branch):
         """ Return a linguas dict like this: {'langs':['lang1', lang2], 'error':"Error"} """
         base_path = branch.co_path
diff --git a/vertimus/models.py b/vertimus/models.py
index 9bf62ef8..5996f75a 100644
--- a/vertimus/models.py
+++ b/vertimus/models.py
@@ -73,6 +73,13 @@ class State(models.Model):
         except Statistics.DoesNotExist:
             return None
 
+    def able_to_commit(self):
+        return (
+            not self.branch.is_vcs_readonly() and
+            self.get_latest_po_file_action() is not None and
+            (self.stats is not None or self.domain.get_linguas_path(self.branch) is not None)
+        )
+
     def change_state(self, state_class, person=None):
         self.name = state_class._name
         self.person = person
@@ -211,8 +218,7 @@ class StateProofread(State):
             action_names = []
         if person.is_committer(self.language.team):
             action_names.append('RC')
-            if (not self.branch.is_vcs_readonly()
-                    and self.get_latest_po_file_action() is not None):
+            if self.able_to_commit():
                 action_names.insert(1, 'CI')
 
         return self._get_available_actions(person, action_names)
@@ -243,7 +249,7 @@ class StateToCommit(State):
     def get_available_actions(self, person):
         if person.is_committer(self.language.team):
             action_names = ['RC', 'TR', 'UNDO']
-            if not self.branch.is_vcs_readonly() and self.get_latest_po_file_action() is not None:
+            if self.able_to_commit():
                 action_names.insert(1, 'CI')
         else:
             action_names = []
@@ -264,7 +270,7 @@ class StateCommitting(State):
         if person.is_committer(self.language.team):
             if (self.person == person):
                 action_names = ['IC', 'TR', 'UNDO']
-            if not self.branch.is_vcs_readonly() and self.get_latest_po_file_action() is not None:
+            if self.able_to_commit():
                 action_names.insert(0, 'CI')
 
         return self._get_available_actions(person, action_names)


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