| 13928 |
kshitij.so |
1 |
import pymongo
|
| 14037 |
kshitij.so |
2 |
from elixir import *
|
| 13572 |
kshitij.so |
3 |
from datetime import datetime
|
| 13986 |
amit.gupta |
4 |
from datetime import timedelta
|
| 13927 |
amit.gupta |
5 |
from dtr.main import Store
|
| 13785 |
kshitij.so |
6 |
from dtr.utils.utils import to_java_date
|
| 13970 |
kshitij.so |
7 |
from operator import itemgetter
|
|
|
8 |
from bson.objectid import ObjectId
|
| 14037 |
kshitij.so |
9 |
from dtr.storage import DataService
|
|
|
10 |
from dtr.storage.DataService import price_preferences, brand_preferences, \
|
|
|
11 |
user_actions
|
|
|
12 |
from dtr.storage.MemCache import MemCache
|
| 13572 |
kshitij.so |
13 |
|
|
|
14 |
con = None
|
| 13907 |
kshitij.so |
15 |
cashBackMap = {}
|
| 13921 |
kshitij.so |
16 |
itemCashBackMap = {}
|
| 13572 |
kshitij.so |
17 |
|
| 14037 |
kshitij.so |
18 |
DataService.initialize(db_hostname="localhost")
|
|
|
19 |
mc = MemCache("127.0.0.1")
|
|
|
20 |
|
| 13572 |
kshitij.so |
21 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
22 |
global con
|
|
|
23 |
if con is None:
|
|
|
24 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
25 |
try:
|
|
|
26 |
con = pymongo.MongoClient(host, port)
|
|
|
27 |
except Exception, e:
|
|
|
28 |
print e
|
|
|
29 |
return None
|
|
|
30 |
return con
|
|
|
31 |
|
| 13907 |
kshitij.so |
32 |
def populateCashBack():
|
| 13921 |
kshitij.so |
33 |
print "Populating cashback"
|
| 13907 |
kshitij.so |
34 |
global cashBackMap
|
| 13921 |
kshitij.so |
35 |
global itemCashBackMap
|
|
|
36 |
cashBackMap = {}
|
|
|
37 |
itemCashBackMap = {}
|
| 13907 |
kshitij.so |
38 |
cashBack = list(get_mongo_connection().Catalog.CategoryCashBack.find())
|
|
|
39 |
for row in cashBack:
|
| 13970 |
kshitij.so |
40 |
temp_map = {}
|
|
|
41 |
temp_list = []
|
| 13907 |
kshitij.so |
42 |
if cashBackMap.has_key(row['source_id']):
|
|
|
43 |
arr = cashBackMap.get(row['source_id'])
|
|
|
44 |
for val in arr:
|
|
|
45 |
temp_list.append(val)
|
|
|
46 |
temp_map[row['category_id']] = row
|
|
|
47 |
temp_list.append(temp_map)
|
|
|
48 |
cashBackMap[row['source_id']] = temp_list
|
|
|
49 |
else:
|
|
|
50 |
temp_map[row['category_id']] = row
|
|
|
51 |
temp_list.append(temp_map)
|
|
|
52 |
cashBackMap[row['source_id']] = temp_list
|
| 13921 |
kshitij.so |
53 |
itemCashBack = list(get_mongo_connection().Catalog.ItemCashBack.find())
|
|
|
54 |
for row in itemCashBack:
|
|
|
55 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
56 |
itemCashBackMap[row['skuId']] = row
|
| 13907 |
kshitij.so |
57 |
|
| 13572 |
kshitij.so |
58 |
def addCategoryDiscount(data):
|
| 13970 |
kshitij.so |
59 |
collection = get_mongo_connection().Catalog.CategoryDiscount
|
| 13572 |
kshitij.so |
60 |
query = []
|
|
|
61 |
data['brand'] = data['brand'].strip().upper()
|
| 13970 |
kshitij.so |
62 |
data['discountType'] = data['discountType'].upper().strip()
|
| 13572 |
kshitij.so |
63 |
query.append({"brand":data['brand']})
|
|
|
64 |
query.append({"category_id":data['category_id']})
|
|
|
65 |
r = collection.find({"$and":query})
|
|
|
66 |
if r.count() > 0:
|
| 13639 |
kshitij.so |
67 |
return {0:"Brand & Category info already present."}
|
| 13572 |
kshitij.so |
68 |
else:
|
|
|
69 |
collection.insert(data)
|
| 13970 |
kshitij.so |
70 |
get_mongo_connection().Catalog.MasterData.update({'brand':data['brand'],'category_id':data['category_id']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 13639 |
kshitij.so |
71 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
72 |
|
| 13970 |
kshitij.so |
73 |
def updateCategoryDiscount(data,_id):
|
|
|
74 |
try:
|
|
|
75 |
collection = get_mongo_connection().Catalog.CategoryDiscount
|
|
|
76 |
collection.update({'_id':ObjectId(_id)},{"$set":{'min_discount':data['min_discount'],'max_discount':data['max_discount'],'discountType':data['discountType'].upper().strip()}},upsert=False, multi = False)
|
|
|
77 |
get_mongo_connection().Catalog.MasterData.update({'brand':data['brand'],'category_id':data['category_id']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
|
|
78 |
return {1:"Data updated successfully"}
|
|
|
79 |
except:
|
|
|
80 |
return {0:"Data not updated."}
|
|
|
81 |
|
|
|
82 |
|
| 13572 |
kshitij.so |
83 |
def getAllCategoryDiscount():
|
|
|
84 |
data = []
|
| 13970 |
kshitij.so |
85 |
collection = get_mongo_connection().Catalog.CategoryDiscount
|
| 13572 |
kshitij.so |
86 |
cursor = collection.find()
|
|
|
87 |
for val in cursor:
|
|
|
88 |
data.append(val)
|
|
|
89 |
return data
|
|
|
90 |
|
|
|
91 |
def addSchemeDetailsForSku(data):
|
| 13970 |
kshitij.so |
92 |
collection = get_mongo_connection().Catalog.SkuSchemeDetails
|
| 13572 |
kshitij.so |
93 |
data['addedOn'] = to_java_date(datetime.now())
|
|
|
94 |
collection.insert(data)
|
| 13639 |
kshitij.so |
95 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
96 |
|
|
|
97 |
def getAllSkuWiseSchemeDetails():
|
|
|
98 |
data = []
|
| 13970 |
kshitij.so |
99 |
collection = get_mongo_connection().Catalog.SkuSchemeDetails
|
| 13572 |
kshitij.so |
100 |
cursor = collection.find()
|
|
|
101 |
for val in cursor:
|
|
|
102 |
data.append(val)
|
|
|
103 |
return data
|
|
|
104 |
|
|
|
105 |
def addSkuDiscountInfo(data):
|
| 13970 |
kshitij.so |
106 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
| 13572 |
kshitij.so |
107 |
cursor = collection.find({"sku":data['sku']})
|
| 13642 |
kshitij.so |
108 |
if cursor.count() > 0:
|
| 13639 |
kshitij.so |
109 |
return {0:"Sku information already present."}
|
| 13572 |
kshitij.so |
110 |
else:
|
|
|
111 |
collection.insert(data)
|
| 13970 |
kshitij.so |
112 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
| 13639 |
kshitij.so |
113 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
114 |
|
| 13970 |
kshitij.so |
115 |
def getallSkuDiscountInfo(offset, limit):
|
| 13572 |
kshitij.so |
116 |
data = []
|
| 13970 |
kshitij.so |
117 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
|
|
118 |
cursor = collection.find().skip(offset).limit(limit)
|
| 13572 |
kshitij.so |
119 |
for val in cursor:
|
| 13970 |
kshitij.so |
120 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
121 |
if len(master) > 0:
|
|
|
122 |
val['brand'] = master['brand']
|
|
|
123 |
val['source_product_name'] = master['source_product_name']
|
|
|
124 |
val['skuBundleId'] = master['skuBundleId']
|
|
|
125 |
else:
|
|
|
126 |
val['brand'] = ""
|
|
|
127 |
val['source_product_name'] = ""
|
|
|
128 |
val['skuBundleId'] = ""
|
| 13572 |
kshitij.so |
129 |
data.append(val)
|
|
|
130 |
return data
|
|
|
131 |
|
| 13970 |
kshitij.so |
132 |
def updateSkuDiscount(data,_id):
|
|
|
133 |
try:
|
|
|
134 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
|
|
135 |
collection.update({'_id':ObjectId(_id)},{"$set":{'min_discount':data['min_discount'],'max_discount':data['max_discount'],'discountType':data['discountType'].upper().strip()}},upsert=False, multi = False)
|
|
|
136 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
137 |
return {1:"Data updated successfully"}
|
|
|
138 |
except:
|
|
|
139 |
return {0:"Data not updated."}
|
|
|
140 |
|
|
|
141 |
|
| 13572 |
kshitij.so |
142 |
def addExceptionalNlc(data):
|
| 13970 |
kshitij.so |
143 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
| 13572 |
kshitij.so |
144 |
cursor = collection.find({"sku":data['sku']})
|
| 13642 |
kshitij.so |
145 |
if cursor.count() > 0:
|
| 13639 |
kshitij.so |
146 |
return {0:"Sku information already present."}
|
| 13572 |
kshitij.so |
147 |
else:
|
|
|
148 |
collection.insert(data)
|
| 13970 |
kshitij.so |
149 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
| 13639 |
kshitij.so |
150 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
151 |
|
| 13970 |
kshitij.so |
152 |
def getAllExceptionlNlcItems(offset, limit):
|
| 13572 |
kshitij.so |
153 |
data = []
|
| 13970 |
kshitij.so |
154 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
| 13572 |
kshitij.so |
155 |
cursor = collection.find()
|
|
|
156 |
for val in cursor:
|
| 13970 |
kshitij.so |
157 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
158 |
if len(master) > 0:
|
|
|
159 |
val['brand'] = master['brand']
|
|
|
160 |
val['source_product_name'] = master['source_product_name']
|
|
|
161 |
val['skuBundleId'] = master['skuBundleId']
|
|
|
162 |
else:
|
|
|
163 |
val['brand'] = ""
|
|
|
164 |
val['source_product_name'] = ""
|
|
|
165 |
val['skuBundleId'] = ""
|
| 13572 |
kshitij.so |
166 |
data.append(val)
|
|
|
167 |
return data
|
|
|
168 |
|
| 14005 |
amit.gupta |
169 |
def getMerchantOrdersByUser(userId, page=1, window=50, searchMap={}):
|
|
|
170 |
if searchMap is None:
|
|
|
171 |
searchMap = {}
|
| 13603 |
amit.gupta |
172 |
if page==None:
|
|
|
173 |
page = 1
|
|
|
174 |
|
|
|
175 |
if window==None:
|
|
|
176 |
window = 50
|
|
|
177 |
result = {}
|
| 13582 |
amit.gupta |
178 |
skip = (page-1)*window
|
| 14005 |
amit.gupta |
179 |
|
|
|
180 |
searchMap['userId'] = userId
|
| 13603 |
amit.gupta |
181 |
collection = get_mongo_connection().Dtr.merchantOrder
|
| 14005 |
amit.gupta |
182 |
cursor = collection.find(searchMap).sort("_id",-1)
|
| 13603 |
amit.gupta |
183 |
total_count = cursor.count()
|
|
|
184 |
pages = total_count/window + (0 if total_count%window==0 else 1)
|
|
|
185 |
print "total_count", total_count
|
|
|
186 |
if total_count > skip:
|
| 13999 |
amit.gupta |
187 |
cursor = cursor.skip(skip).limit(window)
|
| 13603 |
amit.gupta |
188 |
orders = []
|
|
|
189 |
for order in cursor:
|
| 14002 |
amit.gupta |
190 |
del(order["_id"])
|
|
|
191 |
orders.append(order)
|
| 13603 |
amit.gupta |
192 |
result['data'] = orders
|
|
|
193 |
result['window'] = window
|
|
|
194 |
result['totalCount'] = total_count
|
|
|
195 |
result['currCount'] = cursor.count()
|
|
|
196 |
result['totalPages'] = pages
|
|
|
197 |
result['currPage'] = page
|
|
|
198 |
return result
|
|
|
199 |
else:
|
|
|
200 |
return result
|
| 13630 |
kshitij.so |
201 |
|
| 13927 |
amit.gupta |
202 |
def getRefunds(userId, page=1, window=10):
|
|
|
203 |
if page==None:
|
|
|
204 |
page = 1
|
|
|
205 |
|
|
|
206 |
if window==None:
|
| 13995 |
amit.gupta |
207 |
window = 10
|
| 13927 |
amit.gupta |
208 |
result = {}
|
|
|
209 |
skip = (page-1)*window
|
|
|
210 |
collection = get_mongo_connection().Dtr.refund
|
|
|
211 |
cursor = collection.find({"userId":userId})
|
|
|
212 |
total_count = cursor.count()
|
|
|
213 |
pages = total_count/window + (0 if total_count%window==0 else 1)
|
|
|
214 |
print "total_count", total_count
|
|
|
215 |
if total_count > skip:
|
| 13991 |
amit.gupta |
216 |
cursor = cursor.skip(skip).limit(window).sort([('batchId',-1)])
|
| 13927 |
amit.gupta |
217 |
refunds = []
|
|
|
218 |
for refund in cursor:
|
|
|
219 |
del(refund["_id"])
|
|
|
220 |
refunds.append(refund)
|
|
|
221 |
result['data'] = refunds
|
|
|
222 |
result['window'] = window
|
|
|
223 |
result['totalCount'] = total_count
|
|
|
224 |
result['currCount'] = cursor.count()
|
|
|
225 |
result['totalPages'] = pages
|
|
|
226 |
result['currPage'] = page
|
|
|
227 |
return result
|
|
|
228 |
else:
|
|
|
229 |
return result
|
|
|
230 |
|
|
|
231 |
def getPendingRefunds(userId):
|
| 13991 |
amit.gupta |
232 |
print type(userId)
|
| 13927 |
amit.gupta |
233 |
result = get_mongo_connection().Dtr.merchantOrder\
|
|
|
234 |
.aggregate([
|
|
|
235 |
{'$match':{'subOrders.cashBackStatus':Store.CB_APPROVED, 'userId':userId}},
|
|
|
236 |
{'$unwind':"$subOrders"},
|
|
|
237 |
{
|
|
|
238 |
'$group':{
|
|
|
239 |
'_id':None,
|
|
|
240 |
'amount': { '$sum':'$subOrders.cashBackAmount'},
|
|
|
241 |
}
|
|
|
242 |
}
|
| 13987 |
amit.gupta |
243 |
])['result']
|
|
|
244 |
|
|
|
245 |
if len(result)>0:
|
|
|
246 |
result = result[0]
|
|
|
247 |
result.pop("_id")
|
|
|
248 |
else:
|
|
|
249 |
result={}
|
|
|
250 |
result['amount'] = 0.0
|
| 13988 |
amit.gupta |
251 |
result['nextCredit'] = datetime.strftime(next_weekday(datetime.now(), 0),"%Y-%m-%d %H:%M:%S")
|
| 13927 |
amit.gupta |
252 |
return result
|
| 14037 |
kshitij.so |
253 |
|
|
|
254 |
def __populateCache(userId):
|
|
|
255 |
print "Populating memcache for userId",userId
|
|
|
256 |
outer_query = []
|
|
|
257 |
outer_query.append({"showDeal":1})
|
|
|
258 |
query = {}
|
|
|
259 |
query['$gt'] = 0
|
|
|
260 |
outer_query.append({'totalPoints':query})
|
|
|
261 |
brandPrefMap = {}
|
|
|
262 |
pricePrefMap = {}
|
|
|
263 |
actionsMap = {}
|
|
|
264 |
brand_p = session.query(price_preferences).filter_by(user_id=userId).all()
|
|
|
265 |
for x in brand_p:
|
|
|
266 |
pricePrefMap[x.category_id] = [x.min_price,x.max_price]
|
|
|
267 |
for x in session.query(brand_preferences).filter_by(user_id=userId).all():
|
|
|
268 |
temp_map = {}
|
|
|
269 |
if brandPrefMap.has_key((x.brand).strip().upper()):
|
|
|
270 |
val = brandPrefMap.get((x.brand).strip().upper())
|
|
|
271 |
temp_map[x.category_id] = 1 if x.status == 'show' else 0
|
|
|
272 |
val.append(temp_map)
|
|
|
273 |
else:
|
|
|
274 |
temp = []
|
|
|
275 |
temp_map[x.category_id] = 1 if x.status == 'show' else 0
|
|
|
276 |
temp.append(temp_map)
|
|
|
277 |
brandPrefMap[(x.brand).strip().upper()] = temp
|
|
|
278 |
|
|
|
279 |
for x in session.query(user_actions).filter_by(user_id=userId).all():
|
|
|
280 |
actionsMap[x.store_product_id] = 1 if x.action == 'like' else 0
|
|
|
281 |
all_deals = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query},{'_id':1,'category_id':1,'brand':1,'totalPoints':1,'bestSellerPoints':1,'nlcPoints':1,'rank':1,'available_price':1}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
|
|
|
282 |
all_category_deals = []
|
|
|
283 |
mobile_deals = []
|
|
|
284 |
tablet_deals = []
|
|
|
285 |
for deal in all_deals:
|
|
|
286 |
if actionsMap.get(deal['_id']) == 0:
|
|
|
287 |
fav_weight =.25
|
|
|
288 |
elif actionsMap.get(deal['_id']) == 1:
|
|
|
289 |
fav_weight = 1.5
|
|
|
290 |
else:
|
|
|
291 |
fav_weight = 1
|
|
|
292 |
|
|
|
293 |
if brandPrefMap.get(deal['brand'].strip().upper()) is not None:
|
|
|
294 |
brand_weight = 1
|
|
|
295 |
for brandInfo in brandPrefMap.get(deal['brand'].strip().upper()):
|
|
|
296 |
if brandInfo.get(deal['category_id']) is not None:
|
|
|
297 |
if brandInfo.get(deal['category_id']) == 1:
|
|
|
298 |
brand_weight = 1.5
|
|
|
299 |
else:
|
|
|
300 |
brand_weight = 1
|
|
|
301 |
|
|
|
302 |
if pricePrefMap.get(deal['category_id']) is not None:
|
|
|
303 |
|
|
|
304 |
if deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1]:
|
|
|
305 |
asp_weight = 1.5
|
|
|
306 |
elif deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] - 0.5 * pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1] + 0.5 * pricePrefMap.get(deal['category_id'])[1]:
|
|
|
307 |
asp_weight = 1.2
|
|
|
308 |
else:
|
|
|
309 |
asp_weight = 1
|
|
|
310 |
else:
|
|
|
311 |
asp_weight = 1
|
|
|
312 |
|
|
|
313 |
persPoints = deal['totalPoints'] * fav_weight * brand_weight * asp_weight
|
|
|
314 |
deal['persPoints'] = persPoints
|
|
|
315 |
|
|
|
316 |
if deal['category_id'] ==3:
|
|
|
317 |
mobile_deals.append(deal)
|
|
|
318 |
elif deal['category_id'] ==5:
|
|
|
319 |
tablet_deals.append(deal)
|
|
|
320 |
else:
|
|
|
321 |
continue
|
|
|
322 |
all_category_deals.append(deal)
|
|
|
323 |
|
|
|
324 |
|
|
|
325 |
mem_cache_val = {3:mobile_deals, 5:tablet_deals, 0:all_deals}
|
|
|
326 |
mc.set(str(userId), mem_cache_val)
|
|
|
327 |
|
|
|
328 |
|
|
|
329 |
|
|
|
330 |
def getNewDeals(userId, category_id, offset, limit, sort, direction):
|
| 13922 |
kshitij.so |
331 |
if not bool(cashBackMap):
|
| 13921 |
kshitij.so |
332 |
populateCashBack()
|
| 14037 |
kshitij.so |
333 |
|
| 13771 |
kshitij.so |
334 |
rank = 1
|
| 14037 |
kshitij.so |
335 |
dealsMap = {}
|
|
|
336 |
user_specific_deals = mc.get(str(userId))
|
|
|
337 |
if user_specific_deals is None:
|
|
|
338 |
__populateCache(userId)
|
|
|
339 |
user_specific_deals = mc.get(str(userId))
|
|
|
340 |
category_specific_deals = user_specific_deals.get(category_id)
|
|
|
341 |
|
|
|
342 |
if sort is None or direction is None:
|
|
|
343 |
sorted_deals = sorted(category_specific_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
|
|
|
344 |
else:
|
|
|
345 |
if sort == "bestSellerPoints":
|
|
|
346 |
sorted_deals = sorted(category_specific_deals, key = lambda x: (x['bestSellerPoints'], x['rank'], x['nlcPoints']),reverse=True)
|
|
|
347 |
else:
|
|
|
348 |
if direction == -1:
|
|
|
349 |
rev = True
|
|
|
350 |
else:
|
|
|
351 |
rev = False
|
|
|
352 |
sorted_deals = sorted(category_specific_deals, key = lambda x: (x['available_price']),reverse=rev)
|
|
|
353 |
|
|
|
354 |
for d in sorted_deals[offset:offset+limit]:
|
|
|
355 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
|
|
|
356 |
if not dealsMap.has_key(item[0]['identifier']):
|
|
|
357 |
item[0]['dealRank'] = rank
|
|
|
358 |
item[0]['persPoints'] = d['persPoints']
|
|
|
359 |
try:
|
|
|
360 |
cashBack = __getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
|
|
|
361 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
|
|
362 |
item[0]['cash_back_type'] = 0
|
|
|
363 |
item[0]['cash_back'] = 0
|
|
|
364 |
else:
|
|
|
365 |
item[0]['cash_back_type'] = cashBack['cash_back_type']
|
|
|
366 |
item[0]['cash_back'] = cashBack['cash_back']
|
|
|
367 |
except:
|
|
|
368 |
print "Error in adding cashback to deals"
|
|
|
369 |
item[0]['cash_back_type'] = 0
|
|
|
370 |
item[0]['cash_back'] = 0
|
|
|
371 |
dealsMap[item[0]['identifier']] = item[0]
|
|
|
372 |
|
|
|
373 |
rank +=1
|
|
|
374 |
return sorted(dealsMap.values(), key=itemgetter('dealRank'))
|
|
|
375 |
|
|
|
376 |
|
|
|
377 |
def getDeals(userId, category_id, offset, limit, sort, direction):
|
|
|
378 |
if not bool(cashBackMap):
|
|
|
379 |
populateCashBack()
|
|
|
380 |
rank = 1
|
| 13771 |
kshitij.so |
381 |
deals = {}
|
| 13910 |
kshitij.so |
382 |
outer_query = []
|
|
|
383 |
outer_query.append({"showDeal":1})
|
|
|
384 |
query = {}
|
|
|
385 |
query['$gt'] = 0
|
|
|
386 |
outer_query.append({'totalPoints':query})
|
|
|
387 |
if category_id in (3,5):
|
|
|
388 |
outer_query.append({'category_id':category_id})
|
| 13803 |
kshitij.so |
389 |
if sort is None or direction is None:
|
|
|
390 |
direct = -1
|
| 13910 |
kshitij.so |
391 |
print outer_query
|
|
|
392 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('totalPoints',direct),('bestSellerPoints',direct),('nlcPoints',direct),('rank',direct)]).skip(offset).limit(limit))
|
| 13795 |
kshitij.so |
393 |
else:
|
| 13910 |
kshitij.so |
394 |
print outer_query
|
| 13803 |
kshitij.so |
395 |
direct = direction
|
| 13910 |
kshitij.so |
396 |
if sort == "bestSellerPoints":
|
|
|
397 |
print "yes,sorting by bestSellerPoints"
|
|
|
398 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('bestSellerPoints',direct),('rank',direct),('nlcPoints',direct)]).skip(offset).limit(limit))
|
|
|
399 |
else:
|
|
|
400 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([(sort,direct)]).skip(offset).limit(limit))
|
| 13771 |
kshitij.so |
401 |
for d in data:
|
|
|
402 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
|
|
|
403 |
if not deals.has_key(item[0]['identifier']):
|
| 13921 |
kshitij.so |
404 |
item[0]['dealRank'] = rank
|
|
|
405 |
try:
|
|
|
406 |
cashBack = __getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
|
|
|
407 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 13928 |
kshitij.so |
408 |
item[0]['cash_back_type'] = 0
|
|
|
409 |
item[0]['cash_back'] = 0
|
| 13921 |
kshitij.so |
410 |
else:
|
| 13928 |
kshitij.so |
411 |
item[0]['cash_back_type'] = cashBack['cash_back_type']
|
|
|
412 |
item[0]['cash_back'] = cashBack['cash_back']
|
| 13921 |
kshitij.so |
413 |
except:
|
|
|
414 |
print "Error in adding cashback to deals"
|
| 13928 |
kshitij.so |
415 |
item[0]['cash_back_type'] = 0
|
|
|
416 |
item[0]['cash_back'] = 0
|
| 13771 |
kshitij.so |
417 |
deals[item[0]['identifier']] = item[0]
|
| 13921 |
kshitij.so |
418 |
|
| 13771 |
kshitij.so |
419 |
rank +=1
|
| 13785 |
kshitij.so |
420 |
return sorted(deals.values(), key=itemgetter('dealRank'))
|
|
|
421 |
|
|
|
422 |
def getItem(skuId):
|
| 13795 |
kshitij.so |
423 |
try:
|
|
|
424 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
|
| 13785 |
kshitij.so |
425 |
return skuData
|
| 13795 |
kshitij.so |
426 |
except:
|
|
|
427 |
return [{}]
|
| 13836 |
kshitij.so |
428 |
|
| 13921 |
kshitij.so |
429 |
def __getCashBack(skuId, source_id, category_id):
|
|
|
430 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
431 |
if itemCashBack is not None:
|
|
|
432 |
return itemCashBack
|
|
|
433 |
|
|
|
434 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
435 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
436 |
for cashBack in sourceCashBack:
|
|
|
437 |
if cashBack.get(category_id) is None:
|
|
|
438 |
continue
|
|
|
439 |
else:
|
|
|
440 |
return cashBack.get(category_id)
|
|
|
441 |
else:
|
|
|
442 |
return {}
|
|
|
443 |
|
| 13836 |
kshitij.so |
444 |
def getCashBackDetails(identifier, source_id):
|
| 13922 |
kshitij.so |
445 |
if not bool(cashBackMap):
|
| 13921 |
kshitij.so |
446 |
populateCashBack()
|
|
|
447 |
|
| 13836 |
kshitij.so |
448 |
"""Need to add item level cashback, no data available right now."""
|
| 13771 |
kshitij.so |
449 |
|
| 13921 |
kshitij.so |
450 |
if source_id in (1,2,4,5):
|
| 13839 |
kshitij.so |
451 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
|
| 13840 |
kshitij.so |
452 |
elif source_id == 3:
|
| 13839 |
kshitij.so |
453 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
|
| 13840 |
kshitij.so |
454 |
else:
|
|
|
455 |
return {}
|
| 13836 |
kshitij.so |
456 |
if len(skuData) > 0:
|
| 13921 |
kshitij.so |
457 |
|
|
|
458 |
itemCashBack = itemCashBackMap.get(skuData[0]['_id'])
|
|
|
459 |
if itemCashBack is not None:
|
|
|
460 |
return itemCashBack
|
|
|
461 |
|
|
|
462 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
463 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
464 |
for cashBack in sourceCashBack:
|
|
|
465 |
if cashBack.get(skuData[0]['category_id']) is None:
|
|
|
466 |
continue
|
|
|
467 |
else:
|
|
|
468 |
return cashBack.get(skuData[0]['category_id'])
|
| 13836 |
kshitij.so |
469 |
else:
|
|
|
470 |
return {}
|
|
|
471 |
else:
|
|
|
472 |
return {}
|
| 13986 |
amit.gupta |
473 |
|
|
|
474 |
|
|
|
475 |
def next_weekday(d, weekday):
|
|
|
476 |
days_ahead = weekday - d.weekday()
|
|
|
477 |
if days_ahead <= 0: # Target day already happened this week
|
|
|
478 |
days_ahead += 7
|
|
|
479 |
return d + timedelta(days_ahead)
|
|
|
480 |
|
| 13771 |
kshitij.so |
481 |
|
| 13970 |
kshitij.so |
482 |
def getAllDealerPrices(offset, limit):
|
|
|
483 |
data = []
|
|
|
484 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
485 |
cursor = collection.find().skip(offset).limit(limit)
|
|
|
486 |
for val in cursor:
|
|
|
487 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
488 |
if len(master) > 0:
|
|
|
489 |
val['brand'] = master['brand']
|
|
|
490 |
val['source_product_name'] = master['source_product_name']
|
|
|
491 |
val['skuBundleId'] = master['skuBundleId']
|
|
|
492 |
else:
|
|
|
493 |
val['brand'] = ""
|
|
|
494 |
val['source_product_name'] = ""
|
|
|
495 |
val['skuBundleId'] = ""
|
|
|
496 |
data.append(val)
|
|
|
497 |
return data
|
|
|
498 |
|
|
|
499 |
def addSkuDealerPrice(data):
|
|
|
500 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
501 |
cursor = collection.find({"sku":data['sku']})
|
|
|
502 |
if cursor.count() > 0:
|
|
|
503 |
return {0:"Sku information already present."}
|
|
|
504 |
else:
|
|
|
505 |
collection.insert(data)
|
|
|
506 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
507 |
return {1:"Data added successfully"}
|
|
|
508 |
|
|
|
509 |
def updateSkuDealerPrice(data, _id):
|
|
|
510 |
try:
|
|
|
511 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
512 |
collection.update({'_id':ObjectId(_id)},{"$set":{'dp':data['dp']}},upsert=False, multi = False)
|
|
|
513 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
514 |
return {1:"Data updated successfully"}
|
|
|
515 |
except:
|
|
|
516 |
return {0:"Data not updated."}
|
|
|
517 |
|
|
|
518 |
def updateExceptionalNlc(data, _id):
|
|
|
519 |
try:
|
|
|
520 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
|
|
521 |
collection.update({'_id':ObjectId(_id)},{"$set":{'maxNlc':data['maxNlc'], 'minNlc':data['minNlc'], 'overrideNlc':data['overrideNlc']}},upsert=False, multi = False)
|
|
|
522 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
523 |
return {1:"Data updated successfully"}
|
|
|
524 |
except:
|
|
|
525 |
return {0:"Data not updated."}
|
|
|
526 |
|
|
|
527 |
|
| 13572 |
kshitij.so |
528 |
def main():
|
| 14037 |
kshitij.so |
529 |
x = getNewDeals(1, 0, 0, 30, None, None)
|
|
|
530 |
for i in x:
|
|
|
531 |
print i['_id'],
|
|
|
532 |
print '\t',
|
|
|
533 |
print i['persPoints']
|
| 13811 |
kshitij.so |
534 |
|
| 13921 |
kshitij.so |
535 |
|
| 13572 |
kshitij.so |
536 |
if __name__=='__main__':
|
| 13932 |
amit.gupta |
537 |
main()
|