Subversion Repositories SmartDukaan

Rev

Rev 17264 | Rev 20347 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17264 Rev 19187
Line 33... Line 33...
33
        except Exception, e:
33
        except Exception, e:
34
            print e
34
            print e
35
            return None
35
            return None
36
    return con
36
    return con
37
 
37
 
-
 
38
def getNetPriceForItem(itemId, source_id, category_id ,price):
-
 
39
    cash_back_type = 0
-
 
40
    cash_back = 0
-
 
41
    try:
-
 
42
        cashBack = getCashBack(itemId, source_id, category_id, mc, options.mongoHost)
-
 
43
        if not cashBack or cashBack.get('cash_back_status')!=1:
-
 
44
            cash_back_type = 0
-
 
45
            cash_back = 0 
-
 
46
            
-
 
47
        else:
-
 
48
            if cashBack['cash_back_type'] in (1,2):
-
 
49
                
-
 
50
                if cashBack.get('maxCashBack') is not None:
-
 
51
                    
-
 
52
                    if cashBack.get('cash_back_type') ==1 and (float(cashBack.get('cash_back'))*price)/100 > cashBack.get('maxCashBack'):
-
 
53
                        cashBack['cash_back_type'] = 2
-
 
54
                        cashBack['cash_back'] = cashBack['maxCashBack']
-
 
55
                    elif cashBack.get('cash_back_type') ==2 and cashBack.get('cash_back') > cashBack.get('maxCashBack'):
-
 
56
                        cashBack['cash_back'] = cashBack['maxCashBack']
-
 
57
                    else:
-
 
58
                        pass
-
 
59
                
-
 
60
                
-
 
61
                
-
 
62
                cash_back_type = cashBack['cash_back_type']
-
 
63
                cash_back = float(cashBack['cash_back'])
-
 
64
    except Exception as cashBackEx:
-
 
65
        pass
-
 
66
    
-
 
67
    if cash_back_type ==1:
-
 
68
        return (price - float(cash_back)*price/100)
-
 
69
    elif cash_back_type ==2:
-
 
70
        return (price - cash_back)
-
 
71
    else:
-
 
72
        return price
-
 
73
 
-
 
74
 
38
def populate():
75
def populate():
39
    toScrapMap = {}
76
    toScrapMap = {}
40
    bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
77
    bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{'$gt':0}}))
41
    for bestSeller in bestSellers: 
78
    for bestSeller in bestSellers: 
42
        amazonBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':2}))
79
        amazonBestSellers = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':2}))
Line 109... Line 146...
109
    print lowestSp
146
    print lowestSp
110
    print inStock
147
    print inStock
111
    if buyBoxPrice is not None and buyBoxPrice == lowestSp:
148
    if buyBoxPrice is not None and buyBoxPrice == lowestSp:
112
        isBuyBox = 1
149
        isBuyBox = 1
113
    if lowestSp > 0:
150
    if lowestSp > 0:
-
 
151
        netPriceAfterCashBack = getNetPriceForItem(data['_id'], SOURCE_MAP.get('FLIPKART'), data['category_id'], lowestSp)
114
        get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':inStock,'buyBoxFlag':isBuyBox}}, multi=True)
152
        get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp,'updatedOn':to_java_date(datetime.now()),'priceUpdatedOn':to_java_date(datetime.now()),'in_stock':inStock,'buyBoxFlag':isBuyBox}}, multi=True)
115
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock,'codAvailable':data['codAvailable']}}, multi=True)
153
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'available_price':lowestSp , 'in_stock':inStock,'codAvailable':data['codAvailable'],'netPriceAfterCashBack':netPriceAfterCashBack}}, multi=True)
116
    else:
154
    else:
-
 
155
        netPriceAfterCashBack = getNetPriceForItem(data['_id'], SOURCE_MAP.get('FLIPKART'), data['category_id'], data['available_price'])
117
        get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':inStock,'priceUpdatedOn':to_java_date(datetime.now()),'buyBoxFlag':isBuyBox}}, multi=True)
156
        get_mongo_connection().Catalog.MasterData.update({'_id':data['_id']}, {'$set' : {'updatedOn':to_java_date(datetime.now()),'in_stock':inStock,'priceUpdatedOn':to_java_date(datetime.now()),'buyBoxFlag':isBuyBox}}, multi=True)
118
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock,'codAvailable':data['codAvailable']}})
157
        get_mongo_connection().Catalog.Deals.update({'_id':data['_id']}, {'$set' : {'in_stock':inStock,'codAvailable':data['codAvailable'],'netPriceAfterCashBack':netPriceAfterCashBack}})
119
        
158
        
120
    try:
159
    try:
121
        recomputeDeal(data)
160
        recomputeDeal(data)
122
    except:
161
    except:
123
        print "Unable to compute deal for %s"%(data['skuBundleId'])
162
        print "Unable to compute deal for %s"%(data['skuBundleId'])
Line 149... Line 188...
149
def recomputeDeal(item):
188
def recomputeDeal(item):
150
    """Lets recompute deal for this bundle"""
189
    """Lets recompute deal for this bundle"""
151
    print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
190
    print "Recomputing for bundleId %d" %(item.get('skuBundleId'))
152
    skuBundleId = item['skuBundleId']
191
    skuBundleId = item['skuBundleId']
153
    
192
    
154
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
193
    similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('netPriceAfterCashBack',pymongo.ASCENDING)]))
155
    bestPrice = float("inf")
194
    bestPrice = float("inf")
156
    bestOne = None
195
    bestOne = None
157
    bestSellerPoints = 0
196
    bestSellerPoints = 0
158
    toUpdate = []
197
    toUpdate = []
159
    prepaidBestPrice = float("inf")
198
    prepaidBestPrice = float("inf")
Line 167... Line 206...
167
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
206
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
168
                continue
207
                continue
169
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
208
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
170
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
209
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
171
                continue
210
                continue
172
            if similarItem['available_price'] < bestPrice:
211
            if similarItem.get('netPriceAfterCashBack') < bestPrice:
173
                bestOne = similarItem
212
                bestOne = similarItem
174
                bestPrice = similarItem['available_price']
213
                bestPrice = similarItem.get('netPriceAfterCashBack')
175
                bestSellerPoints = similarItem['bestSellerPoints']
214
                bestSellerPoints = similarItem['bestSellerPoints']
176
            elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
215
            elif similarItem.get('netPriceAfterCashBack') == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
177
                bestOne = similarItem
216
                bestOne = similarItem
178
                bestPrice = similarItem['available_price']
217
                bestPrice = similarItem.get('netPriceAfterCashBack')
179
                bestSellerPoints = similarItem['bestSellerPoints']
218
                bestSellerPoints = similarItem['bestSellerPoints']
180
            else:
219
            else:
181
                pass
220
                pass
182
        else:
221
        else:
183
            if mc.get("negative_deals") is None:
222
            if mc.get("negative_deals") is None:
Line 186... Line 225...
186
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
225
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0, 'prepaidDeal':0 }})
187
                continue
226
                continue
188
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
227
            if similarItem['source_id'] == SOURCE_MAP.get('SHOPCLUES.COM') and similarItem['rank']==0:
189
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
228
                get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':0 }})
190
                continue
229
                continue
191
            if similarItem['source_id'] == SOURCE_MAP.get('PAYTM.COM'):
-
 
192
                similarItem['available_price'] = similarItem['gross_price']
-
 
193
            if similarItem['available_price'] < prepaidBestPrice:
230
            if similarItem.get('netPriceAfterCashBack') < prepaidBestPrice:
194
                prepaidBestOne = similarItem
231
                prepaidBestOne = similarItem
195
                prepaidBestPrice = similarItem['available_price']
232
                prepaidBestPrice = similarItem.get('netPriceAfterCashBack')
196
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
233
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
197
            elif similarItem['available_price'] == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
234
            elif similarItem.get('netPriceAfterCashBack') == prepaidBestPrice and prepaidBestSellerPoints < similarItem['bestSellerPoints']:
198
                prepaidBestOne = similarItem
235
                prepaidBestOne = similarItem
199
                prepaidBestPrice = similarItem['available_price']
236
                prepaidBestPrice = similarItem.get('netPriceAfterCashBack')
200
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
237
                prepaidBestSellerPoints = similarItem['bestSellerPoints']
201
            else:
238
            else:
202
                pass
239
                pass
203
    if bestOne is not None or prepaidBestOne is not None:
240
    if bestOne is not None or prepaidBestOne is not None:
204
        for similarItem in similarItems:
241
        for similarItem in similarItems:
Line 206... Line 243...
206
        if bestOne is not None:
243
        if bestOne is not None:
207
            toUpdate.remove(bestOne['_id'])
244
            toUpdate.remove(bestOne['_id'])
208
            get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
245
            get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1,'prepaidDeal':0 }})
209
        if prepaidBestOne is not None:
246
        if prepaidBestOne is not None:
210
            if bestOne is not None:
247
            if bestOne is not None:
211
                if prepaidBestOne['available_price'] < bestOne['available_price']: 
248
                if prepaidBestOne.get('netPriceAfterCashBack') < bestOne.get('netPriceAfterCashBack'): 
212
                    toUpdate.remove(prepaidBestOne['_id'])
249
                    toUpdate.remove(prepaidBestOne['_id'])
213
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
250
                    get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
214
            else:
251
            else:
215
                toUpdate.remove(prepaidBestOne['_id'])
252
                toUpdate.remove(prepaidBestOne['_id'])
216
                get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
253
                get_mongo_connection().Catalog.Deals.update({ '_id' : prepaidBestOne['_id'] }, {'$set':{'showDeal':0,'prepaidDeal':1 }})
217
    if len(toUpdate) > 0:
254
    if len(toUpdate) > 0:
218
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
255
        get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0,'prepaidDeal':0 }},upsert=False, multi=True)
219
 
256
 
-
 
257
 
220
def main():
258
def main():
221
    populate()
259
    populate()
222
            
260
            
223
if __name__=='__main__':
261
if __name__=='__main__':
224
    main()
262
    main()
225
263