Repository: translate/pootle

Issue:
Give better feedback when trying to initialize a tp which already has directories
If you try to initialize a TP from templates where there are directories already present for that project/lang then it fails - which is correct - but we need to improve feedback


Discussion:

Resolving patch:
diff --git a/pootle/apps/pootle_project/forms.py b/pootle/apps/pootle_project/forms.py
--- a/pootle/apps/pootle_project/forms.py
+++ b/pootle/apps/pootle_project/forms.py
@@ -6,6 +6,8 @@
 # or later license. See the LICENSE file for a copy of the license and the
 # AUTHORS file for copyright and authorship information.
 
+import os
+
 from django import forms
 from django.db import connection
 from django.forms.models import BaseModelFormSet
@@ -84,6 +86,22 @@ def __init__(self, *args, **kwargs):
         self.fields["project"].queryset = self.fields[
             "project"].queryset.filter(pk=project_id)
 
+    def clean(self):
+        if not self.cleaned_data.get("id"):
+            exists_on_disk = (
+                self.cleaned_data["language"].code
+                in os.listdir(self.cleaned_data["project"].get_real_path()))
+            if exists_on_disk:
+                errordict = dict(
+                    lang=self.cleaned_data["language"].code,
+                    path=os.path.join(
+                        self.cleaned_data["project"].get_real_path(),
+                        self.cleaned_data["language"].code))
+                raise forms.ValidationError(
+                    _("Cannot create translation project for language "
+                      "'%(lang)s', path '%(path)s' already exists",
+                      errordict))
+
     def save(self, response_url, commit=True):
         tp = self.instance
         initialize_from_templates = False
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
@@ -303,8 +303,12 @@ def process_formset(self):
                     messages.INFO,
                     _("Translation project (%s) has been deleted" % tp))
         else:
-            messages.add_message(self.request, messages.ERROR,
-                                 self.msg_form_error)
+            for form in formset:
+                for error in form.errors.values():
+                    messages.add_message(
+                        self.request,
+                        messages.ERROR,
+                        error)
 
     def render_formset(self, formset):
 


Test patch:
diff --git a/tests/views/admin.py b/tests/views/admin.py
--- a/tests/views/admin.py
+++ b/tests/views/admin.py
@@ -6,6 +6,8 @@
 # or later license. See the LICENSE file for a copy of the license and the
 # AUTHORS file for copyright and authorship information.
 
+import os
+
 import pytest
 
 from django.core.exceptions import ImproperlyConfigured, PermissionDenied
@@ -309,3 +311,32 @@ def test_admin_view_projects(client, request_users, english):
                 'source_language': english.id}}}
     for k, v in expected.items():
         assert response.context_data[k] == v
+
+
+@pytest.mark.django_db
+def test_admin_view_project_add_tp_existing_dir(project0,
+                                                english, client, admin):
+    user = admin
+    project0.treestyle = "nongnu"
+    project0.save()
+    new_language = LanguageDBFactory()
+    client.login(
+        username=user.username,
+        password=TEST_USERS["admin"]["password"])
+    get_response = _admin_view_get(client, project0)
+    post_data = {}
+    formset = get_response.context["formset"]
+    forms = formset.forms + formset.extra_forms + [formset.management_form]
+    os.makedirs(os.path.join(project0.get_real_path(), new_language.code))
+    for form in forms:
+        for field in form.fields:
+            post_data["%s-%s" % (form.prefix, field)] = (
+                form.fields[field].initial
+                or form.initial.get(field, ""))
+    post_data["%s-language" % formset.extra_forms[0].prefix] = new_language.id
+    post_data["%s-project" % formset.extra_forms[0].prefix] = project0.id
+    _admin_view_post(client, project0, **post_data)
+    with pytest.raises(TranslationProject.DoesNotExist):
+        TranslationProject.objects.get(
+            language=new_language,
+            project=project0)