diff --git a/shop/cascade/catalog.py b/shop/cascade/catalog.py index b1030d213..2395ede91 100644 --- a/shop/cascade/catalog.py +++ b/shop/cascade/catalog.py @@ -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 diff --git a/shop/static/shop/js/catalog.js b/shop/static/shop/js/catalog.js index 1284fb876..2b87a15a0 100644 --- a/shop/static/shop/js/catalog.js +++ b/shop/static/shop/js/catalog.js @@ -124,6 +124,8 @@ djangoShopModule.controller('CatalogListController', ['$log', '$scope', '$http', this.loadProducts = function(config) { if ($scope.isLoading || fetchURL === null) return; + + config = this.deleteExistedSearchParams(config); $scope.isLoading = true; $http.get(fetchURL, config).then(function(response) { fetchURL = response.data.next; @@ -136,6 +138,19 @@ djangoShopModule.controller('CatalogListController', ['$log', '$scope', '$http', }); }; + this.deleteExistedSearchParams = function (config) { + if (typeof URLSearchParams !== 'undefined') { + let searchParams = new URLSearchParams(fetchURL.substring(fetchURL.indexOf('?'))); + for (let key in config.params) { + if (searchParams.has(key)) { + delete config.params[key]; + } + } + } + + return config; + }; + this.resetProductsList = function() { fetchURL = djangoShop.getLocationPath(); $scope.catalog.products = []; diff --git a/tests/test_cascade_plugins.py b/tests/test_cascade_plugins.py new file mode 100644 index 000000000..f2b3bd5e4 --- /dev/null +++ b/tests/test_cascade_plugins.py @@ -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')