diff mbox

[V2] Show the bulk edit form when the user can edit all patches in the list

Message ID 20110429131548.6376.6124.stgit@localhost6.localdomain6
State Superseded
Headers show

Commit Message

Guilherme Salgado April 29, 2011, 1:16 p.m. UTC
We used to show that form only when the user is a maintainer of the project to
which the patches in the list apply, but now the user just need to have edit
rights on all patches to see the form, which makes sense and as a bonus will
make it possible to do bulk changes on the bundle view.

Signed-off-by: Guilherme Salgado <guilherme.salgado@linaro.org>
---
 apps/patchwork/tests/updates.py  |   36 ++++++++++++++++++++++++++++++++++++
 apps/patchwork/views/__init__.py |    9 +++++----
 2 files changed, 41 insertions(+), 4 deletions(-)
diff mbox

Patch

diff --git a/apps/patchwork/tests/updates.py b/apps/patchwork/tests/updates.py
index ba1cb6d..7279370 100644
--- a/apps/patchwork/tests/updates.py
+++ b/apps/patchwork/tests/updates.py
@@ -20,6 +20,7 @@ 
 from django.test import TestCase
 from django.core.urlresolvers import reverse
 from patchwork.models import Patch, Person, State
+from patchwork.tests.factory import factory
 from patchwork.tests.utils import defaults, create_maintainer
 
 class MultipleUpdateTest(TestCase):
@@ -116,3 +117,38 @@  class MultipleUpdateTest(TestCase):
         response = self._testDelegateChange('')
         for p in self.patches:
             self.assertEquals(Patch.objects.get(pk = p.pk).delegate, None)
+
+    def testFormShownWhenUserHasEditRightsOnPatches(self):
+        project = factory.makeProject()
+        user_person = Person.objects.get(user = self.user)
+        patch1 = factory.makePatch(project = project, submitter = user_person)
+        patch2 = factory.makePatch(project = project, submitter = user_person)
+
+        # Our user has edit rights on both patches, but not on the project.
+        self.assertTrue(patch1.is_editable(self.user))
+        self.assertTrue(patch2.is_editable(self.user))
+        self.assertFalse(project.is_editable(self.user))
+
+        # Even though our user has no edit rights on the project, they can
+        # edit all patches in the list, so the form is shown.
+        url = reverse('patchwork.views.patch.list', args = [project.linkname])
+        response = self.client.get(url)
+        self.assertContains(response, self.properties_form_id,
+                            status_code = 200)
+
+    def testFormNotShownWhenUserHasNoEditRightsOnPatches(self):
+        project = factory.makeProject()
+        user_person = Person.objects.get(user = self.user)
+        patch1 = factory.makePatch(project = project, submitter = user_person)
+        patch2 = factory.makePatch(project = project)
+
+        # Here our user has edit rights on only one patch.
+        self.assertTrue(patch1.is_editable(self.user))
+        self.assertFalse(patch2.is_editable(self.user))
+        self.assertFalse(project.is_editable(self.user))
+
+        # Since the user can't edit one of the patches, the form is not shown.
+        url = reverse('patchwork.views.patch.list', args = [project.linkname])
+        response = self.client.get(url)
+        self.assertNotContains(response, self.properties_form_id,
+                               status_code = 200)
diff --git a/apps/patchwork/views/__init__.py b/apps/patchwork/views/__init__.py
index bae40c6..aaf9ae8 100644
--- a/apps/patchwork/views/__init__.py
+++ b/apps/patchwork/views/__init__.py
@@ -42,9 +42,7 @@  def generic_list(request, project, view,
     if request.method == 'POST':
         data = request.POST
     user = request.user
-    properties_form = None
-    if project.is_editable(user):
-        properties_form = MultiplePatchForm(project, data = data)
+    properties_form = MultiplePatchForm(project, data = data)
 
     if request.method == 'POST' and data.get('form') == 'patchlistform':
         action = data.get('action', '').lower()
@@ -59,7 +57,7 @@  def generic_list(request, project, view,
         if action in bundle_actions:
             errors = set_bundle(user, project, action, data, ps, context)
 
-        elif properties_form and action == properties_form.action:
+        elif action == properties_form.action:
             errors = process_multiplepatch_form(properties_form, user,
                                                 action, ps, context)
         else:
@@ -84,6 +82,9 @@  def generic_list(request, project, view,
         patches = patches.order_by(order.query())
 
     paginator = Paginator(request, patches)
+    for patch in paginator.current_page.object_list:
+        if not patch.is_editable(user):
+            properties_form = None
 
     context.update({
             'page':             paginator.current_page,