Rev 14327 | Rev 16019 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from elixir import *from shop2020.model.v1.catalog.impl import DataServicefrom shop2020.model.v1.catalog.impl.DataService import PrivateDeals, Itemfrom sqlalchemy.sql.functions import nowfrom datetime import datetime, timedeltaimport pymongofrom dtr.utils.utils import to_java_dateimport optparsefrom dtr.storage.MemCache import MemCachefrom dtr.utils.utils import getCashBackdealsMap = {}con = NonedealsCatalogIds = []itemCatalogMap = {}parser = optparse.OptionParser()parser.add_option("-H", "--host", dest="hostname",default="localhost",type="string", help="The HOST where the DB server is running",metavar="db_host")parser.add_option("-m", "--m", dest="mongoHost",default="localhost",type="string", help="The HOST where the mongo server is running",metavar="mongo_host")(options, args) = parser.parse_args()mc = MemCache(options.mongoHost)DataService.initialize(db_hostname=options.hostname)def get_mongo_connection(host=options.mongoHost, port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef getPrivateDeals():try:global dealsMapdealsMap = dict()all_active_items_query = session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate))print all_active_items_queryall_active_private_deals = all_active_items_query.all()if all_active_private_deals is not None or all_active_private_deals!=[]:for active_private_deal in all_active_private_deals:item = Item.get_by(id = active_private_deal.item_id)if item.sellingPrice > active_private_deal.dealPrice and item.status==3:dealsMap[active_private_deal.item_id] = active_private_dealfinally:session.close()def getItemsToUpdate():global dealsCatalogIdsglobal itemCatalogMapbestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0}}))for bestSeller in bestSellers:saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))for d in saholicCatalogIds:if d['source_id']!=4:continuedealsCatalogIds.append(long(d['identifier'].strip()))# dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':0}}))# for deal in dealFlagged:# if not (deal['_id']) in dealsCatalogIds:# dealsCatalogIds.append(long(deal['identifier'].strip()))items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()for item in items:temp = []if not itemCatalogMap.has_key(item.catalog_item_id):temp.append(item)print "****",item.catalog_item_iditemCatalogMap[item.catalog_item_id] = tempelse:val = itemCatalogMap.get(item.catalog_item_id)for l in val:temp.append(l)temp.append(item)itemCatalogMap[item.catalog_item_id] = tempfor saholicCatalogId in bestSellers:if saholicCatalogId['source_id']!=4:continued_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))available_price = Nonein_stock = 0if d_items is not None:for d_item in d_items:in_stock = 0if d_item.status == 3:in_stock =1else:continueif dealsMap.get(d_item.id) is not None:available_price = dealsMap.get(d_item.id).dealPriceif (available_price !=None):breakif (available_price is None):in_stock = 0if d_items is not None:for d_item in d_items:if d_item.status == 3:available_price = d_item.sellingPricein_stock =1breakprint long(saholicCatalogId['identifier'])print in_stockprint available_priceprint dealsMap.get(d_item.id)print "++++++++++++++++++++++++++"if available_price > 0 or available_price is not None:get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':in_stock}}, multi=True)get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price , 'in_stock':in_stock}}, multi=True)else:get_mongo_connection().Catalog.MasterData.update({'_id':saholicCatalogId['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':in_stock,'priceUpdatedOn':to_java_date(datetime.now())}}, multi=True)get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock}}, multi=True)try:recomputeDeal(saholicCatalogId['skuBundleId'])except:print "Unable to compute deal for ",saholicCatalogId['skuBundleId']def populateNegativeDeals():negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')mc.set("negative_deals", negativeDeals, 600)def recomputeDeal(skuBundleId):"""Lets recompute deal for this bundle"""print "Recomputing for bundleId",skuBundleIdsimilarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))bestPrice = float("inf")bestOne = NonebestSellerPoints = 0toUpdate = []for similarItem in similarItems:if mc.get("negative_deals") is None:populateNegativeDeals()try:cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)if not cashBack or cashBack.get('cash_back_status')!=1:passelse:if cashBack['cash_back_type'] ==1:similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100elif cashBack['cash_back_type'] ==2:similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])else:passexcept Exception as cashBackEx:print cashBackExprint "Error calculating cashback."if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})continueif similarItem['available_price'] < bestPrice:bestOne = similarItembestPrice = similarItem['available_price']bestSellerPoints = similarItem['bestSellerPoints']elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:bestOne = similarItembestPrice = similarItem['available_price']bestSellerPoints = similarItem['bestSellerPoints']else:passif bestOne is not None:for similarItem in similarItems:toUpdate.append(similarItem['_id'])toUpdate.remove(bestOne['_id'])get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})if len(toUpdate) > 0:get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)def main():getPrivateDeals()try:getItemsToUpdate()finally:session.close()if __name__=='__main__':main()