Skip to content

Commit

Permalink
awesto#859 Disabled auto pagination when has offset
Browse files Browse the repository at this point in the history
  • Loading branch information
execut committed Jun 22, 2021
1 parent 13d9a77 commit 8cb5cba
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
11 changes: 10 additions & 1 deletion shop/cascade/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@ def get_render_template(self, context, instance, placeholder):
return select_template(templates)

def render(self, context, instance, placeholder):
context['pagination'] = instance.glossary.get('pagination', 'paginator')
try:
paginator = context['paginator']
except KeyError:
paginator = False
if paginator and paginator.get_offset(paginator.request) > 0:
pagination = 'paginator'
else:
pagination = instance.glossary.get('pagination', 'paginator')

context['pagination'] = pagination
return context

@classmethod
Expand Down
42 changes: 42 additions & 0 deletions tests/test_cascade_plugins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from django.test import TestCase
from cms.api import add_plugin
from cms.models import Placeholder
from shop.cascade.catalog import ShopCatalogPlugin
from rest_framework.pagination import LimitOffsetPagination
from unittest.mock import MagicMock


def get_plugin_model_instance():
placeholder = Placeholder.objects.create(slot='test')
model_instance = add_plugin(
placeholder,
ShopCatalogPlugin,
'en',
)
return model_instance


class ShopCatalogPluginTests(TestCase):
def test_plugin_context_with_auto_pagination(self):
model_instance = get_plugin_model_instance()
plugin_instance = model_instance.get_plugin_class_instance()
model_instance.glossary['pagination'] = 'auto'
context = plugin_instance.render({}, model_instance, None)
self.assertIn('pagination', context)
self.assertEqual(context['pagination'], 'auto')

def test_plugin_context_will_return_pagination_with_offset_when_pagination_is_auto(self):
model_instance = get_plugin_model_instance()
plugin_instance = model_instance.get_plugin_class_instance()
model_instance.glossary['pagination'] = 'auto'
pagination = LimitOffsetPagination()
pagination.request = 123
pagination.get_offset = MagicMock(return_value=1)

context = plugin_instance.render({
'paginator': pagination
}, model_instance, None)

pagination.get_offset.assert_called_with(123)
self.assertIn('pagination', context)
self.assertEqual(context['pagination'], 'paginator')

0 comments on commit 8cb5cba

Please sign in to comment.