| 14305 |
amit.gupta |
1 |
from bson.objectid import ObjectId
|
|
|
2 |
from datetime import datetime, timedelta
|
|
|
3 |
from dtr.config import PythonPropertyReader
|
| 13927 |
amit.gupta |
4 |
from dtr.main import Store
|
| 14531 |
kshitij.so |
5 |
from dtr.dao import FeaturedDeals
|
| 14037 |
kshitij.so |
6 |
from dtr.storage import DataService
|
|
|
7 |
from dtr.storage.DataService import price_preferences, brand_preferences, \
|
| 14305 |
amit.gupta |
8 |
user_actions
|
| 14037 |
kshitij.so |
9 |
from dtr.storage.MemCache import MemCache
|
| 14305 |
amit.gupta |
10 |
from dtr.utils.utils import to_java_date
|
|
|
11 |
from elixir import *
|
|
|
12 |
from operator import itemgetter
|
|
|
13 |
import pymongo
|
| 14322 |
kshitij.so |
14 |
import re
|
| 13572 |
kshitij.so |
15 |
|
|
|
16 |
con = None
|
| 13907 |
kshitij.so |
17 |
cashBackMap = {}
|
| 13921 |
kshitij.so |
18 |
itemCashBackMap = {}
|
| 13572 |
kshitij.so |
19 |
|
| 14037 |
kshitij.so |
20 |
DataService.initialize(db_hostname="localhost")
|
|
|
21 |
mc = MemCache("127.0.0.1")
|
|
|
22 |
|
| 14482 |
kshitij.so |
23 |
SOURCE_MAP = {1:'AMAZON',2:'FLIPKART',3:'SNAPDEAL',4:'SAHOLIC'}
|
|
|
24 |
|
| 13572 |
kshitij.so |
25 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
26 |
global con
|
|
|
27 |
if con is None:
|
|
|
28 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
29 |
try:
|
|
|
30 |
con = pymongo.MongoClient(host, port)
|
|
|
31 |
except Exception, e:
|
|
|
32 |
print e
|
|
|
33 |
return None
|
|
|
34 |
return con
|
|
|
35 |
|
| 13907 |
kshitij.so |
36 |
def populateCashBack():
|
| 13921 |
kshitij.so |
37 |
print "Populating cashback"
|
| 13907 |
kshitij.so |
38 |
global cashBackMap
|
| 13921 |
kshitij.so |
39 |
global itemCashBackMap
|
|
|
40 |
cashBackMap = {}
|
|
|
41 |
itemCashBackMap = {}
|
| 13907 |
kshitij.so |
42 |
cashBack = list(get_mongo_connection().Catalog.CategoryCashBack.find())
|
|
|
43 |
for row in cashBack:
|
| 13970 |
kshitij.so |
44 |
temp_map = {}
|
|
|
45 |
temp_list = []
|
| 13907 |
kshitij.so |
46 |
if cashBackMap.has_key(row['source_id']):
|
|
|
47 |
arr = cashBackMap.get(row['source_id'])
|
|
|
48 |
for val in arr:
|
|
|
49 |
temp_list.append(val)
|
|
|
50 |
temp_map[row['category_id']] = row
|
|
|
51 |
temp_list.append(temp_map)
|
|
|
52 |
cashBackMap[row['source_id']] = temp_list
|
|
|
53 |
else:
|
|
|
54 |
temp_map[row['category_id']] = row
|
|
|
55 |
temp_list.append(temp_map)
|
|
|
56 |
cashBackMap[row['source_id']] = temp_list
|
| 13921 |
kshitij.so |
57 |
itemCashBack = list(get_mongo_connection().Catalog.ItemCashBack.find())
|
|
|
58 |
for row in itemCashBack:
|
|
|
59 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
60 |
itemCashBackMap[row['skuId']] = row
|
| 13907 |
kshitij.so |
61 |
|
| 13572 |
kshitij.so |
62 |
def addCategoryDiscount(data):
|
| 13970 |
kshitij.so |
63 |
collection = get_mongo_connection().Catalog.CategoryDiscount
|
| 13572 |
kshitij.so |
64 |
query = []
|
|
|
65 |
data['brand'] = data['brand'].strip().upper()
|
| 13970 |
kshitij.so |
66 |
data['discountType'] = data['discountType'].upper().strip()
|
| 13572 |
kshitij.so |
67 |
query.append({"brand":data['brand']})
|
|
|
68 |
query.append({"category_id":data['category_id']})
|
|
|
69 |
r = collection.find({"$and":query})
|
|
|
70 |
if r.count() > 0:
|
| 13639 |
kshitij.so |
71 |
return {0:"Brand & Category info already present."}
|
| 13572 |
kshitij.so |
72 |
else:
|
|
|
73 |
collection.insert(data)
|
| 13970 |
kshitij.so |
74 |
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 |
75 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
76 |
|
| 13970 |
kshitij.so |
77 |
def updateCategoryDiscount(data,_id):
|
|
|
78 |
try:
|
|
|
79 |
collection = get_mongo_connection().Catalog.CategoryDiscount
|
|
|
80 |
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)
|
|
|
81 |
get_mongo_connection().Catalog.MasterData.update({'brand':data['brand'],'category_id':data['category_id']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
|
|
82 |
return {1:"Data updated successfully"}
|
|
|
83 |
except:
|
|
|
84 |
return {0:"Data not updated."}
|
|
|
85 |
|
|
|
86 |
|
| 13572 |
kshitij.so |
87 |
def getAllCategoryDiscount():
|
|
|
88 |
data = []
|
| 13970 |
kshitij.so |
89 |
collection = get_mongo_connection().Catalog.CategoryDiscount
|
| 13572 |
kshitij.so |
90 |
cursor = collection.find()
|
|
|
91 |
for val in cursor:
|
|
|
92 |
data.append(val)
|
|
|
93 |
return data
|
|
|
94 |
|
| 14553 |
kshitij.so |
95 |
def __getBundledSkusfromSku(sku):
|
|
|
96 |
masterData = get_mongo_connection().Catalog.MasterData.find_one({"_id":sku},{"skuBundleId":1})
|
|
|
97 |
if masterData is not None:
|
|
|
98 |
return list(get_mongo_connection().Catalog.MasterData.find({"skuBundleId":masterData.get('skuBundleId')},{'_id':1,'skuBundleId':1}))
|
|
|
99 |
else:
|
|
|
100 |
return []
|
| 13572 |
kshitij.so |
101 |
|
| 14553 |
kshitij.so |
102 |
def addSchemeDetailsForSku(data, multi):
|
|
|
103 |
if multi ==1:
|
|
|
104 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
105 |
for sku in skuIds:
|
|
|
106 |
collection = get_mongo_connection().Catalog.SkuSchemeDetails
|
|
|
107 |
data['sku'] = sku.get('_id')
|
|
|
108 |
data['addedOn'] = to_java_date(datetime.now())
|
| 14558 |
kshitij.so |
109 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
110 |
collection.insert(data)
|
|
|
111 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 14559 |
kshitij.so |
112 |
return {1:"Data added successfully"}
|
| 14553 |
kshitij.so |
113 |
else:
|
|
|
114 |
collection = get_mongo_connection().Catalog.SkuSchemeDetails
|
|
|
115 |
data['addedOn'] = to_java_date(datetime.now())
|
|
|
116 |
collection.insert(data)
|
|
|
117 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
118 |
return {1:"Data added successfully"}
|
|
|
119 |
|
| 14069 |
kshitij.so |
120 |
def getAllSkuWiseSchemeDetails(offset, limit):
|
| 13572 |
kshitij.so |
121 |
data = []
|
| 13970 |
kshitij.so |
122 |
collection = get_mongo_connection().Catalog.SkuSchemeDetails
|
| 14071 |
kshitij.so |
123 |
cursor = collection.find().skip(offset).limit(limit)
|
| 13572 |
kshitij.so |
124 |
for val in cursor:
|
| 14069 |
kshitij.so |
125 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
126 |
if len(master) > 0:
|
|
|
127 |
val['brand'] = master[0]['brand']
|
|
|
128 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
129 |
val['skuBundleId'] = master[0]['skuBundleId']
|
|
|
130 |
else:
|
|
|
131 |
val['brand'] = ""
|
|
|
132 |
val['source_product_name'] = ""
|
|
|
133 |
val['skuBundleId'] = ""
|
| 13572 |
kshitij.so |
134 |
data.append(val)
|
|
|
135 |
return data
|
|
|
136 |
|
| 14553 |
kshitij.so |
137 |
def addSkuDiscountInfo(data, multi):
|
|
|
138 |
if multi != 1:
|
|
|
139 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
|
|
140 |
cursor = collection.find({"sku":data['sku']})
|
|
|
141 |
if cursor.count() > 0:
|
|
|
142 |
return {0:"Sku information already present."}
|
|
|
143 |
else:
|
|
|
144 |
collection.insert(data)
|
|
|
145 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
146 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
147 |
else:
|
| 14553 |
kshitij.so |
148 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
149 |
for sku in skuIds:
|
|
|
150 |
data['sku'] = sku.get('_id')
|
|
|
151 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
|
|
152 |
cursor = collection.find({"sku":data['sku']})
|
|
|
153 |
if cursor.count() > 0:
|
|
|
154 |
continue
|
|
|
155 |
else:
|
| 14558 |
kshitij.so |
156 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
157 |
collection.insert(data)
|
|
|
158 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 13639 |
kshitij.so |
159 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
160 |
|
| 13970 |
kshitij.so |
161 |
def getallSkuDiscountInfo(offset, limit):
|
| 13572 |
kshitij.so |
162 |
data = []
|
| 13970 |
kshitij.so |
163 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
|
|
164 |
cursor = collection.find().skip(offset).limit(limit)
|
| 13572 |
kshitij.so |
165 |
for val in cursor:
|
| 13970 |
kshitij.so |
166 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
167 |
if len(master) > 0:
|
| 14069 |
kshitij.so |
168 |
val['brand'] = master[0]['brand']
|
|
|
169 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
170 |
val['skuBundleId'] = master[0]['skuBundleId']
|
| 13970 |
kshitij.so |
171 |
else:
|
|
|
172 |
val['brand'] = ""
|
|
|
173 |
val['source_product_name'] = ""
|
|
|
174 |
val['skuBundleId'] = ""
|
| 13572 |
kshitij.so |
175 |
data.append(val)
|
|
|
176 |
return data
|
|
|
177 |
|
| 13970 |
kshitij.so |
178 |
def updateSkuDiscount(data,_id):
|
|
|
179 |
try:
|
|
|
180 |
collection = get_mongo_connection().Catalog.SkuDiscountInfo
|
|
|
181 |
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)
|
|
|
182 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
183 |
return {1:"Data updated successfully"}
|
|
|
184 |
except:
|
|
|
185 |
return {0:"Data not updated."}
|
|
|
186 |
|
|
|
187 |
|
| 14553 |
kshitij.so |
188 |
def addExceptionalNlc(data, multi):
|
|
|
189 |
if multi!=1:
|
|
|
190 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
|
|
191 |
cursor = collection.find({"sku":data['sku']})
|
|
|
192 |
if cursor.count() > 0:
|
|
|
193 |
return {0:"Sku information already present."}
|
|
|
194 |
else:
|
|
|
195 |
collection.insert(data)
|
|
|
196 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
197 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
198 |
else:
|
| 14553 |
kshitij.so |
199 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
200 |
for sku in skuIds:
|
|
|
201 |
data['sku'] = sku.get('_id')
|
|
|
202 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
|
|
203 |
cursor = collection.find({"sku":data['sku']})
|
|
|
204 |
if cursor.count() > 0:
|
|
|
205 |
continue
|
|
|
206 |
else:
|
| 14558 |
kshitij.so |
207 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
208 |
collection.insert(data)
|
|
|
209 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 13639 |
kshitij.so |
210 |
return {1:"Data added successfully"}
|
| 14553 |
kshitij.so |
211 |
|
|
|
212 |
|
| 13572 |
kshitij.so |
213 |
|
| 13970 |
kshitij.so |
214 |
def getAllExceptionlNlcItems(offset, limit):
|
| 13572 |
kshitij.so |
215 |
data = []
|
| 13970 |
kshitij.so |
216 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
| 14071 |
kshitij.so |
217 |
cursor = collection.find().skip(offset).limit(limit)
|
| 13572 |
kshitij.so |
218 |
for val in cursor:
|
| 13970 |
kshitij.so |
219 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
220 |
if len(master) > 0:
|
| 14069 |
kshitij.so |
221 |
val['brand'] = master[0]['brand']
|
|
|
222 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
223 |
val['skuBundleId'] = master[0]['skuBundleId']
|
| 13970 |
kshitij.so |
224 |
else:
|
|
|
225 |
val['brand'] = ""
|
|
|
226 |
val['source_product_name'] = ""
|
|
|
227 |
val['skuBundleId'] = ""
|
| 13572 |
kshitij.so |
228 |
data.append(val)
|
|
|
229 |
return data
|
|
|
230 |
|
| 14005 |
amit.gupta |
231 |
def getMerchantOrdersByUser(userId, page=1, window=50, searchMap={}):
|
|
|
232 |
if searchMap is None:
|
|
|
233 |
searchMap = {}
|
| 13603 |
amit.gupta |
234 |
if page==None:
|
|
|
235 |
page = 1
|
|
|
236 |
|
|
|
237 |
if window==None:
|
|
|
238 |
window = 50
|
|
|
239 |
result = {}
|
| 13582 |
amit.gupta |
240 |
skip = (page-1)*window
|
| 14005 |
amit.gupta |
241 |
|
| 14353 |
amit.gupta |
242 |
if userId is not None:
|
|
|
243 |
searchMap['userId'] = userId
|
| 13603 |
amit.gupta |
244 |
collection = get_mongo_connection().Dtr.merchantOrder
|
| 14005 |
amit.gupta |
245 |
cursor = collection.find(searchMap).sort("_id",-1)
|
| 13603 |
amit.gupta |
246 |
total_count = cursor.count()
|
|
|
247 |
pages = total_count/window + (0 if total_count%window==0 else 1)
|
|
|
248 |
print "total_count", total_count
|
|
|
249 |
if total_count > skip:
|
| 13999 |
amit.gupta |
250 |
cursor = cursor.skip(skip).limit(window)
|
| 13603 |
amit.gupta |
251 |
orders = []
|
|
|
252 |
for order in cursor:
|
| 14002 |
amit.gupta |
253 |
del(order["_id"])
|
|
|
254 |
orders.append(order)
|
| 13603 |
amit.gupta |
255 |
result['data'] = orders
|
|
|
256 |
result['window'] = window
|
|
|
257 |
result['totalCount'] = total_count
|
|
|
258 |
result['currCount'] = cursor.count()
|
|
|
259 |
result['totalPages'] = pages
|
|
|
260 |
result['currPage'] = page
|
|
|
261 |
return result
|
|
|
262 |
else:
|
|
|
263 |
return result
|
| 13630 |
kshitij.so |
264 |
|
| 13927 |
amit.gupta |
265 |
def getRefunds(userId, page=1, window=10):
|
|
|
266 |
if page==None:
|
|
|
267 |
page = 1
|
|
|
268 |
|
|
|
269 |
if window==None:
|
| 13995 |
amit.gupta |
270 |
window = 10
|
| 13927 |
amit.gupta |
271 |
result = {}
|
|
|
272 |
skip = (page-1)*window
|
|
|
273 |
collection = get_mongo_connection().Dtr.refund
|
|
|
274 |
cursor = collection.find({"userId":userId})
|
|
|
275 |
total_count = cursor.count()
|
|
|
276 |
pages = total_count/window + (0 if total_count%window==0 else 1)
|
|
|
277 |
print "total_count", total_count
|
|
|
278 |
if total_count > skip:
|
| 13991 |
amit.gupta |
279 |
cursor = cursor.skip(skip).limit(window).sort([('batchId',-1)])
|
| 13927 |
amit.gupta |
280 |
refunds = []
|
|
|
281 |
for refund in cursor:
|
|
|
282 |
del(refund["_id"])
|
|
|
283 |
refunds.append(refund)
|
|
|
284 |
result['data'] = refunds
|
|
|
285 |
result['window'] = window
|
|
|
286 |
result['totalCount'] = total_count
|
|
|
287 |
result['currCount'] = cursor.count()
|
|
|
288 |
result['totalPages'] = pages
|
|
|
289 |
result['currPage'] = page
|
|
|
290 |
return result
|
|
|
291 |
else:
|
|
|
292 |
return result
|
|
|
293 |
|
|
|
294 |
def getPendingRefunds(userId):
|
| 13991 |
amit.gupta |
295 |
print type(userId)
|
| 13927 |
amit.gupta |
296 |
result = get_mongo_connection().Dtr.merchantOrder\
|
|
|
297 |
.aggregate([
|
|
|
298 |
{'$match':{'subOrders.cashBackStatus':Store.CB_APPROVED, 'userId':userId}},
|
|
|
299 |
{'$unwind':"$subOrders"},
|
|
|
300 |
{
|
|
|
301 |
'$group':{
|
|
|
302 |
'_id':None,
|
|
|
303 |
'amount': { '$sum':'$subOrders.cashBackAmount'},
|
|
|
304 |
}
|
|
|
305 |
}
|
| 13987 |
amit.gupta |
306 |
])['result']
|
|
|
307 |
|
|
|
308 |
if len(result)>0:
|
|
|
309 |
result = result[0]
|
|
|
310 |
result.pop("_id")
|
|
|
311 |
else:
|
|
|
312 |
result={}
|
|
|
313 |
result['amount'] = 0.0
|
| 14305 |
amit.gupta |
314 |
result['nextCredit'] = datetime.strftime(next_weekday(datetime.now(), int(PythonPropertyReader.getConfig('CREDIT_DAY_OF_WEEK'))),"%Y-%m-%d %H:%M:%S")
|
| 13927 |
amit.gupta |
315 |
return result
|
| 14037 |
kshitij.so |
316 |
|
|
|
317 |
def __populateCache(userId):
|
|
|
318 |
print "Populating memcache for userId",userId
|
|
|
319 |
outer_query = []
|
|
|
320 |
outer_query.append({"showDeal":1})
|
|
|
321 |
query = {}
|
|
|
322 |
query['$gt'] = 0
|
|
|
323 |
outer_query.append({'totalPoints':query})
|
|
|
324 |
brandPrefMap = {}
|
|
|
325 |
pricePrefMap = {}
|
|
|
326 |
actionsMap = {}
|
|
|
327 |
brand_p = session.query(price_preferences).filter_by(user_id=userId).all()
|
|
|
328 |
for x in brand_p:
|
|
|
329 |
pricePrefMap[x.category_id] = [x.min_price,x.max_price]
|
|
|
330 |
for x in session.query(brand_preferences).filter_by(user_id=userId).all():
|
|
|
331 |
temp_map = {}
|
|
|
332 |
if brandPrefMap.has_key((x.brand).strip().upper()):
|
|
|
333 |
val = brandPrefMap.get((x.brand).strip().upper())
|
|
|
334 |
temp_map[x.category_id] = 1 if x.status == 'show' else 0
|
|
|
335 |
val.append(temp_map)
|
|
|
336 |
else:
|
|
|
337 |
temp = []
|
|
|
338 |
temp_map[x.category_id] = 1 if x.status == 'show' else 0
|
|
|
339 |
temp.append(temp_map)
|
|
|
340 |
brandPrefMap[(x.brand).strip().upper()] = temp
|
|
|
341 |
|
|
|
342 |
for x in session.query(user_actions).filter_by(user_id=userId).all():
|
|
|
343 |
actionsMap[x.store_product_id] = 1 if x.action == 'like' else 0
|
| 14323 |
kshitij.so |
344 |
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,'dealType':1,'source_id':1}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
|
| 14037 |
kshitij.so |
345 |
all_category_deals = []
|
|
|
346 |
mobile_deals = []
|
|
|
347 |
tablet_deals = []
|
|
|
348 |
for deal in all_deals:
|
|
|
349 |
if actionsMap.get(deal['_id']) == 0:
|
|
|
350 |
fav_weight =.25
|
|
|
351 |
elif actionsMap.get(deal['_id']) == 1:
|
|
|
352 |
fav_weight = 1.5
|
|
|
353 |
else:
|
|
|
354 |
fav_weight = 1
|
|
|
355 |
|
|
|
356 |
if brandPrefMap.get(deal['brand'].strip().upper()) is not None:
|
|
|
357 |
brand_weight = 1
|
|
|
358 |
for brandInfo in brandPrefMap.get(deal['brand'].strip().upper()):
|
|
|
359 |
if brandInfo.get(deal['category_id']) is not None:
|
|
|
360 |
if brandInfo.get(deal['category_id']) == 1:
|
| 14055 |
kshitij.so |
361 |
brand_weight = 2.0
|
| 14037 |
kshitij.so |
362 |
else:
|
|
|
363 |
brand_weight = 1
|
|
|
364 |
|
|
|
365 |
if pricePrefMap.get(deal['category_id']) is not None:
|
|
|
366 |
|
|
|
367 |
if deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1]:
|
|
|
368 |
asp_weight = 1.5
|
|
|
369 |
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]:
|
|
|
370 |
asp_weight = 1.2
|
|
|
371 |
else:
|
|
|
372 |
asp_weight = 1
|
|
|
373 |
else:
|
|
|
374 |
asp_weight = 1
|
|
|
375 |
|
|
|
376 |
persPoints = deal['totalPoints'] * fav_weight * brand_weight * asp_weight
|
|
|
377 |
deal['persPoints'] = persPoints
|
|
|
378 |
|
|
|
379 |
if deal['category_id'] ==3:
|
|
|
380 |
mobile_deals.append(deal)
|
|
|
381 |
elif deal['category_id'] ==5:
|
|
|
382 |
tablet_deals.append(deal)
|
|
|
383 |
else:
|
|
|
384 |
continue
|
|
|
385 |
all_category_deals.append(deal)
|
|
|
386 |
|
| 14144 |
kshitij.so |
387 |
session.close()
|
| 14037 |
kshitij.so |
388 |
mem_cache_val = {3:mobile_deals, 5:tablet_deals, 0:all_deals}
|
|
|
389 |
mc.set(str(userId), mem_cache_val)
|
|
|
390 |
|
|
|
391 |
|
| 14531 |
kshitij.so |
392 |
def __populateFeaturedDeals():
|
|
|
393 |
all_category_fd = []
|
|
|
394 |
mobile_fd = []
|
|
|
395 |
tablet_fd = []
|
| 14550 |
kshitij.so |
396 |
activeFeaturedDeals = get_mongo_connection().Catalog.FeaturedDeals.find({'startDate':{'$lte':to_java_date(datetime.now())},'endDate':{'$gte':to_java_date(datetime.now())}}).sort({'rank':pymongo.ASCENDING})
|
| 14531 |
kshitij.so |
397 |
for activeFeaturedDeal in activeFeaturedDeals:
|
|
|
398 |
for k,v in activeFeaturedDeal['rankDetails']:
|
|
|
399 |
featuredDeal = FeaturedDeals(activeFeaturedDeal['sku'], int(k), activeFeaturedDeal['thresholdPrice'], int(v))
|
|
|
400 |
if featuredDeal.category_id == 0:
|
|
|
401 |
all_category_fd.append(featuredDeal)
|
|
|
402 |
elif featuredDeal.category_id == 3:
|
|
|
403 |
mobile_fd.append(featuredDeal)
|
|
|
404 |
elif featuredDeal.category_id == 5:
|
|
|
405 |
tablet_fd.append(featuredDeal)
|
|
|
406 |
else:
|
|
|
407 |
continue
|
| 14550 |
kshitij.so |
408 |
mc.set("featured_deals_category_"+str(0), all_category_fd, 3600)
|
|
|
409 |
mc.set("featured_deals_category_"+str(3), mobile_fd, 3600)
|
|
|
410 |
mc.set("featured_deals_category_"+str(5), tablet_fd, 3600)
|
| 14037 |
kshitij.so |
411 |
|
|
|
412 |
def getNewDeals(userId, category_id, offset, limit, sort, direction):
|
| 13922 |
kshitij.so |
413 |
if not bool(cashBackMap):
|
| 13921 |
kshitij.so |
414 |
populateCashBack()
|
| 14037 |
kshitij.so |
415 |
|
| 14550 |
kshitij.so |
416 |
try:
|
|
|
417 |
if mc.get("featured_deals_category_"+str(category_id)) is None:
|
|
|
418 |
__populateFeaturedDeals()
|
|
|
419 |
except:
|
|
|
420 |
pass
|
| 14531 |
kshitij.so |
421 |
|
| 13771 |
kshitij.so |
422 |
rank = 1
|
| 14037 |
kshitij.so |
423 |
dealsMap = {}
|
|
|
424 |
user_specific_deals = mc.get(str(userId))
|
|
|
425 |
if user_specific_deals is None:
|
|
|
426 |
__populateCache(userId)
|
|
|
427 |
user_specific_deals = mc.get(str(userId))
|
| 14038 |
kshitij.so |
428 |
else:
|
|
|
429 |
print "Getting user deals from cache"
|
| 14037 |
kshitij.so |
430 |
category_specific_deals = user_specific_deals.get(category_id)
|
|
|
431 |
|
|
|
432 |
if sort is None or direction is None:
|
|
|
433 |
sorted_deals = sorted(category_specific_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
|
|
|
434 |
else:
|
|
|
435 |
if sort == "bestSellerPoints":
|
|
|
436 |
sorted_deals = sorted(category_specific_deals, key = lambda x: (x['bestSellerPoints'], x['rank'], x['nlcPoints']),reverse=True)
|
|
|
437 |
else:
|
|
|
438 |
if direction == -1:
|
|
|
439 |
rev = True
|
|
|
440 |
else:
|
|
|
441 |
rev = False
|
|
|
442 |
sorted_deals = sorted(category_specific_deals, key = lambda x: (x['available_price']),reverse=rev)
|
|
|
443 |
|
|
|
444 |
for d in sorted_deals[offset:offset+limit]:
|
|
|
445 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
|
|
|
446 |
if not dealsMap.has_key(item[0]['identifier']):
|
|
|
447 |
item[0]['dealRank'] = rank
|
|
|
448 |
item[0]['persPoints'] = d['persPoints']
|
| 14322 |
kshitij.so |
449 |
if d['dealType'] == 1 and d['source_id'] ==1:
|
|
|
450 |
item[0]['marketPlaceUrl'] = "http://www.amazon.in/dp/%s"%(item[0]['identifier'].strip())
|
| 14037 |
kshitij.so |
451 |
try:
|
|
|
452 |
cashBack = __getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
|
|
|
453 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
|
|
454 |
item[0]['cash_back_type'] = 0
|
|
|
455 |
item[0]['cash_back'] = 0
|
|
|
456 |
else:
|
|
|
457 |
item[0]['cash_back_type'] = cashBack['cash_back_type']
|
|
|
458 |
item[0]['cash_back'] = cashBack['cash_back']
|
|
|
459 |
except:
|
|
|
460 |
print "Error in adding cashback to deals"
|
|
|
461 |
item[0]['cash_back_type'] = 0
|
|
|
462 |
item[0]['cash_back'] = 0
|
|
|
463 |
dealsMap[item[0]['identifier']] = item[0]
|
|
|
464 |
|
|
|
465 |
rank +=1
|
|
|
466 |
return sorted(dealsMap.values(), key=itemgetter('dealRank'))
|
|
|
467 |
|
|
|
468 |
|
|
|
469 |
def getDeals(userId, category_id, offset, limit, sort, direction):
|
|
|
470 |
if not bool(cashBackMap):
|
|
|
471 |
populateCashBack()
|
|
|
472 |
rank = 1
|
| 13771 |
kshitij.so |
473 |
deals = {}
|
| 13910 |
kshitij.so |
474 |
outer_query = []
|
|
|
475 |
outer_query.append({"showDeal":1})
|
|
|
476 |
query = {}
|
|
|
477 |
query['$gt'] = 0
|
|
|
478 |
outer_query.append({'totalPoints':query})
|
|
|
479 |
if category_id in (3,5):
|
|
|
480 |
outer_query.append({'category_id':category_id})
|
| 13803 |
kshitij.so |
481 |
if sort is None or direction is None:
|
|
|
482 |
direct = -1
|
| 13910 |
kshitij.so |
483 |
print outer_query
|
|
|
484 |
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 |
485 |
else:
|
| 13910 |
kshitij.so |
486 |
print outer_query
|
| 13803 |
kshitij.so |
487 |
direct = direction
|
| 13910 |
kshitij.so |
488 |
if sort == "bestSellerPoints":
|
|
|
489 |
print "yes,sorting by bestSellerPoints"
|
|
|
490 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('bestSellerPoints',direct),('rank',direct),('nlcPoints',direct)]).skip(offset).limit(limit))
|
|
|
491 |
else:
|
|
|
492 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([(sort,direct)]).skip(offset).limit(limit))
|
| 13771 |
kshitij.so |
493 |
for d in data:
|
|
|
494 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
|
|
|
495 |
if not deals.has_key(item[0]['identifier']):
|
| 13921 |
kshitij.so |
496 |
item[0]['dealRank'] = rank
|
|
|
497 |
try:
|
|
|
498 |
cashBack = __getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
|
|
|
499 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
| 13928 |
kshitij.so |
500 |
item[0]['cash_back_type'] = 0
|
|
|
501 |
item[0]['cash_back'] = 0
|
| 13921 |
kshitij.so |
502 |
else:
|
| 13928 |
kshitij.so |
503 |
item[0]['cash_back_type'] = cashBack['cash_back_type']
|
|
|
504 |
item[0]['cash_back'] = cashBack['cash_back']
|
| 13921 |
kshitij.so |
505 |
except:
|
|
|
506 |
print "Error in adding cashback to deals"
|
| 13928 |
kshitij.so |
507 |
item[0]['cash_back_type'] = 0
|
|
|
508 |
item[0]['cash_back'] = 0
|
| 13771 |
kshitij.so |
509 |
deals[item[0]['identifier']] = item[0]
|
| 13921 |
kshitij.so |
510 |
|
| 13771 |
kshitij.so |
511 |
rank +=1
|
| 13785 |
kshitij.so |
512 |
return sorted(deals.values(), key=itemgetter('dealRank'))
|
|
|
513 |
|
|
|
514 |
def getItem(skuId):
|
| 14113 |
kshitij.so |
515 |
if not bool(cashBackMap):
|
|
|
516 |
populateCashBack()
|
| 13795 |
kshitij.so |
517 |
try:
|
|
|
518 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
|
| 14107 |
kshitij.so |
519 |
for sku in skuData:
|
|
|
520 |
try:
|
|
|
521 |
cashBack = __getCashBack(sku['_id'], sku['source_id'], sku['category_id'])
|
|
|
522 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
|
|
523 |
sku['cash_back_type'] = 0
|
|
|
524 |
sku['cash_back'] = 0
|
|
|
525 |
else:
|
|
|
526 |
sku['cash_back_type'] = cashBack['cash_back_type']
|
|
|
527 |
sku['cash_back'] = cashBack['cash_back']
|
|
|
528 |
except:
|
|
|
529 |
print "Error in adding cashback to deals"
|
|
|
530 |
sku['cash_back_type'] = 0
|
|
|
531 |
sku['cash_back'] = 0
|
| 13785 |
kshitij.so |
532 |
return skuData
|
| 13795 |
kshitij.so |
533 |
except:
|
|
|
534 |
return [{}]
|
| 13836 |
kshitij.so |
535 |
|
| 13921 |
kshitij.so |
536 |
def __getCashBack(skuId, source_id, category_id):
|
|
|
537 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
538 |
if itemCashBack is not None:
|
|
|
539 |
return itemCashBack
|
|
|
540 |
|
|
|
541 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
542 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
543 |
for cashBack in sourceCashBack:
|
|
|
544 |
if cashBack.get(category_id) is None:
|
|
|
545 |
continue
|
|
|
546 |
else:
|
|
|
547 |
return cashBack.get(category_id)
|
|
|
548 |
else:
|
|
|
549 |
return {}
|
|
|
550 |
|
| 13836 |
kshitij.so |
551 |
def getCashBackDetails(identifier, source_id):
|
| 13922 |
kshitij.so |
552 |
if not bool(cashBackMap):
|
| 13921 |
kshitij.so |
553 |
populateCashBack()
|
|
|
554 |
|
| 13836 |
kshitij.so |
555 |
"""Need to add item level cashback, no data available right now."""
|
| 13771 |
kshitij.so |
556 |
|
| 13921 |
kshitij.so |
557 |
if source_id in (1,2,4,5):
|
| 13839 |
kshitij.so |
558 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
|
| 13840 |
kshitij.so |
559 |
elif source_id == 3:
|
| 13839 |
kshitij.so |
560 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
|
| 13840 |
kshitij.so |
561 |
else:
|
|
|
562 |
return {}
|
| 13836 |
kshitij.so |
563 |
if len(skuData) > 0:
|
| 13921 |
kshitij.so |
564 |
|
|
|
565 |
itemCashBack = itemCashBackMap.get(skuData[0]['_id'])
|
|
|
566 |
if itemCashBack is not None:
|
|
|
567 |
return itemCashBack
|
|
|
568 |
|
|
|
569 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
570 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
571 |
for cashBack in sourceCashBack:
|
|
|
572 |
if cashBack.get(skuData[0]['category_id']) is None:
|
|
|
573 |
continue
|
|
|
574 |
else:
|
|
|
575 |
return cashBack.get(skuData[0]['category_id'])
|
| 13836 |
kshitij.so |
576 |
else:
|
|
|
577 |
return {}
|
|
|
578 |
else:
|
|
|
579 |
return {}
|
| 13986 |
amit.gupta |
580 |
|
| 14398 |
amit.gupta |
581 |
def getImgSrc(identifier, source_id):
|
|
|
582 |
skuData = None
|
|
|
583 |
if source_id in (1,2,4,5):
|
| 14414 |
amit.gupta |
584 |
skuData = get_mongo_connection().Catalog.MasterData.find_one({'identifier':identifier.strip(), 'source_id':source_id})
|
| 14398 |
amit.gupta |
585 |
elif source_id == 3:
|
| 14414 |
amit.gupta |
586 |
skuData = get_mongo_connection().Catalog.MasterData.find_one({'secondaryIdentifier':identifier.strip(), 'source_id':source_id})
|
| 14398 |
amit.gupta |
587 |
if skuData is None:
|
|
|
588 |
return {}
|
|
|
589 |
else:
|
|
|
590 |
return {'thumbnail':skuData.get('thumbnail')}
|
| 13986 |
amit.gupta |
591 |
|
|
|
592 |
def next_weekday(d, weekday):
|
|
|
593 |
days_ahead = weekday - d.weekday()
|
|
|
594 |
if days_ahead <= 0: # Target day already happened this week
|
|
|
595 |
days_ahead += 7
|
|
|
596 |
return d + timedelta(days_ahead)
|
|
|
597 |
|
| 13771 |
kshitij.so |
598 |
|
| 13970 |
kshitij.so |
599 |
def getAllDealerPrices(offset, limit):
|
|
|
600 |
data = []
|
|
|
601 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
602 |
cursor = collection.find().skip(offset).limit(limit)
|
|
|
603 |
for val in cursor:
|
|
|
604 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
605 |
if len(master) > 0:
|
| 14069 |
kshitij.so |
606 |
val['brand'] = master[0]['brand']
|
|
|
607 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
608 |
val['skuBundleId'] = master[0]['skuBundleId']
|
| 13970 |
kshitij.so |
609 |
else:
|
|
|
610 |
val['brand'] = ""
|
|
|
611 |
val['source_product_name'] = ""
|
|
|
612 |
val['skuBundleId'] = ""
|
|
|
613 |
data.append(val)
|
|
|
614 |
return data
|
|
|
615 |
|
| 14553 |
kshitij.so |
616 |
def addSkuDealerPrice(data, multi):
|
|
|
617 |
if multi != 1:
|
|
|
618 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
619 |
cursor = collection.find({"sku":data['sku']})
|
|
|
620 |
if cursor.count() > 0:
|
|
|
621 |
return {0:"Sku information already present."}
|
|
|
622 |
else:
|
|
|
623 |
collection.insert(data)
|
|
|
624 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
625 |
return {1:"Data added successfully"}
|
| 13970 |
kshitij.so |
626 |
else:
|
| 14553 |
kshitij.so |
627 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
628 |
for sku in skuIds:
|
|
|
629 |
data['sku'] = sku.get('_id')
|
|
|
630 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
631 |
cursor = collection.find({"sku":data['sku']})
|
|
|
632 |
if cursor.count() > 0:
|
|
|
633 |
continue
|
|
|
634 |
else:
|
| 14558 |
kshitij.so |
635 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
636 |
collection.insert(data)
|
|
|
637 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 13970 |
kshitij.so |
638 |
return {1:"Data added successfully"}
|
| 14553 |
kshitij.so |
639 |
|
|
|
640 |
|
| 13970 |
kshitij.so |
641 |
|
|
|
642 |
def updateSkuDealerPrice(data, _id):
|
|
|
643 |
try:
|
|
|
644 |
collection = get_mongo_connection().Catalog.SkuDealerPrices
|
|
|
645 |
collection.update({'_id':ObjectId(_id)},{"$set":{'dp':data['dp']}},upsert=False, multi = False)
|
|
|
646 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
647 |
return {1:"Data updated successfully"}
|
|
|
648 |
except:
|
|
|
649 |
return {0:"Data not updated."}
|
|
|
650 |
|
|
|
651 |
def updateExceptionalNlc(data, _id):
|
|
|
652 |
try:
|
|
|
653 |
collection = get_mongo_connection().Catalog.ExceptionalNlc
|
|
|
654 |
collection.update({'_id':ObjectId(_id)},{"$set":{'maxNlc':data['maxNlc'], 'minNlc':data['minNlc'], 'overrideNlc':data['overrideNlc']}},upsert=False, multi = False)
|
|
|
655 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
656 |
return {1:"Data updated successfully"}
|
|
|
657 |
except:
|
|
|
658 |
return {0:"Data not updated."}
|
|
|
659 |
|
| 14041 |
kshitij.so |
660 |
def resetCache(userId):
|
| 14043 |
kshitij.so |
661 |
try:
|
|
|
662 |
mc.delete(userId)
|
|
|
663 |
return {1:'Cache cleared.'}
|
|
|
664 |
except:
|
|
|
665 |
return {0:'Unable to clear cache.'}
|
|
|
666 |
|
| 14575 |
kshitij.so |
667 |
def updateCollection(data, multi):
|
| 14083 |
kshitij.so |
668 |
print data
|
| 14575 |
kshitij.so |
669 |
if multi!=1:
|
|
|
670 |
try:
|
|
|
671 |
collection = get_mongo_connection().Catalog[data['class']]
|
|
|
672 |
class_name = data.pop('class')
|
|
|
673 |
if class_name == "SkuSchemeDetails":
|
|
|
674 |
data['addedOn'] = to_java_date(datetime.now())
|
|
|
675 |
_id = data.pop('oid')
|
|
|
676 |
result = collection.update({'_id':ObjectId(_id)},{"$set":data},upsert=False, multi = False)
|
|
|
677 |
record = list(collection.find({'_id':ObjectId(_id)}))
|
|
|
678 |
if class_name !="CategoryDiscount":
|
|
|
679 |
get_mongo_connection().Catalog.MasterData.update({'_id':record[0]['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}})
|
|
|
680 |
else:
|
|
|
681 |
get_mongo_connection().Catalog.MasterData.update({'brand':re.compile(record[0]['brand'], re.IGNORECASE),'category_id':record[0]['category_id']}, \
|
|
|
682 |
{"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False,multi=True)
|
|
|
683 |
return {1:"Data updated successfully"}
|
|
|
684 |
except Exception as e:
|
|
|
685 |
print e
|
|
|
686 |
return {0:"Data not updated."}
|
|
|
687 |
else:
|
|
|
688 |
try:
|
|
|
689 |
collection = get_mongo_connection().Catalog[data['class']]
|
|
|
690 |
class_name = data.pop('class')
|
|
|
691 |
_id = data.pop('oid')
|
|
|
692 |
record = list(collection.find({'_id':ObjectId(_id)}))
|
|
|
693 |
skuIds = __getBundledSkusfromSku(record[0]['sku'])
|
|
|
694 |
for sku in skuIds:
|
|
|
695 |
if class_name == "SkuSchemeDetails":
|
|
|
696 |
data['addedOn'] = to_java_date(datetime.now())
|
| 14582 |
kshitij.so |
697 |
data['sku'] = sku.get('_id')
|
| 14575 |
kshitij.so |
698 |
data.pop('_id',None)
|
| 14583 |
kshitij.so |
699 |
collection.update({'sku':sku.get('_id')},{"$set":data},upsert=False,multi=False)
|
| 14575 |
kshitij.so |
700 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 14580 |
kshitij.so |
701 |
return {1:"Data updatedsuccessfully"}
|
| 14575 |
kshitij.so |
702 |
except Exception as e:
|
|
|
703 |
print e
|
|
|
704 |
return {0:"Data not updated."}
|
|
|
705 |
|
| 14076 |
kshitij.so |
706 |
|
| 14553 |
kshitij.so |
707 |
def addNegativeDeals(data, multi):
|
|
|
708 |
if multi !=1:
|
|
|
709 |
collection = get_mongo_connection().Catalog.NegativeDeals
|
|
|
710 |
cursor = collection.find({"sku":data['sku']})
|
|
|
711 |
if cursor.count() > 0:
|
|
|
712 |
return {0:"Sku information already present."}
|
|
|
713 |
else:
|
|
|
714 |
collection.insert(data)
|
|
|
715 |
return {1:"Data added successfully"}
|
| 14481 |
kshitij.so |
716 |
else:
|
| 14553 |
kshitij.so |
717 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
718 |
for sku in skuIds:
|
|
|
719 |
data['sku'] = sku.get('_id')
|
|
|
720 |
collection = get_mongo_connection().Catalog.NegativeDeals
|
|
|
721 |
cursor = collection.find({"sku":data['sku']})
|
|
|
722 |
if cursor.count() > 0:
|
|
|
723 |
continue
|
|
|
724 |
else:
|
| 14558 |
kshitij.so |
725 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
726 |
collection.insert(data)
|
| 14481 |
kshitij.so |
727 |
return {1:"Data added successfully"}
|
|
|
728 |
|
|
|
729 |
def getAllNegativeDeals(offset, limit):
|
|
|
730 |
data = []
|
|
|
731 |
collection = get_mongo_connection().Catalog.NegativeDeals
|
|
|
732 |
cursor = collection.find().skip(offset).limit(limit)
|
|
|
733 |
for val in cursor:
|
|
|
734 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
735 |
if len(master) > 0:
|
|
|
736 |
val['brand'] = master[0]['brand']
|
|
|
737 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
738 |
val['skuBundleId'] = master[0]['skuBundleId']
|
|
|
739 |
else:
|
|
|
740 |
val['brand'] = ""
|
|
|
741 |
val['source_product_name'] = ""
|
|
|
742 |
val['skuBundleId'] = ""
|
|
|
743 |
data.append(val)
|
|
|
744 |
return data
|
|
|
745 |
|
|
|
746 |
def getAllManualDeals(offset, limit):
|
|
|
747 |
data = []
|
|
|
748 |
collection = get_mongo_connection().Catalog.ManualDeals
|
| 14495 |
kshitij.so |
749 |
cursor = collection.find({'endDate':{'$gte':to_java_date(datetime.now())}}).skip(offset).limit(limit)
|
| 14481 |
kshitij.so |
750 |
for val in cursor:
|
|
|
751 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
752 |
if len(master) > 0:
|
|
|
753 |
val['brand'] = master[0]['brand']
|
|
|
754 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
755 |
val['skuBundleId'] = master[0]['skuBundleId']
|
|
|
756 |
else:
|
|
|
757 |
val['brand'] = ""
|
|
|
758 |
val['source_product_name'] = ""
|
|
|
759 |
val['skuBundleId'] = ""
|
|
|
760 |
data.append(val)
|
|
|
761 |
return data
|
| 14076 |
kshitij.so |
762 |
|
| 14553 |
kshitij.so |
763 |
def addManualDeal(data, multi):
|
|
|
764 |
if multi !=1:
|
|
|
765 |
collection = get_mongo_connection().Catalog.ManualDeals
|
|
|
766 |
cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
|
|
|
767 |
if cursor.count() > 0:
|
|
|
768 |
return {0:"Sku information already present."}
|
|
|
769 |
else:
|
|
|
770 |
collection.insert(data)
|
|
|
771 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
772 |
return {1:"Data added successfully"}
|
| 14481 |
kshitij.so |
773 |
else:
|
| 14553 |
kshitij.so |
774 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
775 |
for sku in skuIds:
|
|
|
776 |
data['sku'] = sku.get('_id')
|
|
|
777 |
collection = get_mongo_connection().Catalog.ManualDeals
|
|
|
778 |
cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
|
|
|
779 |
if cursor.count() > 0:
|
|
|
780 |
continue
|
|
|
781 |
else:
|
| 14558 |
kshitij.so |
782 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
783 |
collection.insert(data)
|
|
|
784 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 14481 |
kshitij.so |
785 |
return {1:"Data added successfully"}
|
| 14553 |
kshitij.so |
786 |
|
| 14481 |
kshitij.so |
787 |
def deleteDocument(data):
|
|
|
788 |
print data
|
|
|
789 |
try:
|
|
|
790 |
collection = get_mongo_connection().Catalog[data['class']]
|
|
|
791 |
class_name = data.pop('class')
|
|
|
792 |
_id = data.pop('oid')
|
|
|
793 |
record = list(collection.find({'_id':ObjectId(_id)}))
|
|
|
794 |
collection.remove({'_id':ObjectId(_id)})
|
|
|
795 |
if class_name !="CategoryDiscount":
|
|
|
796 |
get_mongo_connection().Catalog.MasterData.update({'_id':record[0]['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}})
|
|
|
797 |
else:
|
|
|
798 |
get_mongo_connection().Catalog.MasterData.update({'brand':re.compile(record[0]['brand'], re.IGNORECASE),'category_id':record[0]['category_id']}, \
|
|
|
799 |
{"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False,multi=True)
|
|
|
800 |
return {1:"Document deleted successfully"}
|
|
|
801 |
except Exception as e:
|
|
|
802 |
print e
|
|
|
803 |
return {0:"Document not deleted."}
|
| 13970 |
kshitij.so |
804 |
|
| 14482 |
kshitij.so |
805 |
def searchMaster(offset, limit, search_term):
|
|
|
806 |
data = []
|
| 14531 |
kshitij.so |
807 |
if search_term is not None:
|
| 14551 |
kshitij.so |
808 |
terms = search_term.split(' ')
|
|
|
809 |
outer_query = []
|
|
|
810 |
for term in terms:
|
|
|
811 |
outer_query.append({"source_product_name":re.compile(term, re.IGNORECASE)})
|
| 14531 |
kshitij.so |
812 |
try:
|
| 14551 |
kshitij.so |
813 |
collection = get_mongo_connection().Catalog.MasterData.find({"$and":outer_query,'source_id':{'$in':SOURCE_MAP.keys()}}).skip(offset).limit(limit)
|
| 14531 |
kshitij.so |
814 |
for record in collection:
|
|
|
815 |
data.append(record)
|
|
|
816 |
except:
|
|
|
817 |
pass
|
|
|
818 |
else:
|
|
|
819 |
collection = get_mongo_connection().Catalog.MasterData.find({'source_id':{'$in':SOURCE_MAP.keys()}}).skip(offset).limit(limit)
|
| 14482 |
kshitij.so |
820 |
for record in collection:
|
|
|
821 |
data.append(record)
|
|
|
822 |
return data
|
| 14481 |
kshitij.so |
823 |
|
| 14495 |
kshitij.so |
824 |
def getAllFeaturedDeals(offset, limit):
|
|
|
825 |
data = []
|
|
|
826 |
collection = get_mongo_connection().Catalog.FeaturedDeals
|
|
|
827 |
cursor = collection.find({'endDate':{'$gte':to_java_date(datetime.now())}}).skip(offset).limit(limit)
|
|
|
828 |
for val in cursor:
|
|
|
829 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
830 |
if len(master) > 0:
|
|
|
831 |
val['brand'] = master[0]['brand']
|
|
|
832 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
833 |
val['skuBundleId'] = master[0]['skuBundleId']
|
|
|
834 |
else:
|
|
|
835 |
val['brand'] = ""
|
|
|
836 |
val['source_product_name'] = ""
|
|
|
837 |
val['skuBundleId'] = ""
|
|
|
838 |
data.append(val)
|
|
|
839 |
return data
|
|
|
840 |
|
| 14553 |
kshitij.so |
841 |
def addFeaturedDeal(data, multi):
|
|
|
842 |
if multi !=1:
|
|
|
843 |
collection = get_mongo_connection().Catalog.FeaturedDeals
|
|
|
844 |
cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
|
|
|
845 |
if cursor.count() > 0:
|
|
|
846 |
return {0:"Sku information already present."}
|
|
|
847 |
else:
|
|
|
848 |
collection.insert(data)
|
|
|
849 |
get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
|
|
|
850 |
return {1:"Data added successfully"}
|
| 14495 |
kshitij.so |
851 |
else:
|
| 14553 |
kshitij.so |
852 |
skuIds = __getBundledSkusfromSku(data['sku'])
|
|
|
853 |
for sku in skuIds:
|
|
|
854 |
data['sku'] = sku.get('_id')
|
|
|
855 |
collection = get_mongo_connection().Catalog.FeaturedDeals
|
|
|
856 |
cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
|
|
|
857 |
if cursor.count() > 0:
|
|
|
858 |
continue
|
|
|
859 |
else:
|
| 14558 |
kshitij.so |
860 |
data.pop('_id',None)
|
| 14553 |
kshitij.so |
861 |
collection.insert(data)
|
|
|
862 |
get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
|
| 14495 |
kshitij.so |
863 |
return {1:"Data added successfully"}
|
|
|
864 |
|
| 14499 |
kshitij.so |
865 |
def searchCollection(class_name, sku, skuBundleId):
|
| 14497 |
kshitij.so |
866 |
data = []
|
|
|
867 |
collection = get_mongo_connection().Catalog[class_name]
|
| 14499 |
kshitij.so |
868 |
if sku is not None:
|
|
|
869 |
cursor = collection.find({'sku':sku})
|
|
|
870 |
for val in cursor:
|
|
|
871 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
872 |
if len(master) > 0:
|
|
|
873 |
val['brand'] = master[0]['brand']
|
|
|
874 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
875 |
val['skuBundleId'] = master[0]['skuBundleId']
|
|
|
876 |
else:
|
|
|
877 |
val['brand'] = ""
|
|
|
878 |
val['source_product_name'] = ""
|
|
|
879 |
val['skuBundleId'] = ""
|
|
|
880 |
data.append(val)
|
|
|
881 |
return data
|
|
|
882 |
else:
|
| 14562 |
kshitij.so |
883 |
skuIds = get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId}).distinct('_id')
|
| 14499 |
kshitij.so |
884 |
for sku in skuIds:
|
|
|
885 |
cursor = collection.find({'sku':sku})
|
|
|
886 |
for val in cursor:
|
|
|
887 |
master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
|
|
|
888 |
if len(master) > 0:
|
|
|
889 |
val['brand'] = master[0]['brand']
|
|
|
890 |
val['source_product_name'] = master[0]['source_product_name']
|
|
|
891 |
val['skuBundleId'] = master[0]['skuBundleId']
|
|
|
892 |
else:
|
|
|
893 |
val['brand'] = ""
|
|
|
894 |
val['source_product_name'] = ""
|
|
|
895 |
val['skuBundleId'] = ""
|
|
|
896 |
data.append(val)
|
|
|
897 |
return data
|
| 14495 |
kshitij.so |
898 |
|
| 14588 |
kshitij.so |
899 |
def addNewItem(data):
|
| 14594 |
kshitij.so |
900 |
try:
|
|
|
901 |
data['updatedOn'] = to_java_date(datetime.now())
|
|
|
902 |
data['addedOn'] = to_java_date(datetime.now())
|
|
|
903 |
data['priceUpdatedOn'] = to_java_date(datetime.now())
|
|
|
904 |
max_id = list(get_mongo_connection().Catalog.MasterData.find().sort([('_id',pymongo.DESCENDING)]).limit(1))
|
|
|
905 |
max_bundle = list(get_mongo_connection().Catalog.MasterData.find().sort([('skuBundleId',pymongo.DESCENDING)]).limit(1))
|
|
|
906 |
data['_id'] = max_id[0]['_id'] + 1
|
|
|
907 |
data['skuBundleId'] = max_bundle[0]['skuBundleId'] + 1
|
|
|
908 |
data['identifier'] = str(data['identifier'])
|
|
|
909 |
data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
|
|
|
910 |
get_mongo_connection().Catalog.MasterData.insert(data)
|
|
|
911 |
return {1:'Data added successfully'}
|
|
|
912 |
except Exception as e:
|
|
|
913 |
print e
|
|
|
914 |
return {0:'Unable to add data.'}
|
| 14588 |
kshitij.so |
915 |
|
|
|
916 |
def addItemToExistingBundle(data):
|
|
|
917 |
try:
|
|
|
918 |
data['updatedOn'] = to_java_date(datetime.now())
|
|
|
919 |
data['addedOn'] = to_java_date(datetime.now())
|
|
|
920 |
data['priceUpdatedOn'] = to_java_date(datetime.now())
|
| 14593 |
kshitij.so |
921 |
max_id = list(get_mongo_connection().Catalog.MasterData.find().sort([('_id',pymongo.DESCENDING)]).limit(1))
|
| 14590 |
kshitij.so |
922 |
data['_id'] = max_id[0]['_id'] + 1
|
| 14594 |
kshitij.so |
923 |
data['identifier'] = str(data['identifier'])
|
|
|
924 |
data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
|
| 14588 |
kshitij.so |
925 |
get_mongo_connection().Catalog.MasterData.insert(data)
|
|
|
926 |
return {1:'Data added successfully.'}
|
| 14594 |
kshitij.so |
927 |
except Exception as e:
|
|
|
928 |
print e
|
| 14588 |
kshitij.so |
929 |
return {0:'Unable to add data.'}
|
|
|
930 |
|
|
|
931 |
def updateMaster(data, multi):
|
| 14597 |
kshitij.so |
932 |
if multi != 1:
|
|
|
933 |
_id = data.pop('_id')
|
|
|
934 |
skuBundleId = data.pop('skuBundleId')
|
|
|
935 |
data['updatedOn'] = to_java_date(datetime.now())
|
|
|
936 |
get_mongo_connection().Catalog.MasterData.update({'_id':_id},{"$set":data},upsert=False)
|
|
|
937 |
return {1:'Data updated successfully.'}
|
|
|
938 |
else:
|
|
|
939 |
_id = data.pop('_id')
|
|
|
940 |
skuBundleId = data.pop('skuBundleId')
|
|
|
941 |
get_mongo_connection().Catalog.MasterData.update({'_id':_id},{"$set":data},upsert=False)
|
|
|
942 |
similarItems = get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId})
|
|
|
943 |
for item in similarItems:
|
| 14598 |
kshitij.so |
944 |
if item['_id'] == _id:
|
| 14597 |
kshitij.so |
945 |
continue
|
|
|
946 |
item['updatedOn'] = to_java_date(datetime.now())
|
|
|
947 |
item['thumbnail'] = data['thumbnail']
|
|
|
948 |
item['category'] = data['category']
|
|
|
949 |
item['category_id'] = data['category_id']
|
|
|
950 |
item['tagline'] = data['tagline']
|
|
|
951 |
item['is_shortage'] = data['is_shortage']
|
|
|
952 |
item['mrp'] = data['mrp']
|
|
|
953 |
get_mongo_connection().Catalog.MasterData.update({'_id':item['_id']},{"$set":item},upsert=False)
|
|
|
954 |
return {1:'Data updated successfully.'}
|
| 14588 |
kshitij.so |
955 |
|
| 13572 |
kshitij.so |
956 |
def main():
|
| 14553 |
kshitij.so |
957 |
print __getBundledSkusfromSku(1)
|
| 13811 |
kshitij.so |
958 |
|
| 13921 |
kshitij.so |
959 |
|
| 13572 |
kshitij.so |
960 |
if __name__=='__main__':
|
| 13932 |
amit.gupta |
961 |
main()
|