Repository: akvo/akvo-rsr

Issue:
Add links to be able to view more projects in the project directory
Currently, only 10 projects are shown in the project directory. Add an option to be able to view more projects. 


Discussion:

Resolving patch:
diff --git a/akvo/rsr/templatetags/addparam.py b/akvo/rsr/templatetags/addparam.py
--- a/akvo/rsr/templatetags/addparam.py
+++ b/akvo/rsr/templatetags/addparam.py
@@ -26,20 +26,11 @@ def render(self, context):
                 value = self.vars[i + 1]
             params[key] = value
 
-        # return '%s?%s' % (req.path, params.urlencode())
         if params:
-            # return ('?%s' % params.urlencode()).replace('&', '&amp;')
-
-            params = '?%s' % params.urlencode()
-            return params.replace('&', '&amp;')
-        else:
-            return ''
-        '''
-        if params:
-            return '?%s' % params.urlencode()
+            params = '&amp;'.join(sorted(params.urlencode().split('&')))
+            return '?%s' % params
         else:
             return ''
-        '''
 
 
 def addparam(parser, token):
diff --git a/akvo/rsr/views/project.py b/akvo/rsr/views/project.py
--- a/akvo/rsr/views/project.py
+++ b/akvo/rsr/views/project.py
@@ -94,9 +94,15 @@ def directory(request):
     sorted_projects = f.qs.distinct().order_by(sorting)
 
     # Build page
-    page = request.GET.get('page')
-    limit = request.GET.get('limit', settings.PROJECT_DIRECTORY_DEFAULT_SIZE)
-    page, paginator, page_range = pagination(page, sorted_projects, limit)
+    page_number = request.GET.get('page')
+    limit = request.GET.get('limit', settings.PROJECT_DIRECTORY_PAGE_SIZES[0])
+    limit = min(int(limit), settings.PROJECT_DIRECTORY_PAGE_SIZES[-1])
+    page, paginator, page_range = pagination(page_number, sorted_projects, limit)
+    start_index = page.start_index()
+    page_info = [
+        (start_index // size + 1, size)
+        for size in settings.PROJECT_DIRECTORY_PAGE_SIZES
+    ]
 
     # Get the current org filter for typeahead
     org_filter = request.GET.get('organisation', '')
@@ -114,6 +120,7 @@ def directory(request):
         'recipient_countries',
         'sectors',
         'budget_items',
+        'partnerships__organisation',
     ).select_related(
         'primary_organisation',
         'last_update'
@@ -137,6 +144,7 @@ def directory(request):
     context = {
         'project_count': sorted_projects.count(),
         'filter': f,
+        'page_info': page_info,
         'page': page,
         'page_range': page_range,
         'paginator': paginator,


Test patch:
diff --git a/akvo/rsr/tests/views/test_project.py b/akvo/rsr/tests/views/test_project.py
--- a/akvo/rsr/tests/views/test_project.py
+++ b/akvo/rsr/tests/views/test_project.py
@@ -12,7 +12,10 @@
 from unittest import skip
 
 import django_perf_rec
-from django.test import TestCase
+from django.conf import settings
+from django.test import Client, TestCase
+
+from akvo.rsr.models import Organisation
 
 
 @skip('Needs Django >= 1.8')
@@ -25,3 +28,59 @@ def setUp(self):
     def test_project_directory_listing(self):
         with django_perf_rec.record():
             self.client.get('/en/projects/', follow=True)
+
+
+class ProjectViewsTestCase(TestCase):
+    """Test the project views."""
+
+    def setUp(self):
+        super(ProjectViewsTestCase, self).setUp()
+        self.c = Client(HTTP_HOST=settings.RSR_DOMAIN)
+
+    def test_should_add_page_size_limit_links(self):
+        # Given
+        url = '/en/projects/'
+        page_limit_1 = 'href="?limit=20&amp;page=1"'
+        page_limit_2 = 'href="?limit=50&amp;page=1"'
+        page_limit_3 = 'href="?limit=100&amp;page=1"'
+
+        # When
+        response = self.c.get(url, follow=True)
+
+        # Then
+        self.assertIn(page_limit_1, response.content)
+        self.assertIn(page_limit_2, response.content)
+        self.assertIn(page_limit_3, response.content)
+
+    def test_should_add_page_size_limit_parameter(self):
+        # Given
+        org = Organisation.objects.create()
+        url = '/en/projects/'
+        data = {'organisation': org.id}
+        page_limit_1 = 'href="?limit=20&amp;organisation={}&amp;page=1"'.format(org.id)
+        page_limit_2 = 'href="?limit=50&amp;organisation={}&amp;page=1"'.format(org.id)
+        page_limit_3 = 'href="?limit=100&amp;organisation={}&amp;page=1"'.format(org.id)
+
+        # When
+        response = self.c.get(url, data=data, follow=True)
+
+        # Then
+        self.assertIn(page_limit_1, response.content)
+        self.assertIn(page_limit_2, response.content)
+        self.assertIn(page_limit_3, response.content)
+
+    def test_should_replace_page_size_limit_parameter(self):
+        # Given
+        url = '/en/projects/'
+        data = {'limit': 10}
+        page_limit_1 = 'href="?limit=20&amp;page=1"'
+        page_limit_2 = 'href="?limit=50&amp;page=1"'
+        page_limit_3 = 'href="?limit=100&amp;page=1"'
+
+        # When
+        response = self.c.get(url, data=data, follow=True)
+
+        # Then
+        self.assertIn(page_limit_1, response.content)
+        self.assertIn(page_limit_2, response.content)
+        self.assertIn(page_limit_3, response.content)