[PATCH v2] views: Update how patch counts are retrieved

Stephen Finucane stephen at that.guru
Sat Oct 22 22:23:27 AEDT 2016


It's no longer possible to access 'Project.patch_set' as
'Project.submission_set' has replaced it. This was missed when the
cover letter feature was merged, so resolve it now.

Signed-off-by: Stephen Finucane <stephen at that.guru>
Closes-bug: #54
---
v2:
- Add tests to prevent recurrence of the issue
---
 patchwork/tests/test_projects.py | 87 ++++++++++++++++++++++++++++++++++++++++
 patchwork/views/project.py       |  6 ++-
 2 files changed, 91 insertions(+), 2 deletions(-)
 create mode 100644 patchwork/tests/test_projects.py

diff --git a/patchwork/tests/test_projects.py b/patchwork/tests/test_projects.py
new file mode 100644
index 0000000..75d579a
--- /dev/null
+++ b/patchwork/tests/test_projects.py
@@ -0,0 +1,87 @@
+# Patchwork - automated patch tracking system
+# Copyright (C) 2016 Stephen Finucane <stephen at that.guru>
+#
+# This file is part of the Patchwork package.
+#
+# Patchwork is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# Patchwork is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Patchwork; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+
+from patchwork.tests import utils
+
+
+class ProjectViewTest(TestCase):
+
+    def test_redirect(self):
+        project = utils.create_project()
+
+        requested_url = reverse('project-list')
+        redirect_url = reverse('patch-list', kwargs={
+            'project_id': project.linkname})
+
+        response = self.client.get(requested_url)
+        self.assertRedirects(response, redirect_url)
+
+    def test_no_redirect(self):
+        project_a = utils.create_project()
+        project_b = utils.create_project()
+
+        requested_url = reverse('project-list')
+
+        response = self.client.get(requested_url)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.context['projects']), 2)
+
+    def test_n_patches(self):
+        project = utils.create_project()
+
+        requested_url = reverse('project-detail', kwargs={
+            'project_id': project.linkname})
+
+        response = self.client.get(requested_url)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.context['n_patches'], 0)
+        self.assertEqual(response.context['n_archived_patches'], 0)
+
+        patch_a = utils.create_patch(project=project)
+
+        response = self.client.get(requested_url)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.context['n_patches'], 1)
+        self.assertEqual(response.context['n_archived_patches'], 0)
+
+        patch_b = utils.create_patch(project=project, archived=True)
+
+        response = self.client.get(requested_url)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(response.context['n_patches'], 1)
+        self.assertEqual(response.context['n_archived_patches'], 1)
+
+    def test_maintainers(self):
+        project = utils.create_project()
+
+        requested_url = reverse('project-detail', kwargs={
+            'project_id': project.linkname})
+
+        response = self.client.get(requested_url)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.context['maintainers']), 0)
+
+        maintainer = utils.create_maintainer(project=project)
+
+        response = self.client.get(requested_url)
+        self.assertEqual(response.status_code, 200)
+        self.assertEqual(len(response.context['maintainers']), 1)
diff --git a/patchwork/views/project.py b/patchwork/views/project.py
index 461618b..4c59c10 100644
--- a/patchwork/views/project.py
+++ b/patchwork/views/project.py
@@ -25,6 +25,7 @@ from django.core import urlresolvers
 from django.http import HttpResponseRedirect
 from django.shortcuts import get_object_or_404, render
 
+from patchwork.models import Patch
 from patchwork.models import Project
 
 
@@ -44,13 +45,14 @@ def projects(request):
 
 def project(request, project_id):
     project = get_object_or_404(Project, linkname=project_id)
+    patches = Patch.objects.filter(project=project)
 
     context = {
         'project': project,
         'maintainers': User.objects.filter(
             profile__maintainer_projects=project),
-        'n_patches': project.patch_set.filter(archived=False).count(),
-        'n_archived_patches': project.patch_set.filter(archived=True).count(),
+        'n_patches': patches.filter(archived=False).count(),
+        'n_archived_patches': patches.filter(archived=True).count(),
         'enable_xmlrpc': settings.ENABLE_XMLRPC,
     }
     return render(request, 'patchwork/project.html', context)
-- 
2.7.4



More information about the Patchwork mailing list