Repository: zestedesavoir/zds-site

Issue:
Unifier les adresses de création de contenu
On a les trois chemins `contenus/nouveau-tutoriel`, `contenus/nouvel-article` et `contenus/nouveau-billet`. Pourquoi ne pas juste avoir `contenus/nouveau-contenu` avec le type de contenu en paramètre dans l'URL (et qui vaut `TUTORIAL` par défaut) vu que c'est la même vue (`CreateContent`) qui est appelée dans tous les cas avec l'argument `created_content_type` qui vaut `TUTORIAL`, `ARTICLE` ou `OPINION`.


Discussion:

Resolving patch:
diff --git a/zds/tutorialv2/urls/urls_contents.py b/zds/tutorialv2/urls/urls_contents.py
--- a/zds/tutorialv2/urls/urls_contents.py
+++ b/zds/tutorialv2/urls/urls_contents.py
@@ -100,9 +100,7 @@
     # typo:
     re_path(r"^reactions/typo/$", WarnTypo.as_view(), name="warn-typo"),
     # create:
-    re_path(r"^nouveau-tutoriel/$", CreateContent.as_view(created_content_type="TUTORIAL"), name="create-tutorial"),
-    re_path(r"^nouvel-article/$", CreateContent.as_view(created_content_type="ARTICLE"), name="create-article"),
-    re_path(r"^nouveau-billet/$", CreateContent.as_view(created_content_type="OPINION"), name="create-opinion"),
+    path("nouveau-contenu/<str:created_content_type>/", CreateContent.as_view(), name="create-content"),
     re_path(
         r"^nouveau-conteneur/(?P<pk>\d+)/(?P<slug>.+)/(?P<container_slug>.+)/$",
         CreateContainer.as_view(),
diff --git a/zds/tutorialv2/views/contents.py b/zds/tutorialv2/views/contents.py
--- a/zds/tutorialv2/views/contents.py
+++ b/zds/tutorialv2/views/contents.py
@@ -45,6 +45,7 @@
 from zds.utils.models import get_hat_from_settings
 from zds.mp.utils import send_mp, send_message_mp
 from zds.utils.uuslug_wrapper import slugify
+from zds.tutorialv2.models import CONTENT_TYPE_LIST
 
 logger = logging.getLogger(__name__)
 
@@ -73,6 +74,9 @@ class CreateContent(LoggedWithReadWriteHability, FormWithPreview):
 
     def get_form(self, form_class=ContentForm):
         form = super().get_form(form_class)
+        content_type = self.kwargs["created_content_type"]
+        if content_type in CONTENT_TYPE_LIST:
+            self.created_content_type = content_type
         form.initial["type"] = self.created_content_type
         return form
 


Test patch:
diff --git a/zds/tutorialv2/tests/tests_front.py b/zds/tutorialv2/tests/tests_front.py
--- a/zds/tutorialv2/tests/tests_front.py
+++ b/zds/tutorialv2/tests/tests_front.py
@@ -154,7 +154,9 @@ def test_the_editor_forgets_its_content_on_form_submission(self):
 
         self.login(author)
         selenium.execute_script('localStorage.setItem("editor_choice", "new")')  # we want the new editor
-        new_article_url = self.live_server_url + reverse("content:create-article")
+        new_article_url = self.live_server_url + reverse(
+            "content:create-content", kwargs={"created_content_type": "ARTICLE"}
+        )
         selenium.get(new_article_url)
         WebDriverWait(self.selenium, 10).until(
             ec.element_to_be_clickable((By.CSS_SELECTOR, "input[type=checkbox][name=subcategory]"))
diff --git a/zds/tutorialv2/tests/tests_opinion_views.py b/zds/tutorialv2/tests/tests_opinion_views.py
--- a/zds/tutorialv2/tests/tests_opinion_views.py
+++ b/zds/tutorialv2/tests/tests_opinion_views.py
@@ -170,7 +170,7 @@ def test_no_help_for_tribune(self):
 
     def test_help_for_article(self):
         self.client.force_login(self.user_author)
-        resp = self.client.get(reverse("content:create-article"))
+        resp = self.client.get(reverse("content:create-content", kwargs={"created_content_type": "ARTICLE"}))
         self.assertEqual(200, resp.status_code)
 
     def test_opinion_publication_staff(self):
diff --git a/zds/tutorialv2/tests/tests_views/tests_content.py b/zds/tutorialv2/tests/tests_views/tests_content.py
--- a/zds/tutorialv2/tests/tests_views/tests_content.py
+++ b/zds/tutorialv2/tests/tests_views/tests_content.py
@@ -245,7 +245,7 @@ def test_basic_tutorial_workflow(self):
         random_with_md = "un text contenant du **markdown** ."
 
         response = self.client.post(
-            reverse("content:create-tutorial"),
+            reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}),
             {
                 "text": random_with_md,
                 "preview": "",
@@ -259,7 +259,7 @@ def test_basic_tutorial_workflow(self):
         self.assertIn("<strong>markdown</strong>", result_string, "We need the text to be properly formatted")
 
         result = self.client.post(
-            reverse("content:create-tutorial"),
+            reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}),
             {
                 "title": title,
                 "description": description,
@@ -1272,7 +1272,7 @@ def test_export_content(self):
 
         # create a tutorial
         result = self.client.post(
-            reverse("content:create-tutorial"),
+            reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}),
             {
                 "title": given_title,
                 "description": some_text,
@@ -1432,7 +1432,7 @@ def test_import_create_content(self):
 
         # create a tutorial
         result = self.client.post(
-            reverse("content:create-tutorial"),
+            reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}),
             {
                 "title": given_title,
                 "description": some_text,
@@ -1548,7 +1548,7 @@ def test_import_in_existing_content(self):
 
         # create a tutorial
         result = self.client.post(
-            reverse("content:create-tutorial"),
+            reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}),
             {
                 "title": given_title,
                 "description": some_text,
@@ -3557,7 +3557,9 @@ def test_no_invalid_titles(self):
 
         for title in disallowed_titles:
             dic["title"] = title
-            result = self.client.post(reverse("content:create-tutorial"), dic, follow=False)
+            result = self.client.post(
+                reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}), dic, follow=False
+            )
             self.assertEqual(result.status_code, 200)
             self.assertEqual(PublishableContent.objects.all().count(), 1)
             self.assertFalse(result.context["form"].is_valid())
@@ -3571,7 +3573,9 @@ def test_no_invalid_titles(self):
 
         for title in allowed_titles:
             dic["title"] = title
-            result = self.client.post(reverse("content:create-tutorial"), dic, follow=False)
+            result = self.client.post(
+                reverse("content:create-content", kwargs={"created_content_type": "TUTORIAL"}), dic, follow=False
+            )
             self.assertEqual(result.status_code, 302)
             self.assertNotEqual(PublishableContent.objects.all().count(), prev_count)
             prev_count += 1
diff --git a/zds/tutorialv2/tests/tests_views/tests_edgecases.py b/zds/tutorialv2/tests/tests_views/tests_edgecases.py
--- a/zds/tutorialv2/tests/tests_views/tests_edgecases.py
+++ b/zds/tutorialv2/tests/tests_views/tests_edgecases.py
@@ -73,3 +73,11 @@ def test_no_bad_slug_renaming_on_rename(self):
         self.assertEqual(200, result.status_code)
         result = self.client.get(article.get_absolute_url())
         self.assertEqual(200, result.status_code)
+
+    def test_default_content_type_is_tutorial(self):
+        """
+        Test that if a wrong content type is provided to the create-content view, TUTORIAL is used as a default
+        """
+        self.client.force_login(self.author.user)
+        result = self.client.get(reverse("content:create-content", kwargs={"created_content_type": "WRONG_TYPE"}))
+        self.assertEqual("TUTORIAL", result.context_data.get("form").initial.get("type"))