def test_decorate_class(self): class Cls(object): val = False def __init__(self): self.val = True Deprecated = deprecated(Cls) # noqa with warnings.catch_warnings(record=True) as caught: warnings.simplefilter("always") obj = Deprecated() self.assertTrue(obj.val) assert len(caught) == 1 assert issubclass(caught[0].category, DeprecationWarning)
self.shop = shop self.categories = categories super(SolrProductSearchHandler, self).__init__(request_data, full_path) def get_search_queryset(self): sqs = super(SolrProductSearchHandler, self).get_search_queryset() if self.categories: pattern = ' OR '.join( ['"%s"' % c.full_name for c in self.categories]) sqs = sqs.narrow('category_exact:(%s)' % pattern) if self.shop: sqs = sqs.narrow('product_class__shop_id:(%s)' % self.shop.id) return sqs ProductSearchHandler = deprecated(SolrProductSearchHandler) class ESProductSearchHandler(CoreESProductSearchHandler): def __init__(self, request_data, full_path, categories=None, shop=None): self.shop = shop self.categories = categories super(ESProductSearchHandler, self).__init__(request_data, full_path) def get_search_queryset(self): sqs = super(ESProductSearchHandler, self).get_search_queryset() if self.categories: for category in self.categories: sqs = sqs.filter_or(category=category.full_name) if self.shop: sqs = sqs.filter_or(product_class__shop_id=self.shop.id)
class Structured(Base): """ A strategy class which provides separate, overridable methods for determining the 3 things that a ``PurchaseInfo`` instance requires: #) A stockrecord #) A pricing policy #) An availability policy """ def fetch_for_product(self, product, stockrecord=None): """ Return the appropriate ``PurchaseInfo`` instance. This method is not intended to be overridden. """ if stockrecord is None: stockrecord = self.select_stockrecord(product) return PurchaseInfo(price=self.pricing_policy(product, stockrecord), availability=self.availability_policy( product, stockrecord), stockrecord=stockrecord) def fetch_for_parent(self, product): # Select children and associated stockrecords children_stock = self.select_children_stockrecords(product) return PurchaseInfo(price=self.parent_pricing_policy( product, children_stock), availability=self.parent_availability_policy( product, children_stock), stockrecord=None) fetch_for_group = deprecated(fetch_for_parent) def select_stockrecord(self, product): """ Select the appropriate stockrecord """ raise NotImplementedError("A structured strategy class must define a " "'select_stockrecord' method") def select_children_stockrecords(self, product): """ Select appropriate stock record for all children of a product """ records = [] for child in product.children.all(): # Use tuples of (child product, stockrecord) records.append((child, self.select_stockrecord(child))) return records select_variant_stockrecords = deprecated(select_children_stockrecords) def pricing_policy(self, product, stockrecord): """ Return the appropriate pricing policy """ raise NotImplementedError("A structured strategy class must define a " "'pricing_policy' method") def parent_pricing_policy(self, product, children_stock): raise NotImplementedError("A structured strategy class must define a " "'parent_pricing_policy' method") group_pricing_policy = deprecated(parent_pricing_policy) def availability_policy(self, product, stockrecord): """ Return the appropriate availability policy """ raise NotImplementedError("A structured strategy class must define a " "'availability_policy' method") def parent_availability_policy(self, product, children_stock): raise NotImplementedError("A structured strategy class must define a " "'parent_availability_policy' method") group_availability_policy = deprecated(parent_availability_policy)
self.categories = categories super(SolrProductSearchHandler, self).__init__(request_data, full_path) def get_search_queryset(self): sqs = super(SolrProductSearchHandler, self).get_search_queryset() if self.categories: # We use 'narrow' API to ensure Solr's 'fq' filtering is used as # opposed to filtering using 'q'. pattern = ' OR '.join([ '"%s"' % c.full_name for c in self.categories]) sqs = sqs.narrow('category_exact:(%s)' % pattern) return sqs # Deprecated name. TODO: Remove in Oscar 1.2 ProductSearchHandler = deprecated(SolrProductSearchHandler) class ESProductSearchHandler(SearchHandler): """ Search handler specialised for searching products. Comes with optional category filtering. To be used with an ElasticSearch search backend. """ form_class = BrowseCategoryForm model_whitelist = [Product] paginate_by = settings.OSCAR_PRODUCTS_PER_PAGE def __init__(self, request_data, full_path, categories=None): self.categories = categories super(ESProductSearchHandler, self).__init__(request_data, full_path)