| 13828 |
kshitij.so |
1 |
from elixir import *
|
|
|
2 |
from shop2020.model.v1.catalog.impl import DataService
|
|
|
3 |
from shop2020.model.v1.catalog.impl.DataService import PrivateDeals, Item
|
|
|
4 |
from sqlalchemy.sql.functions import now
|
|
|
5 |
from datetime import datetime, timedelta
|
|
|
6 |
import pymongo
|
|
|
7 |
from dtr.utils.utils import to_java_date
|
| 13843 |
kshitij.so |
8 |
import optparse
|
| 14325 |
kshitij.so |
9 |
from dtr.storage.MemCache import MemCache
|
| 14705 |
kshitij.so |
10 |
from dtr.utils.utils import getCashBack
|
| 13828 |
kshitij.so |
11 |
|
|
|
12 |
dealsMap = {}
|
|
|
13 |
con = None
|
|
|
14 |
dealsCatalogIds = []
|
|
|
15 |
itemCatalogMap = {}
|
|
|
16 |
|
| 13843 |
kshitij.so |
17 |
parser = optparse.OptionParser()
|
| 14259 |
kshitij.so |
18 |
parser.add_option("-H", "--host", dest="hostname",
|
| 13843 |
kshitij.so |
19 |
default="localhost",
|
|
|
20 |
type="string", help="The HOST where the DB server is running",
|
| 14259 |
kshitij.so |
21 |
metavar="db_host")
|
| 14253 |
kshitij.so |
22 |
parser.add_option("-m", "--m", dest="mongoHost",
|
|
|
23 |
default="localhost",
|
|
|
24 |
type="string", help="The HOST where the mongo server is running",
|
|
|
25 |
metavar="mongo_host")
|
| 13828 |
kshitij.so |
26 |
|
| 13849 |
kshitij.so |
27 |
(options, args) = parser.parse_args()
|
| 14325 |
kshitij.so |
28 |
|
|
|
29 |
mc = MemCache(options.mongoHost)
|
|
|
30 |
|
| 13843 |
kshitij.so |
31 |
DataService.initialize(db_hostname=options.hostname)
|
|
|
32 |
|
| 14253 |
kshitij.so |
33 |
def get_mongo_connection(host=options.mongoHost, port=27017):
|
| 13828 |
kshitij.so |
34 |
global con
|
|
|
35 |
if con is None:
|
|
|
36 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
37 |
try:
|
|
|
38 |
con = pymongo.MongoClient(host, port)
|
|
|
39 |
except Exception, e:
|
|
|
40 |
print e
|
|
|
41 |
return None
|
|
|
42 |
return con
|
|
|
43 |
|
|
|
44 |
def getPrivateDeals():
|
| 14259 |
kshitij.so |
45 |
try:
|
|
|
46 |
global dealsMap
|
|
|
47 |
dealsMap = dict()
|
|
|
48 |
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 |
if all_active_private_deals is not None or all_active_private_deals!=[]:
|
|
|
52 |
for active_private_deal in all_active_private_deals:
|
|
|
53 |
item = Item.get_by(id = active_private_deal.item_id)
|
|
|
54 |
if item.sellingPrice > active_private_deal.dealPrice and item.status==3:
|
|
|
55 |
dealsMap[active_private_deal.item_id] = active_private_deal
|
|
|
56 |
finally:
|
|
|
57 |
session.close()
|
| 13828 |
kshitij.so |
58 |
|
|
|
59 |
def getItemsToUpdate():
|
|
|
60 |
global dealsCatalogIds
|
|
|
61 |
global itemCatalogMap
|
| 14130 |
kshitij.so |
62 |
bestSellers = list(get_mongo_connection().Catalog.MasterData.find({'rank':{"$gt":0}}))
|
|
|
63 |
for bestSeller in bestSellers:
|
|
|
64 |
saholicCatalogIds = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':bestSeller['skuBundleId'],'source_id':4}))
|
|
|
65 |
for d in saholicCatalogIds:
|
|
|
66 |
if d['source_id']!=4:
|
|
|
67 |
continue
|
|
|
68 |
dealsCatalogIds.append(long(d['identifier'].strip()))
|
| 14253 |
kshitij.so |
69 |
# dealFlagged = list(get_mongo_connection().Catalog.Deals.find({'source_id':4,'showDeal':1,'totalPoints':{'$gt':0}}))
|
|
|
70 |
# for deal in dealFlagged:
|
|
|
71 |
# if not (deal['_id']) in dealsCatalogIds:
|
|
|
72 |
# dealsCatalogIds.append(long(deal['identifier'].strip()))
|
| 13828 |
kshitij.so |
73 |
items = Item.query.filter(Item.catalog_item_id.in_(dealsCatalogIds)).all()
|
|
|
74 |
for item in items:
|
|
|
75 |
temp = []
|
|
|
76 |
if not itemCatalogMap.has_key(item.catalog_item_id):
|
|
|
77 |
temp.append(item)
|
| 14179 |
kshitij.so |
78 |
print "****",item.catalog_item_id
|
| 13828 |
kshitij.so |
79 |
itemCatalogMap[item.catalog_item_id] = temp
|
|
|
80 |
else:
|
|
|
81 |
val = itemCatalogMap.get(item.catalog_item_id)
|
|
|
82 |
for l in val:
|
|
|
83 |
temp.append(l)
|
|
|
84 |
temp.append(item)
|
|
|
85 |
itemCatalogMap[item.catalog_item_id] = temp
|
|
|
86 |
|
| 14130 |
kshitij.so |
87 |
for saholicCatalogId in bestSellers:
|
| 14129 |
kshitij.so |
88 |
if saholicCatalogId['source_id']!=4:
|
|
|
89 |
continue
|
| 13916 |
kshitij.so |
90 |
d_items = itemCatalogMap.get(long(saholicCatalogId['identifier'].strip()))
|
| 13828 |
kshitij.so |
91 |
available_price = None
|
| 14179 |
kshitij.so |
92 |
in_stock = 0
|
|
|
93 |
if d_items is not None:
|
| 13828 |
kshitij.so |
94 |
for d_item in d_items:
|
| 14179 |
kshitij.so |
95 |
in_stock = 0
|
| 13828 |
kshitij.so |
96 |
if d_item.status == 3:
|
|
|
97 |
in_stock =1
|
| 14179 |
kshitij.so |
98 |
else:
|
|
|
99 |
continue
|
|
|
100 |
if dealsMap.get(d_item.id) is not None:
|
|
|
101 |
available_price = dealsMap.get(d_item.id).dealPrice
|
|
|
102 |
if (available_price !=None):
|
| 13828 |
kshitij.so |
103 |
break
|
| 14179 |
kshitij.so |
104 |
if (available_price is None):
|
|
|
105 |
in_stock = 0
|
|
|
106 |
if d_items is not None:
|
|
|
107 |
for d_item in d_items:
|
|
|
108 |
if d_item.status == 3:
|
|
|
109 |
available_price = d_item.sellingPrice
|
|
|
110 |
in_stock =1
|
|
|
111 |
break
|
| 13828 |
kshitij.so |
112 |
print long(saholicCatalogId['identifier'])
|
|
|
113 |
print in_stock
|
|
|
114 |
print available_price
|
|
|
115 |
print dealsMap.get(d_item.id)
|
|
|
116 |
print "++++++++++++++++++++++++++"
|
| 13850 |
kshitij.so |
117 |
if available_price > 0 or available_price is not None:
|
| 14127 |
kshitij.so |
118 |
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)
|
| 13916 |
kshitij.so |
119 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'available_price':available_price , 'in_stock':in_stock}}, multi=True)
|
| 13828 |
kshitij.so |
120 |
else:
|
| 14127 |
kshitij.so |
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)
|
| 13916 |
kshitij.so |
122 |
get_mongo_connection().Catalog.Deals.update({'_id':saholicCatalogId['_id']}, {'$set' : {'in_stock':in_stock}}, multi=True)
|
| 13828 |
kshitij.so |
123 |
|
| 13916 |
kshitij.so |
124 |
try:
|
|
|
125 |
recomputeDeal(saholicCatalogId['skuBundleId'])
|
|
|
126 |
except:
|
|
|
127 |
print "Unable to compute deal for ",saholicCatalogId['skuBundleId']
|
|
|
128 |
|
| 14325 |
kshitij.so |
129 |
def populateNegativeDeals():
|
|
|
130 |
negativeDeals = get_mongo_connection().Catalog.NegativeDeals.find().distinct('sku')
|
|
|
131 |
mc.set("negative_deals", negativeDeals, 600)
|
|
|
132 |
|
| 13916 |
kshitij.so |
133 |
def recomputeDeal(skuBundleId):
|
|
|
134 |
"""Lets recompute deal for this bundle"""
|
|
|
135 |
print "Recomputing for bundleId",skuBundleId
|
|
|
136 |
|
|
|
137 |
similarItems = list(get_mongo_connection().Catalog.Deals.find({'skuBundleId':skuBundleId}).sort([('available_price',pymongo.ASCENDING)]))
|
|
|
138 |
bestPrice = float("inf")
|
|
|
139 |
bestOne = None
|
|
|
140 |
bestSellerPoints = 0
|
|
|
141 |
toUpdate = []
|
|
|
142 |
for similarItem in similarItems:
|
| 14327 |
kshitij.so |
143 |
if mc.get("negative_deals") is None:
|
| 14325 |
kshitij.so |
144 |
populateNegativeDeals()
|
| 14705 |
kshitij.so |
145 |
try:
|
|
|
146 |
cashBack = getCashBack(similarItem['_id'], similarItem['source_id'], similarItem['category_id'], mc, options.mongoHost)
|
|
|
147 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
|
|
148 |
pass
|
|
|
149 |
else:
|
|
|
150 |
if cashBack['cash_back_type'] ==1:
|
|
|
151 |
similarItem['available_price'] = similarItem['available_price'] - similarItem['available_price'] * float(cashBack['cash_back'])/100
|
|
|
152 |
elif cashBack['cash_back_type'] ==2:
|
|
|
153 |
similarItem['available_price'] = similarItem['available_price'] - float(cashBack['cash_back'])
|
|
|
154 |
else:
|
|
|
155 |
pass
|
|
|
156 |
except Exception as cashBackEx:
|
|
|
157 |
print cashBackEx
|
|
|
158 |
print "Error calculating cashback."
|
| 14326 |
kshitij.so |
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"):
|
| 13916 |
kshitij.so |
160 |
get_mongo_connection().Catalog.Deals.update({ '_id' : similarItem['_id'] }, {'$set':{'showDeal':0 }})
|
|
|
161 |
continue
|
|
|
162 |
if similarItem['available_price'] < bestPrice:
|
|
|
163 |
bestOne = similarItem
|
|
|
164 |
bestPrice = similarItem['available_price']
|
|
|
165 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
166 |
elif similarItem['available_price'] == bestPrice and bestSellerPoints < similarItem['bestSellerPoints']:
|
|
|
167 |
bestOne = similarItem
|
|
|
168 |
bestPrice = similarItem['available_price']
|
|
|
169 |
bestSellerPoints = similarItem['bestSellerPoints']
|
|
|
170 |
else:
|
|
|
171 |
pass
|
|
|
172 |
if bestOne is not None:
|
|
|
173 |
for similarItem in similarItems:
|
|
|
174 |
toUpdate.append(similarItem['_id'])
|
|
|
175 |
toUpdate.remove(bestOne['_id'])
|
|
|
176 |
get_mongo_connection().Catalog.Deals.update({ '_id' : bestOne['_id'] }, {'$set':{'showDeal':1 }})
|
|
|
177 |
if len(toUpdate) > 0:
|
|
|
178 |
get_mongo_connection().Catalog.Deals.update({ '_id' : { "$in": toUpdate } }, {'$set':{'showDeal':0 }},upsert=False, multi=True)
|
|
|
179 |
|
| 13828 |
kshitij.so |
180 |
def main():
|
|
|
181 |
getPrivateDeals()
|
| 14259 |
kshitij.so |
182 |
try:
|
|
|
183 |
getItemsToUpdate()
|
|
|
184 |
finally:
|
|
|
185 |
session.close()
|
| 13828 |
kshitij.so |
186 |
|
|
|
187 |
if __name__=='__main__':
|
|
|
188 |
main()
|