| Line 6... |
Line 6... |
| 6 |
import pymongo
|
6 |
import pymongo
|
| 7 |
from dtr.utils.utils import to_java_date
|
7 |
from dtr.utils.utils import to_java_date
|
| 8 |
import optparse
|
8 |
import optparse
|
| 9 |
from dtr.storage.MemCache import MemCache
|
9 |
from dtr.storage.MemCache import MemCache
|
| 10 |
from dtr.utils.utils import getCashBack
|
10 |
from dtr.utils.utils import getCashBack
|
| - |
|
11 |
from dtr.utils.utils import getNlcPoints
|
| - |
|
12 |
import traceback
|
| 11 |
|
13 |
|
| 12 |
dealsMap = {}
|
14 |
dealsMap = {}
|
| 13 |
con = None
|
15 |
con = None
|
| 14 |
dealsCatalogIds = []
|
16 |
dealsCatalogIds = []
|
| 15 |
itemCatalogMap = {}
|
17 |
itemCatalogMap = {}
|
| Line 44... |
Line 46... |
| 44 |
def getPrivateDeals():
|
46 |
def getPrivateDeals():
|
| 45 |
try:
|
47 |
try:
|
| 46 |
global dealsMap
|
48 |
global dealsMap
|
| 47 |
dealsMap = dict()
|
49 |
dealsMap = dict()
|
| 48 |
all_active_items_query = session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate))
|
50 |
all_active_items_query = session.query(PrivateDeals).filter(PrivateDeals.isActive==True).filter(now().between(PrivateDeals.startDate, PrivateDeals.endDate))
|
| 49 |
print all_active_items_query
|
- |
|
| 50 |
all_active_private_deals = all_active_items_query.all()
|
51 |
all_active_private_deals = all_active_items_query.all()
|
| 51 |
if all_active_private_deals is not None or all_active_private_deals!=[]:
|
52 |
if all_active_private_deals is not None or all_active_private_deals!=[]:
|
| 52 |
for active_private_deal in all_active_private_deals:
|
53 |
for active_private_deal in all_active_private_deals:
|
| - |
|
54 |
print active_private_deal.item_id
|
| 53 |
item = Item.get_by(id = active_private_deal.item_id)
|
55 |
item = Item.get_by(id = active_private_deal.item_id)
|
| 54 |
if item.sellingPrice > active_private_deal.dealPrice and item.status==3:
|
56 |
if item.sellingPrice > active_private_deal.dealPrice and item.status==3:
|
| 55 |
dealsMap[active_private_deal.item_id] = active_private_deal
|
57 |
dealsMap[active_private_deal.item_id] = active_private_deal
|
| 56 |
finally:
|
58 |
finally:
|
| 57 |
session.close()
|
59 |
session.close()
|
| 58 |
|
60 |
|
| 59 |
def getItemsToUpdate():
|
61 |
def getItemsToUpdate():
|
| 60 |
global dealsCatalogIds
|
62 |
global dealsCatalogIds
|
| 61 |
global itemCatalogMap
|
63 |
global itemCatalogMap
|
| - |
|
64 |
toScrapMap = {}
|
| 62 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0}}))
|
65 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0}}))
|
| 63 |
for bestSeller in bestSellers:
|
66 |
for bestSeller in bestSellers:
|
| 64 |
saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))
|
67 |
saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))
|
| 65 |
for d in saholicCatalogIds:
|
68 |
for d in saholicCatalogIds:
|
| 66 |
if d['source_id']!=4:
|
69 |
if d['source_id']!=4:
|
| 67 |
continue
|
70 |
continue
|
| - |
|
71 |
d['dealFlag'] = 0
|
| - |
|
72 |
d['dealType'] = 0
|
| - |
|
73 |
d['dealPoints'] = 0
|
| - |
|
74 |
d['manualDealThresholdPrice'] = None
|
| - |
|
75 |
toScrapMap[d['_id']] = d
|
| 68 |
dealsCatalogIds.append(long(d['identifier'].strip()))
|
76 |
dealsCatalogIds.append(long(d['identifier'].strip()))
|
| 69 |
# dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':0}}))
|
77 |
dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':0}}))
|
| 70 |
# for deal in dealFlagged:
|
78 |
for deal in dealFlagged:
|
| - |
|
79 |
if not toScrapMap.has_key(deal['_id']):
|
| - |
|
80 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']}))
|
| - |
|
81 |
data[0]['dealFlag'] = 0
|
| - |
|
82 |
data[0]['dealType'] = 0
|
| - |
|
83 |
data[0]['dealPoints'] = 0
|
| - |
|
84 |
data[0]['manualDealThresholdPrice'] = None
|
| - |
|
85 |
toScrapMap[deal['_id']] = data[0]
|
| 71 |
# if not (deal['_id']) in dealsCatalogIds:
|
86 |
if long(data[0]['identifier'].strip()) not in dealsCatalogIds:
|
| 72 |
# dealsCatalogIds.append(long(deal['identifier'].strip()))
|
87 |
dealsCatalogIds.append(long(data[0]['identifier'].strip()))
|
| - |
|
88 |
manualDeals = list(get_mongo_connection().Catalog.ManualDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())},'source_id':4}))
|
| - |
|
89 |
for manualDeal in manualDeals:
|
| - |
|
90 |
if not toScrapMap.has_key(manualDeal['sku']):
|
| - |
|
91 |
data = list(get_mongo_connection().Catalog.MasterData.find({'_id':manualDeal['sku']}))
|
| - |
|
92 |
if len(data) > 0:
|
| - |
|
93 |
data[0]['dealFlag'] = 1
|
| - |
|
94 |
data[0]['dealType'] = manualDeal['dealType']
|
| - |
|
95 |
data[0]['dealPoints'] = manualDeal['dealPoints']
|
| - |
|
96 |
data[0]['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
|
| - |
|
97 |
toScrapMap[manualDeal['sku']] = data[0]
|
| - |
|
98 |
if long(data[0]['identifier'].strip()) not in dealsCatalogIds:
|
| - |
|
99 |
dealsCatalogIds.append(long(data[0]['identifier'].strip()))
|
| - |
|
100 |
else:
|
| - |
|
101 |
data = toScrapMap.get(manualDeal['sku'])
|
| - |
|
102 |
data['dealFlag'] = 1
|
| - |
|
103 |
data['dealType'] = manualDeal['dealType']
|
| - |
|
104 |
data['dealPoints'] = manualDeal['dealPoints']
|
| - |
|
105 |
data['manualDealThresholdPrice'] = manualDeal['dealThresholdPrice']
|
| - |
|
106 |
|
| 73 |
items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
|
107 |
items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
|
| 74 |
for item in items:
|
108 |
for item in items:
|
| 75 |
temp = []
|
109 |
temp = []
|
| 76 |
if not itemCatalogMap.has_key(item.catalog_item_id):
|
110 |
if not itemCatalogMap.has_key(item.catalog_item_id):
|
| 77 |
temp.append(item)
|
111 |
temp.append(item)
|
| Line 82... |
Line 116... |
| 82 |
for l in val:
|
116 |
for l in val:
|
| 83 |
temp.append(l)
|
117 |
temp.append(l)
|
| 84 |
temp.append(item)
|
118 |
temp.append(item)
|
| 85 |
itemCatalogMap[item.catalog_item_id] = temp
|
119 |
itemCatalogMap[item.catalog_item_id] = temp
|
| 86 |
|
120 |
|
| 87 |
for saholicCatalogId in bestSellers:
|
121 |
for saholicCatalogId in toScrapMap.itervalues():
|
| 88 |
if saholicCatalogId['source_id']!=4:
|
122 |
if saholicCatalogId['source_id']!=4:
|
| 89 |
continue
|
123 |
continue
|
| 90 |
d_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))
|
124 |
d_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))
|
| 91 |
available_price = None
|
125 |
available_price = None
|
| 92 |
in_stock = 0
|
126 |
in_stock = 0
|
| Line 120... |
Line 154... |
| 120 |
else:
|
154 |
else:
|
| 121 |
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)
|
155 |
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)
|
| 122 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock}}, multi=True)
|
156 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock}}, multi=True)
|
| 123 |
|
157 |
|
| 124 |
try:
|
158 |
try:
|
| 125 |
recomputeDeal(saholicCatalogId['skuBundleId'])
|
159 |
recomputeDeal(saholicCatalogId)
|
| 126 |
except:
|
160 |
except:
|
| 127 |
print "Unable to compute deal for ",saholicCatalogId['skuBundleId']
|
161 |
print "Unable to compute deal for ",saholicCatalogId['skuBundleId']
|
| 128 |
|
162 |
|
| 129 |
def populateNegativeDeals():
|
163 |
def populateNegativeDeals():
|
| 130 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
164 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
| 131 |
mc.set("negative_deals", negativeDeals, 600)
|
165 |
mc.set("negative_deals", negativeDeals, 600)
|
| - |
|
166 |
|
| - |
|
167 |
def recomputePoints(item, deal):
|
| - |
|
168 |
try:
|
| - |
|
169 |
nlcPoints = getNlcPoints(item, deal['minNlc'], deal['maxNlc'], deal['available_price'])
|
| - |
|
170 |
except:
|
| - |
|
171 |
traceback.print_exc()
|
| - |
|
172 |
nlcPoints = deal['nlcPoints']
|
| - |
|
173 |
if item['manualDealThresholdPrice'] >= deal['available_price']:
|
| - |
|
174 |
dealPoints = item['dealPoints']
|
| - |
|
175 |
else:
|
| - |
|
176 |
dealPoints = 0
|
| - |
|
177 |
get_mongo_connection().Catalog.Deals.update({'_id':deal['_id']},{"$set":{'totalPoints':deal['totalPoints'] - deal['nlcPoints'] + nlcPoints - deal['dealPoints'] +dealPoints , 'nlcPoints': nlcPoints, 'dealPoints': dealPoints, 'manualDealThresholdPrice': item['manualDealThresholdPrice']}})
|
| 132 |
|
178 |
|
| - |
|
179 |
|
| - |
|
180 |
|
| - |
|
181 |
|
| 133 |
def recomputeDeal(skuBundleId):
|
182 |
def recomputeDeal(item):
|
| 134 |
"""Lets recompute deal for this bundle"""
|
183 |
"""Lets recompute deal for this bundle"""
|
| 135 |
print "Recomputing for bundleId",skuBundleId
|
184 |
print "Recomputing for bundleId",item.get('skuBundleId')
|
| - |
|
185 |
skuBundleId = item['skuBundleId']
|
| 136 |
|
186 |
|
| 137 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
187 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
| 138 |
bestPrice = float("inf")
|
188 |
bestPrice = float("inf")
|
| 139 |
bestOne = None
|
189 |
bestOne = None
|
| 140 |
bestSellerPoints = 0
|
190 |
bestSellerPoints = 0
|
| 141 |
toUpdate = []
|
191 |
toUpdate = []
|
| 142 |
for similarItem in similarItems:
|
192 |
for similarItem in similarItems:
|
| 143 |
if mc.get("negative_deals") is None:
|
193 |
if mc.get("negative_deals") is None:
|
| 144 |
populateNegativeDeals()
|
194 |
populateNegativeDeals()
|
| 145 |
try:
|
195 |
# try:
|
| 146 |
cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
|
196 |
# cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
|
| 147 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
197 |
# if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 148 |
pass
|
198 |
# pass
|
| 149 |
else:
|
199 |
# else:
|
| 150 |
if cashBack['cash_back_type'] ==1:
|
200 |
# if cashBack['cash_back_type'] ==1:
|
| 151 |
similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
|
201 |
# similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
|
| 152 |
elif cashBack['cash_back_type'] ==2:
|
202 |
# elif cashBack['cash_back_type'] ==2:
|
| 153 |
similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
|
203 |
# similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
|
| 154 |
else:
|
204 |
# else:
|
| 155 |
pass
|
205 |
# pass
|
| 156 |
except Exception as cashBackEx:
|
206 |
# except Exception as cashBackEx:
|
| 157 |
print cashBackEx
|
207 |
# print cashBackEx
|
| 158 |
print "Error calculating cashback."
|
208 |
# print "Error calculating cashback."
|
| - |
|
209 |
if similarItem['_id'] == item['_id']:
|
| - |
|
210 |
try:
|
| - |
|
211 |
recomputePoints(item, similarItem)
|
| - |
|
212 |
except:
|
| - |
|
213 |
traceback.print_exc()
|
| 159 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
214 |
if similarItem['in_stock'] == 0 or similarItem['maxprice'] is None or similarItem['maxprice'] < similarItem['available_price'] or similarItem['_id'] in mc.get("negative_deals"):
|
| 160 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
215 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
| 161 |
continue
|
216 |
continue
|
| 162 |
if similarItem['available_price'] < bestPrice:
|
217 |
if similarItem['available_price'] < bestPrice:
|
| 163 |
bestOne = similarItem
|
218 |
bestOne = similarItem
|
| Line 181... |
Line 236... |
| 181 |
getPrivateDeals()
|
236 |
getPrivateDeals()
|
| 182 |
try:
|
237 |
try:
|
| 183 |
getItemsToUpdate()
|
238 |
getItemsToUpdate()
|
| 184 |
finally:
|
239 |
finally:
|
| 185 |
session.close()
|
240 |
session.close()
|
| - |
|
241 |
print "Done with saholic pricing"
|
| 186 |
|
242 |
|
| 187 |
if __name__=='__main__':
|
243 |
if __name__=='__main__':
|
| 188 |
main()
|
244 |
main()
|
| 189 |
|
245 |
|