def get_or_create_product_tag(item, list, tab): product = item['product'] try: product_tag = ProductTags.objects.get(product=product, type='sabse_sasta_sale') product_tag.tab = tab product_tag.sort_order = item['row_no'] except ProductTags.DoesNotExist: product_tag = ProductTags() product_tag.product = product product_tag.type = 'sabse_sasta_sale' product_tag.tab = tab product_tag.sort_order = item['row_no'] product_tag.save() return product_tag
def compute(sync): _client_id = 5 last_month = datetime.now() + timedelta(days=-30) # Get srcs of products created before 1 month new_rate_charts = SellerRateChart.objects.select_related('product', 'product__status').filter(product__is_online=True, seller__client=_client_id,\ product__created_on__gte=last_month).order_by("-product__created_on")[:200] new_products = [] for src in new_rate_charts: new_products.append(src.product) # New arrivals products should be atleast 45 items if len(new_products) < 45: new_rate_charts = SellerRateChart.objects.select_related('product', 'product__status').filter(product__is_online=True,\ seller__client=_client_id).order_by("-product__created_on")[:200] new_products = [] for src in new_rate_charts: new_products.append(src.product) # Create popular_deal tag, for indexing products into solr tag_type = 'new_arrivals' tag = Tag.objects.filter(tag=tag_type) if not tag: tag = Tag(tag=tag_type) tag.display_name = tag_type.capitalize() tag.save() else: tag = tag[0] products = [] # variant_products contains product obj of {normal, variant} type # so replace variant products by variable # to reindex in solr variant_products = new_products productvariants = ProductVariant.objects.select_related('blueprint', 'variant').filter(variant__in=variant_products) variant_product_map = {} for pv in productvariants: # get {variable : variant} mapping variant_product_map[pv.variant] = pv.blueprint for product in variant_products: prod = product if product.type == 'variant': try: # get corresponding variable of variant from variable:variant mapping prod = variant_product_map[product] except KeyError: continue if prod not in products: products.append(prod) # products list contains all products with type {variable, normal} product_ids_to_reindexed = [] for product in products: try: pt = ProductTags.objects.select_related('product').get(type=tag_type, product=product, tag=tag) except ProductTags.DoesNotExist: pt = ProductTags() pt.type = tag_type pt.product = product pt.tag = tag pt.save() # if product is not included in previous list then # then create ProductTag for it and reindex it product_ids_to_reindexed.append(int(product.id)) # Get previously indexed new arrivals from solr from utils.solrutils import solr_search q = 'tag_id:%s AND client_id:%s' % (tag.id, _client_id) params = {'rows':1000} solr_result = solr_search(q, fields='id', highlight=None, score=True, sort=None, sort_order="asc", operation='/select',\ active_product_query='', boost_query='', request=None, **params) indexed_new_arrivals = [int(res['id']) for res in solr_result.results] # ProductTags of (previously_indexed_new_arrivals - current_new_arrivals) # should to be removed new_arrivals_to_remove = set(indexed_new_arrivals) - set(product_ids_to_reindexed) product_ids_to_reindexed = set(product_ids_to_reindexed) - set(indexed_new_arrivals) log.info("New Arrivals ID:: %s" % product_ids_to_reindexed) sync.found = len(indexed_new_arrivals) sync.deletes = len(new_arrivals_to_remove) sync.add = len(product_ids_to_reindexed) sync.save() if not product_ids_to_reindexed: log.info("Found no new_arrivals") return products_to_reindexed = Product.objects.filter(id__in=product_ids_to_reindexed) removed_new_arrivals = [] for product_tag in ProductTags.objects.select_related('product').filter(product__in=new_arrivals_to_remove, tag=tag): removed_new_arrivals.append(product_tag.product) product_tag.delete() for product in products_to_reindexed: # Reindex: # 1) new product set (to add tag entry into solr) # 2) old tagged product set (to delete tag entry from solr) product.update_solr_index() for product in removed_new_arrivals: product.update_solr_index()
def index_popular_deals(popular_pending_deals, popular_confirmed_deals, client_id=5): popular_products = popular_pending_deals + popular_confirmed_deals popular_products = Product.objects.filter(id__in=popular_products, is_online=True) if not popular_products: popular_orders = OrderCountByState.objects.select_related('product').filter(client=client_id,\ product__is_online=True).order_by('-order_count')[:200] popular_products = [order.product for order in popular_orders] # Create popular_deal tag, for indexing products into solr popular_type = 'popular_deals' tag = Tag.objects.filter(tag=popular_type) if not tag: tag = Tag(tag=popular_type) tag.display_name = popular_type.capitalize() tag.save() else: tag = tag[0] product_ids_to_reindexed = [] products = [] variant_products = popular_products productvariants = ProductVariant.objects.select_related( 'blueprint', 'variant').filter(variant__in=variant_products) variant_product_map = {} for pv in productvariants: variant_product_map[pv.variant] = pv.blueprint for product in variant_products: prod = product if product.type == 'variant': try: prod = variant_product_map[product] except KeyError: continue if prod not in products: products.append(prod) for product in products: try: pt = ProductTags.objects.select_related('product').get( type=popular_type, product=product, tag=tag) except ProductTags.DoesNotExist: pt = ProductTags() pt.type = popular_type pt.product = product pt.tag = tag pt.save() product_ids_to_reindexed.append(int(product.id)) from utils.solrutils import solr_search q = 'tag_id:%s AND client_id:%s' % (tag.id, client_id) params = {'rows': 1000} solr_result = solr_search(q, fields='id', highlight=None, score=True, sort=None, sort_order="asc", operation='/select',\ active_product_query='', boost_query='', request=None, **params) indexed_popular_deals = [int(res['id']) for res in solr_result.results] popular_deals_to_remove = set(indexed_popular_deals) - set( product_ids_to_reindexed) product_ids_to_reindexed = set(product_ids_to_reindexed) - set( indexed_popular_deals) log.info("Popular Deals ID:: %s" % product_ids_to_reindexed) sync.found = len(indexed_popular_deals) sync.deletes = len(popular_deals_to_remove) sync.add = len(product_ids_to_reindexed) sync.save() if not product_ids_to_reindexed: log.info("Found no popular_deals") return products_to_reindexed = Product.objects.filter( id__in=product_ids_to_reindexed) removed_popular_deals = [] for product_tag in ProductTags.objects.select_related('product').filter( product__in=popular_deals_to_remove, tag=tag): removed_popular_deals.append(product_tag.product) product_tag.delete() for product in products_to_reindexed: # Reindex: # 1) new product set (to add tag entry into solr) # 2) old tagged product set (to delete tag entry from solr) product.update_solr_index() for product in removed_popular_deals: product.update_solr_index()
def index_popular_deals(popular_pending_deals, popular_confirmed_deals, client_id=5): popular_products = popular_pending_deals + popular_confirmed_deals popular_products = Product.objects.filter(id__in=popular_products, is_online=True) if not popular_products: popular_orders = OrderCountByState.objects.select_related('product').filter(client=client_id,\ product__is_online=True).order_by('-order_count')[:200] popular_products = [order.product for order in popular_orders] # Create popular_deal tag, for indexing products into solr popular_type = 'popular_deals' tag = Tag.objects.filter(tag=popular_type) if not tag: tag = Tag(tag=popular_type) tag.display_name = popular_type.capitalize() tag.save() else: tag = tag[0] product_ids_to_reindexed = [] products = [] variant_products = popular_products productvariants = ProductVariant.objects.select_related('blueprint', 'variant').filter(variant__in=variant_products) variant_product_map = {} for pv in productvariants: variant_product_map[pv.variant] = pv.blueprint for product in variant_products: prod = product if product.type == 'variant': try: prod = variant_product_map[product] except KeyError: continue if prod not in products: products.append(prod) for product in products: try: pt = ProductTags.objects.select_related('product').get(type=popular_type, product=product, tag=tag) except ProductTags.DoesNotExist: pt = ProductTags() pt.type = popular_type pt.product = product pt.tag = tag pt.save() product_ids_to_reindexed.append(int(product.id)) from utils.solrutils import solr_search q = 'tag_id:%s AND client_id:%s' % (tag.id, client_id) params = {'rows':1000} solr_result = solr_search(q, fields='id', highlight=None, score=True, sort=None, sort_order="asc", operation='/select',\ active_product_query='', boost_query='', request=None, **params) indexed_popular_deals = [int(res['id']) for res in solr_result.results] popular_deals_to_remove = set(indexed_popular_deals) - set(product_ids_to_reindexed) product_ids_to_reindexed = set(product_ids_to_reindexed) - set(indexed_popular_deals) log.info("Popular Deals ID:: %s" % product_ids_to_reindexed) sync.found = len(indexed_popular_deals) sync.deletes = len(popular_deals_to_remove) sync.add = len(product_ids_to_reindexed) sync.save() if not product_ids_to_reindexed: log.info("Found no popular_deals") return products_to_reindexed = Product.objects.filter(id__in=product_ids_to_reindexed) removed_popular_deals= [] for product_tag in ProductTags.objects.select_related('product').filter(product__in=popular_deals_to_remove, tag=tag): removed_popular_deals.append(product_tag.product) product_tag.delete() for product in products_to_reindexed: # Reindex: # 1) new product set (to add tag entry into solr) # 2) old tagged product set (to delete tag entry from solr) product.update_solr_index() for product in removed_popular_deals: product.update_solr_index()
def compute(sync): _client_id = 5 last_month = datetime.now() + timedelta(days=-30) # Get srcs of products created before 1 month new_rate_charts = SellerRateChart.objects.select_related('product', 'product__status').filter(product__is_online=True, seller__client=_client_id,\ product__created_on__gte=last_month).order_by("-product__created_on")[:200] new_products = [] for src in new_rate_charts: new_products.append(src.product) # New arrivals products should be atleast 45 items if len(new_products) < 45: new_rate_charts = SellerRateChart.objects.select_related('product', 'product__status').filter(product__is_online=True,\ seller__client=_client_id).order_by("-product__created_on")[:200] new_products = [] for src in new_rate_charts: new_products.append(src.product) # Create popular_deal tag, for indexing products into solr tag_type = 'new_arrivals' tag = Tag.objects.filter(tag=tag_type) if not tag: tag = Tag(tag=tag_type) tag.display_name = tag_type.capitalize() tag.save() else: tag = tag[0] products = [] # variant_products contains product obj of {normal, variant} type # so replace variant products by variable # to reindex in solr variant_products = new_products productvariants = ProductVariant.objects.select_related( 'blueprint', 'variant').filter(variant__in=variant_products) variant_product_map = {} for pv in productvariants: # get {variable : variant} mapping variant_product_map[pv.variant] = pv.blueprint for product in variant_products: prod = product if product.type == 'variant': try: # get corresponding variable of variant from variable:variant mapping prod = variant_product_map[product] except KeyError: continue if prod not in products: products.append(prod) # products list contains all products with type {variable, normal} product_ids_to_reindexed = [] for product in products: try: pt = ProductTags.objects.select_related('product').get( type=tag_type, product=product, tag=tag) except ProductTags.DoesNotExist: pt = ProductTags() pt.type = tag_type pt.product = product pt.tag = tag pt.save() # if product is not included in previous list then # then create ProductTag for it and reindex it product_ids_to_reindexed.append(int(product.id)) # Get previously indexed new arrivals from solr from utils.solrutils import solr_search q = 'tag_id:%s AND client_id:%s' % (tag.id, _client_id) params = {'rows': 1000} solr_result = solr_search(q, fields='id', highlight=None, score=True, sort=None, sort_order="asc", operation='/select',\ active_product_query='', boost_query='', request=None, **params) indexed_new_arrivals = [int(res['id']) for res in solr_result.results] # ProductTags of (previously_indexed_new_arrivals - current_new_arrivals) # should to be removed new_arrivals_to_remove = set(indexed_new_arrivals) - set( product_ids_to_reindexed) product_ids_to_reindexed = set(product_ids_to_reindexed) - set( indexed_new_arrivals) log.info("New Arrivals ID:: %s" % product_ids_to_reindexed) sync.found = len(indexed_new_arrivals) sync.deletes = len(new_arrivals_to_remove) sync.add = len(product_ids_to_reindexed) sync.save() if not product_ids_to_reindexed: log.info("Found no new_arrivals") return products_to_reindexed = Product.objects.filter( id__in=product_ids_to_reindexed) removed_new_arrivals = [] for product_tag in ProductTags.objects.select_related('product').filter( product__in=new_arrivals_to_remove, tag=tag): removed_new_arrivals.append(product_tag.product) product_tag.delete() for product in products_to_reindexed: # Reindex: # 1) new product set (to add tag entry into solr) # 2) old tagged product set (to delete tag entry from solr) product.update_solr_index() for product in removed_new_arrivals: product.update_solr_index()