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