Example #1
0
 def filter_placeholder_test(self):
     f = Feed.objects.get(pk=9)  # has 2 in stock tiles
     tiles = list(f.tiles.all())  # force load
     p = Product.objects.get(pk=3)  # in stock product
     p.placeholder = True
     p.save()
     f.add(p)
     result = algorithms.ir_filter(
         feed=f)  # filter out out of stock product
     self.assertEqual(set(tiles), set(result))
Example #2
0
 def filter_out_of_stock_test(self):
     f = Feed.objects.get(pk=9)  # has 2 in stock tiles
     tiles = list(f.tiles.all())  # force load
     p = Product.objects.get(pk=12)  # out of stock product
     i = ProductImage.objects.get(pk=4)
     p.product_images.add(i)
     f.add(p)
     result = algorithms.ir_filter(
         feed=f)  # filter out out of stock product
     self.assertEqual(set(tiles), set(result))
Example #3
0
 def filter_tiles_from_feed_test(self):
     f = Feed.objects.get(pk=9)  # has 2 in stock tiles
     tiles = list(f.tiles.all())  # force load
     p = Product.objects.get(pk=3)  # in stock product
     i = ProductImage.objects.get(pk=4)
     p.product_images.add(i)
     f.add(p)
     # only tiles will be used b/c they are specifically specified
     result = algorithms.ir_filter(tiles=tiles, feed=f)
     self.assertEqual(set(tiles), set(result))
Example #4
0
    def filter_out_of_stock_store_display_override_test(self):
        store = Store.objects.get(pk=1)
        store.display_out_of_stock = True  # includes p in ir_filter
        store.save()

        f = Feed.objects.get(pk=9)  # has 2 in stock tiles
        tiles = list(f.tiles.all())  # force load
        p = Product.objects.get(pk=12)  # out of stock product
        i = ProductImage.objects.get(pk=4)
        p.product_images.add(i)
        new_tile, _ = f.add(p)
        tiles.append(new_tile)  # add out of stock tile

        result = algorithms.ir_filter(
            feed=f)  # does not filter out of stock b/c store
        self.assertEqual(set(tiles), set(result))
Example #5
0
 def filter_no_feed_no_tiles_test(self):
     with self.assertRaises(ValueError):
         algorithms.ir_filter()
Example #6
0
 def filter_feed_test(self):
     f = Feed.objects.get(pk=9)  # has 2 in stock tiles
     tiles = list(f.tiles.all())  # force load
     result = algorithms.ir_filter(feed=f)
     self.assertEqual(set(tiles), set(result))
Example #7
0
    def get_results(self,
                    results=settings.INTENTRANK_DEFAULT_NUM_RESULTS,
                    algorithm=None,
                    tile_id=0,
                    offset=0,
                    **kwargs):
        """Converts a feed into a list of <any> using given parameters.

        :param results      number of <any> to return
        :param exclude_set  IDs of items in the feed to never consider (ex: seen tiles)
        :param request      (relay)
        :param algorithm    reference to a <Feed> => [<Tile>] function
        :param tile_id      for getting related tiles (unused!)

        :returns            a list of <any>
        """
        request = kwargs.get('request', None)
        category_name = kwargs.get('category_name', None)
        feed = self._feed
        store = self._store
        exclude_set = kwargs.get('exclude_set', [])
        products_only = kwargs.get('products_only', False)
        content_only = kwargs.get('content_only', False)
        tiles = None

        if not algorithm:
            algorithm = self.algorithm

        if not feed.tiles.count():  # short circuit: return empty resultset
            return Tile.objects.none()

        # categories filter a feed
        category_names = []
        tiles = []

        if category_name:
            category_names = category_name.split('|')

            # Get first category
            try:
                base_category = Category.objects.get(store=store,
                                                     name=category_names[0])
            except Category.DoesNotExist:
                # This text is hardcoded by API consumers, so be careful about changing it
                raise Category.DoesNotExist(
                    "Category '{0}' does not exist for Store '{1}'".format(
                        category_names[0], store.name))
            else:
                tiles = feed.tiles.filter(categories__id=base_category.id)

                # Filter tiles with additional categories if they exist
                for name in category_names[1:]:
                    try:
                        filter_category = Category.objects.get(store=store,
                                                               name=name)
                    except Category.DoesNotExist:
                        # This text is hardcoded by API consumers, so be careful about changing it
                        raise Category.DoesNotExist(
                            "Category '{0}' does not exist for Store '{1}'".
                            format(name, store.name))
                    tiles = tiles.filter(categories__id=filter_category.id)
        else:
            # no category found - default to all tiles
            tiles = feed.tiles.all()

        if not tiles:
            return Tile.objects.none()
        else:
            # Apply filter algorithm
            tiles = ir_filter(feed=feed,
                              tiles=tiles,
                              exclude_set=exclude_set,
                              products_only=products_only,
                              content_only=content_only)

            return algorithm(tiles=tiles,
                             num_results=results,
                             exclude_set=exclude_set,
                             offset=offset,
                             tile_id=tile_id,
                             finite=self._page.is_finite,
                             products_only=products_only,
                             content_only=content_only)