Beispiel #1
0
    def suggest_products_for(self, products, max_results=3):
        product_ids = [p.id for p in products]
        if len(products) == 1:
            suggestions = r.zrange(self.get_product_key(product_ids[0]),
                                   0,
                                   -1,
                                   desc=True)[:max_results]

        else:
            flat_ids = ''.join([str(id) for id in product_ids])
            tmp_key = 'tmp_{}'.format(flat_ids)
            keys = [self.get_product_key(id) for id in product_ids]
            r.zunionstore(tmp_key, keys)
            r.zrem(tmp_key, *product_ids)
            suggestions = r.zrange(tmp_key, 0, -1, desc=True)[:max_results]
            r.delete(tmp_key)

        suggested_products_ids = [int(id) for id in suggestions]

        suggested_products = list(
            Product.query.filter(Product.id.in_(suggested_products_ids)))
        suggested_products.sort(
            key=lambda x: suggested_products_ids.index(x.id))

        return suggested_products
Beispiel #2
0
    def delete(self):
        """Delete the entity from the database"""
        # Delete the indexes
        for key in self.__indexes__:
            oldvalue = self.__getitem__(key)
            # If the key is an index, delete the index
            if oldvalue:
                del self.__indexes__[key][oldvalue]

        return redis.delete(self.id)
Beispiel #3
0
    def delete(self):
        """Delete the entity from the database"""
        # Delete the indexes
        for key in self.__indexes__:
            oldvalue = self.__getitem__(key)
            # If the key is an index, delete the index
            if oldvalue:
                del self.__indexes__[key][oldvalue]

        return redis.delete(self.id)
Beispiel #4
0
    def deleteall(cls):
        if not hasattr(cls, '__prefix__'):
            raise AttributeError("Models must define the attribute: __prefix__ or define one of the model keys as primary")

        keys = redis.keys(cls.__prefix__ + '*')
        if len(keys) > 0:
            for key in cls.__indexes__:
                cls.__indexes__[key].deleteall()

            return redis.delete(*keys)

        return 0
Beispiel #5
0
    def deleteall(cls):
        if not hasattr(cls, '__prefix__'):
            raise AttributeError(
                "Models must define the attribute: __prefix__ or define one of the model keys as primary"
            )

        keys = redis.keys(cls.__prefix__ + '*')
        if len(keys) > 0:
            for key in cls.__indexes__:
                cls.__indexes__[key].deleteall()

            return redis.delete(*keys)

        return 0
Beispiel #6
0
    def deleteall(self):
        keys = redis.keys(self.__prefix__ + '*')
        if len(keys) > 0:
            return redis.delete(*keys)

        return 0
Beispiel #7
0
 def __delitem__(self, key):
     return redis.delete(self.__keytransform__(key))
Beispiel #8
0
    def deleteall(self):
        keys = redis.keys(self.__prefix__ + '*')
        if len(keys) > 0:
            return redis.delete(*keys)

        return 0
Beispiel #9
0
 def __delitem__(self, key):
     return redis.delete(self.__keytransform__(key))
Beispiel #10
0
def purge_data(listing_id):
    """Purges all data for listing listing_id."""
    r.delete("listings.%s.data" % listing_id)
    r.delete("listings.%s.users" % listing_id)
    r.zrem("treasures", listing_id)