| 13572 |
kshitij.so |
1 |
import pymongo
|
|
|
2 |
from datetime import datetime
|
| 13785 |
kshitij.so |
3 |
from dtr.utils.utils import to_java_date
|
|
|
4 |
from operator import itemgetter
|
| 13572 |
kshitij.so |
5 |
|
|
|
6 |
con = None
|
| 13907 |
kshitij.so |
7 |
cashBackMap = {}
|
| 13921 |
kshitij.so |
8 |
itemCashBackMap = {}
|
| 13572 |
kshitij.so |
9 |
|
|
|
10 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
11 |
global con
|
|
|
12 |
if con is None:
|
|
|
13 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
14 |
try:
|
|
|
15 |
con = pymongo.MongoClient(host, port)
|
|
|
16 |
except Exception, e:
|
|
|
17 |
print e
|
|
|
18 |
return None
|
|
|
19 |
return con
|
|
|
20 |
|
| 13907 |
kshitij.so |
21 |
def populateCashBack():
|
| 13921 |
kshitij.so |
22 |
print "Populating cashback"
|
| 13907 |
kshitij.so |
23 |
global cashBackMap
|
| 13921 |
kshitij.so |
24 |
global itemCashBackMap
|
|
|
25 |
cashBackMap = {}
|
|
|
26 |
itemCashBackMap = {}
|
| 13907 |
kshitij.so |
27 |
cashBack = list(get_mongo_connection().Catalog.CategoryCashBack.find())
|
|
|
28 |
for row in cashBack:
|
|
|
29 |
if cashBackMap.has_key(row['source_id']):
|
|
|
30 |
temp_map = {}
|
|
|
31 |
temp_list = []
|
|
|
32 |
arr = cashBackMap.get(row['source_id'])
|
|
|
33 |
for val in arr:
|
|
|
34 |
temp_list.append(val)
|
|
|
35 |
temp_map[row['category_id']] = row
|
|
|
36 |
temp_list.append(temp_map)
|
|
|
37 |
cashBackMap[row['source_id']] = temp_list
|
|
|
38 |
else:
|
|
|
39 |
temp_map = {}
|
|
|
40 |
temp_list = []
|
|
|
41 |
temp_map[row['category_id']] = row
|
|
|
42 |
temp_list.append(temp_map)
|
|
|
43 |
cashBackMap[row['source_id']] = temp_list
|
| 13921 |
kshitij.so |
44 |
itemCashBack = list(get_mongo_connection().Catalog.ItemCashBack.find())
|
|
|
45 |
for row in itemCashBack:
|
|
|
46 |
if not itemCashBackMap.has_key(row['skuId']):
|
|
|
47 |
itemCashBackMap[row['skuId']] = row
|
| 13907 |
kshitij.so |
48 |
|
| 13572 |
kshitij.so |
49 |
def addCategoryDiscount(data):
|
|
|
50 |
collection = get_mongo_connection().Dtr.CategoryDiscount
|
|
|
51 |
query = []
|
|
|
52 |
data['brand'] = data['brand'].strip().upper()
|
|
|
53 |
query.append({"brand":data['brand']})
|
|
|
54 |
query.append({"category_id":data['category_id']})
|
|
|
55 |
r = collection.find({"$and":query})
|
|
|
56 |
if r.count() > 0:
|
| 13639 |
kshitij.so |
57 |
return {0:"Brand & Category info already present."}
|
| 13572 |
kshitij.so |
58 |
else:
|
|
|
59 |
collection.insert(data)
|
| 13639 |
kshitij.so |
60 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
61 |
|
|
|
62 |
def getAllCategoryDiscount():
|
|
|
63 |
data = []
|
|
|
64 |
collection = get_mongo_connection().Dtr.CategoryDiscount
|
|
|
65 |
cursor = collection.find()
|
|
|
66 |
for val in cursor:
|
|
|
67 |
data.append(val)
|
|
|
68 |
return data
|
|
|
69 |
|
|
|
70 |
def addSchemeDetailsForSku(data):
|
|
|
71 |
collection = get_mongo_connection().Dtr.SkuSchemeDetails
|
|
|
72 |
data['addedOn'] = to_java_date(datetime.now())
|
|
|
73 |
collection.insert(data)
|
| 13639 |
kshitij.so |
74 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
75 |
|
|
|
76 |
def getAllSkuWiseSchemeDetails():
|
|
|
77 |
data = []
|
|
|
78 |
collection = get_mongo_connection().Dtr.SkuSchemeDetails
|
|
|
79 |
cursor = collection.find()
|
|
|
80 |
for val in cursor:
|
|
|
81 |
data.append(val)
|
|
|
82 |
return data
|
|
|
83 |
|
|
|
84 |
def addSkuDiscountInfo(data):
|
|
|
85 |
collection = get_mongo_connection().Dtr.SkuDiscountInfo
|
|
|
86 |
cursor = collection.find({"sku":data['sku']})
|
| 13642 |
kshitij.so |
87 |
if cursor.count() > 0:
|
| 13639 |
kshitij.so |
88 |
return {0:"Sku information already present."}
|
| 13572 |
kshitij.so |
89 |
else:
|
|
|
90 |
collection.insert(data)
|
| 13639 |
kshitij.so |
91 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
92 |
|
|
|
93 |
def getallSkuDiscountInfo():
|
|
|
94 |
data = []
|
|
|
95 |
collection = get_mongo_connection().Dtr.SkuDiscountInfo
|
|
|
96 |
cursor = collection.find()
|
|
|
97 |
for val in cursor:
|
|
|
98 |
data.append(val)
|
|
|
99 |
return data
|
|
|
100 |
|
|
|
101 |
def addExceptionalNlc(data):
|
|
|
102 |
collection = get_mongo_connection().Dtr.ExceptionalNlc
|
|
|
103 |
cursor = collection.find({"sku":data['sku']})
|
| 13642 |
kshitij.so |
104 |
if cursor.count() > 0:
|
| 13639 |
kshitij.so |
105 |
return {0:"Sku information already present."}
|
| 13572 |
kshitij.so |
106 |
else:
|
|
|
107 |
collection.insert(data)
|
| 13639 |
kshitij.so |
108 |
return {1:"Data added successfully"}
|
| 13572 |
kshitij.so |
109 |
|
|
|
110 |
def getAllExceptionlNlcItems():
|
|
|
111 |
data = []
|
| 13639 |
kshitij.so |
112 |
collection = get_mongo_connection().Dtr.ExceptionalNlc
|
| 13572 |
kshitij.so |
113 |
cursor = collection.find()
|
|
|
114 |
for val in cursor:
|
|
|
115 |
data.append(val)
|
|
|
116 |
return data
|
|
|
117 |
|
| 13582 |
amit.gupta |
118 |
def getMerchantOrdersByUser(userId, page=1, window=50):
|
| 13603 |
amit.gupta |
119 |
if page==None:
|
|
|
120 |
page = 1
|
|
|
121 |
|
|
|
122 |
if window==None:
|
|
|
123 |
window = 50
|
|
|
124 |
result = {}
|
| 13582 |
amit.gupta |
125 |
skip = (page-1)*window
|
| 13603 |
amit.gupta |
126 |
collection = get_mongo_connection().Dtr.merchantOrder
|
|
|
127 |
cursor = collection.find({"userId":userId})
|
|
|
128 |
total_count = cursor.count()
|
|
|
129 |
pages = total_count/window + (0 if total_count%window==0 else 1)
|
|
|
130 |
print "total_count", total_count
|
|
|
131 |
if total_count > skip:
|
|
|
132 |
cursor = cursor.skip(skip).limit(window)
|
|
|
133 |
orders = []
|
|
|
134 |
for order in cursor:
|
|
|
135 |
del(order["_id"])
|
|
|
136 |
orders.append(order)
|
|
|
137 |
result['data'] = orders
|
|
|
138 |
result['window'] = window
|
|
|
139 |
result['totalCount'] = total_count
|
|
|
140 |
result['currCount'] = cursor.count()
|
|
|
141 |
result['totalPages'] = pages
|
|
|
142 |
result['currPage'] = page
|
|
|
143 |
return result
|
|
|
144 |
else:
|
|
|
145 |
return result
|
| 13630 |
kshitij.so |
146 |
|
| 13795 |
kshitij.so |
147 |
def getDeals(category_id, offset, limit, sort, direction):
|
| 13921 |
kshitij.so |
148 |
if (not bool(cashBackMap)) or (not bool(itemCashBackMap)):
|
|
|
149 |
populateCashBack()
|
| 13771 |
kshitij.so |
150 |
rank = 1
|
|
|
151 |
deals = {}
|
| 13910 |
kshitij.so |
152 |
outer_query = []
|
|
|
153 |
outer_query.append({"showDeal":1})
|
|
|
154 |
query = {}
|
|
|
155 |
query['$gt'] = 0
|
|
|
156 |
outer_query.append({'totalPoints':query})
|
|
|
157 |
if category_id in (3,5):
|
|
|
158 |
outer_query.append({'category_id':category_id})
|
| 13803 |
kshitij.so |
159 |
if sort is None or direction is None:
|
|
|
160 |
direct = -1
|
| 13910 |
kshitij.so |
161 |
print outer_query
|
|
|
162 |
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 |
163 |
else:
|
| 13910 |
kshitij.so |
164 |
print outer_query
|
| 13803 |
kshitij.so |
165 |
direct = direction
|
| 13910 |
kshitij.so |
166 |
if sort == "bestSellerPoints":
|
|
|
167 |
print "yes,sorting by bestSellerPoints"
|
|
|
168 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('bestSellerPoints',direct),('rank',direct),('nlcPoints',direct)]).skip(offset).limit(limit))
|
|
|
169 |
else:
|
|
|
170 |
data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([(sort,direct)]).skip(offset).limit(limit))
|
| 13771 |
kshitij.so |
171 |
for d in data:
|
|
|
172 |
item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
|
|
|
173 |
if not deals.has_key(item[0]['identifier']):
|
| 13921 |
kshitij.so |
174 |
item[0]['dealRank'] = rank
|
|
|
175 |
try:
|
|
|
176 |
cashBack = __getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
|
|
|
177 |
if not cashBack or cashBack.get('cash_back_status')!=1:
|
|
|
178 |
item[0]['cashBackAmount'] = 0.0
|
|
|
179 |
else:
|
|
|
180 |
if cashBack.get('cash_back_type') ==1:
|
|
|
181 |
cashBackAmount = round(item[0]['available_price'] * (cashBack.get('cash_back'))/100)
|
|
|
182 |
else:
|
|
|
183 |
cashBackAmount = round(cashBack.get('cash_back'))
|
|
|
184 |
item[0]['cashBackAmount'] = cashBackAmount
|
|
|
185 |
except:
|
|
|
186 |
print "Error in adding cashback to deals"
|
|
|
187 |
item[0]['cashBackAmount'] = 0.0
|
| 13771 |
kshitij.so |
188 |
deals[item[0]['identifier']] = item[0]
|
| 13921 |
kshitij.so |
189 |
|
| 13771 |
kshitij.so |
190 |
rank +=1
|
| 13785 |
kshitij.so |
191 |
return sorted(deals.values(), key=itemgetter('dealRank'))
|
|
|
192 |
|
|
|
193 |
def getItem(skuId):
|
| 13795 |
kshitij.so |
194 |
try:
|
|
|
195 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
|
| 13785 |
kshitij.so |
196 |
return skuData
|
| 13795 |
kshitij.so |
197 |
except:
|
|
|
198 |
return [{}]
|
| 13836 |
kshitij.so |
199 |
|
| 13921 |
kshitij.so |
200 |
def __getCashBack(skuId, source_id, category_id):
|
|
|
201 |
itemCashBack = itemCashBackMap.get(skuId)
|
|
|
202 |
if itemCashBack is not None:
|
|
|
203 |
return itemCashBack
|
|
|
204 |
|
|
|
205 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
206 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
207 |
for cashBack in sourceCashBack:
|
|
|
208 |
if cashBack.get(category_id) is None:
|
|
|
209 |
continue
|
|
|
210 |
else:
|
|
|
211 |
return cashBack.get(category_id)
|
|
|
212 |
else:
|
|
|
213 |
return {}
|
|
|
214 |
|
| 13836 |
kshitij.so |
215 |
def getCashBackDetails(identifier, source_id):
|
| 13921 |
kshitij.so |
216 |
if (not bool(cashBackMap)) or (not bool(itemCashBackMap)):
|
|
|
217 |
populateCashBack()
|
|
|
218 |
|
| 13836 |
kshitij.so |
219 |
"""Need to add item level cashback, no data available right now."""
|
| 13771 |
kshitij.so |
220 |
|
| 13921 |
kshitij.so |
221 |
if source_id in (1,2,4,5):
|
| 13839 |
kshitij.so |
222 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
|
| 13840 |
kshitij.so |
223 |
elif source_id == 3:
|
| 13839 |
kshitij.so |
224 |
skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
|
| 13840 |
kshitij.so |
225 |
else:
|
|
|
226 |
return {}
|
| 13836 |
kshitij.so |
227 |
if len(skuData) > 0:
|
| 13921 |
kshitij.so |
228 |
|
|
|
229 |
itemCashBack = itemCashBackMap.get(skuData[0]['_id'])
|
|
|
230 |
if itemCashBack is not None:
|
|
|
231 |
return itemCashBack
|
|
|
232 |
|
|
|
233 |
sourceCashBack = cashBackMap.get(source_id)
|
|
|
234 |
if sourceCashBack is not None and len(sourceCashBack) > 0:
|
|
|
235 |
for cashBack in sourceCashBack:
|
|
|
236 |
if cashBack.get(skuData[0]['category_id']) is None:
|
|
|
237 |
continue
|
|
|
238 |
else:
|
|
|
239 |
return cashBack.get(skuData[0]['category_id'])
|
| 13836 |
kshitij.so |
240 |
else:
|
|
|
241 |
return {}
|
|
|
242 |
else:
|
|
|
243 |
return {}
|
| 13785 |
kshitij.so |
244 |
|
| 13836 |
kshitij.so |
245 |
|
|
|
246 |
|
| 13771 |
kshitij.so |
247 |
|
| 13572 |
kshitij.so |
248 |
def main():
|
| 13921 |
kshitij.so |
249 |
for x in getDeals(0, 0, 20, None, -1):
|
|
|
250 |
print x['source_id']
|
|
|
251 |
print x['category_id']
|
|
|
252 |
print x['available_price']
|
|
|
253 |
print x['cashBackAmount']
|
|
|
254 |
print "===================="
|
| 13811 |
kshitij.so |
255 |
|
| 13921 |
kshitij.so |
256 |
|
| 13572 |
kshitij.so |
257 |
if __name__=='__main__':
|
|
|
258 |
main()
|