def get_renderer_context(self): renderer_context = super(ProductListView, self).get_renderer_context() if self.filter_class and renderer_context['request'].accepted_renderer.format == 'html': # restrict to products associated to this CMS page only backend = CMSPagesFilterBackend() queryset = backend.filter_queryset(self.request, self.get_queryset(), self) if callable(getattr(self.filter_class, 'get_render_context', None)): renderer_context['filter'] = self.filter_class.get_render_context(self.request, queryset) elif isinstance(getattr(self.filter_class, 'render_context', None), dict): renderer_context['filter'] = self.filter_class.render_context return renderer_context
class CMSPageProductListView(ProductListView): """ This view is used to list all products being associated with a CMS page. It normally is added to the urlpatterns as: ``url(r'^$', CMSPageProductListView.as_view(serializer_class=ProductSummarySerializer))`` where the ``ProductSummarySerializer`` is a customized REST serializer that that specific product model. """ renderer_classes = (CMSPageRenderer, JSONRenderer, BrowsableAPIRenderer) filter_backends = list(api_settings.DEFAULT_FILTER_BACKENDS) filter_backends.append(CMSPagesFilterBackend()) def filter_queryset(self, queryset): self.filter_context = None if self.filter_class: filter_instance = self.filter_class(self.request.query_params, queryset=queryset) if callable(getattr(filter_instance, 'get_render_context', None)): self.filter_context = filter_instance.get_render_context() elif hasattr(filter_instance, 'render_context'): self.filter_context = filter_instance.render_context qs = super(CMSPageProductListView, self).filter_queryset(queryset) return qs def get_renderer_context(self): renderer_context = super(CMSPageProductListView, self).get_renderer_context() if renderer_context['request'].accepted_renderer.format == 'html': renderer_context['filter'] = self.filter_context return renderer_context
def get_renderer_context(self): renderer_context = super(ProductListView, self).get_renderer_context() if self.filter_class and renderer_context[ 'request'].accepted_renderer.format == 'html': # restrict to products associated to this CMS page only backend = CMSPagesFilterBackend() queryset = backend.filter_queryset(self.request, self.get_queryset(), self) if callable(getattr(self.filter_class, 'get_render_context', None)): renderer_context[ 'filter'] = self.filter_class.get_render_context( self.request, queryset) elif isinstance(getattr(self.filter_class, 'render_context', None), dict): renderer_context['filter'] = self.filter_class.render_context return renderer_context
def get_renderer_context(self): renderer_context = super(AddFilterContextMixin, self).get_renderer_context() if self.filter_class and renderer_context['request'].accepted_renderer.format == 'html': # restrict filter set to products associated to this CMS page only queryset = self.product_model.objects.filter(self.limit_choices_to) queryset = CMSPagesFilterBackend().filter_queryset(self.request, queryset, self) renderer_context['filter'] = self.filter_class.get_render_context(self.request, queryset) return renderer_context
class CMSPageProductListView(ProductListView): """ This view is used to list all products being associated with a CMS page. It normally is added to the urlpatterns as: ``url(r'^$', CMSPageProductListView.as_view(serializer_class=ProductSummarySerializer))``. :param product_model: A specific product model. If unspecified, the default ``ProductModel`` is used. :param serializer_class: for instance ``ProductSummarySerializer``, a customized REST serializer for that specific product model. :param filter_class: TODO: :param cms_pages_fields: A tuple of field names used for looking up, which products belong to which CMS page. """ renderer_classes = (CMSPageRenderer, JSONRenderer, BrowsableAPIRenderer) filter_backends = [CMSPagesFilterBackend()] + list( api_settings.DEFAULT_FILTER_BACKENDS) cms_pages_fields = ('cms_pages', ) def get_renderer_context(self): renderer_context = super(ProductListView, self).get_renderer_context() if self.filter_class and renderer_context[ 'request'].accepted_renderer.format == 'html': # restrict to products associated to this CMS page only backend = CMSPagesFilterBackend() queryset = backend().filter_queryset(self.request, self.get_queryset(), self) if callable(getattr(self.filter_class, 'get_render_context', None)): renderer_context[ 'filter'] = self.filter_class.get_render_context( self.request, queryset) elif isinstance(getattr(self.filter_class, 'render_context', None), dict): renderer_context['filter'] = self.filter_class.render_context return renderer_context
# -*- coding: utf-8 -*- from __future__ import unicode_literals from django.conf.urls import patterns, url from rest_framework.settings import api_settings from shop.rest.filters import CMSPagesFilterBackend from shop.views.catalog import AddToCartView, ProductListView, ProductRetrieveView from shop.search.views import SearchView from myshop.serializers import (ProductSummarySerializer, ProductDetailSerializer, AddSmartCardToCartSerializer, AddSmartPhoneToCartSerializer, CatalogSearchSerializer) filter_backends = list(api_settings.DEFAULT_FILTER_BACKENDS) filter_backends.append(CMSPagesFilterBackend()) urlpatterns = patterns( '', url( r'^$', ProductListView.as_view( serializer_class=ProductSummarySerializer, filter_backends=filter_backends, )), url(r'^search-catalog$', SearchView.as_view(serializer_class=CatalogSearchSerializer, )), url( r'^(?P<slug>[\w-]+)$', ProductRetrieveView.as_view( serializer_class=ProductDetailSerializer, )),