Repository: mozmeao/snippets-service

Issue:
Set snippet to `Draft` when `Save as New`



Discussion:
Attaching 2018Q4 label and removing from milestone since that's not blocking Fx63

Resolving patch:
diff --git a/snippets/base/admin/adminmodels.py b/snippets/base/admin/adminmodels.py
--- a/snippets/base/admin/adminmodels.py
+++ b/snippets/base/admin/adminmodels.py
@@ -202,6 +202,14 @@ def preview_url(self, obj):
         '''
         return mark_safe(text)
 
+    def change_view(self, request, *args, **kwargs):
+        if request.method == 'POST' and '_saveasnew' in request.POST:
+            # Always saved cloned snippets as un-published and un-check ready for review.
+            post_data = request.POST.copy()
+            post_data['status'] = models.STATUS_CHOICES['Draft']
+            request.POST = post_data
+        return super().change_view(request, *args, **kwargs)
+
 
 class CampaignAdmin(admin.ModelAdmin):
     readonly_fields = ('created', 'modified', 'creator',)


Test patch:
diff --git a/snippets/base/tests/test_admin.py b/snippets/base/tests/test_admin.py
--- a/snippets/base/tests/test_admin.py
+++ b/snippets/base/tests/test_admin.py
@@ -3,8 +3,9 @@
 
 from mock import Mock, patch
 
-from snippets.base.admin import SnippetAdmin, SnippetTemplateAdmin
-from snippets.base.models import Snippet, SnippetTemplate, SnippetTemplateVariable
+from snippets.base.admin import ASRSnippetAdmin, SnippetAdmin, SnippetTemplateAdmin
+from snippets.base.models import (STATUS_CHOICES, ASRSnippet, Snippet, SnippetTemplate,
+                                  SnippetTemplateVariable)
 from snippets.base.tests import SnippetTemplateFactory, SnippetTemplateVariableFactory, TestCase
 
 
@@ -138,3 +139,41 @@ def test_save_related_reserved_name(self):
 
         self.assertFalse(SnippetTemplateVariable.objects
                          .filter(template=template, name='reserved_name').exists())
+
+
+class ASRSnippetAdminTests(TestCase):
+    def setUp(self):
+        self.factory = RequestFactory()
+        self.model_admin = ASRSnippetAdmin(ASRSnippet, None)
+        self.model_admin.admin_site = Mock()
+        self.user = User.objects.get_or_create(username='foo', email='foo@example.com')[0]
+
+    def test_save_as_published(self):
+        request = self.factory.post('/', data={
+            'name': 'test',
+            'template': 'foo',
+            'status': STATUS_CHOICES['Published'],
+            '_saveasnew': True
+        })
+        request.user = self.user
+
+        with patch('snippets.base.admin.admin.ModelAdmin.change_view') as change_view_mock:
+            self.model_admin.change_view(request, 999)
+            change_view_mock.assert_called_with(request, 999)
+            request = change_view_mock.call_args[0][0]
+            self.assertEqual(request.POST['status'], STATUS_CHOICES['Draft'])
+
+    def test_normal_save_published(self):
+        """Test that normal save doesn't alter `status` attribute."""
+        request = self.factory.post('/', data={
+            'name': 'test',
+            'template': 'foo',
+            'status': STATUS_CHOICES['Published'],
+        })
+        request.user = self.user
+
+        with patch('snippets.base.admin.admin.ModelAdmin.change_view') as change_view_mock:
+            self.model_admin.change_view(request, 999)
+            change_view_mock.assert_called_with(request, 999)
+            request = change_view_mock.call_args[0][0]
+            self.assertEqual(request.POST['status'], str(STATUS_CHOICES['Published']))