| 14047 |
kshitij.so |
1 |
from elixir import *
|
|
|
2 |
import pymongo
|
|
|
3 |
from dtr.storage.MemCache import MemCache
|
|
|
4 |
import xlwt
|
|
|
5 |
from dtr.storage import DataService
|
|
|
6 |
from dtr.storage.DataService import brand_preferences, user_actions, price_preferences
|
|
|
7 |
from email.mime.multipart import MIMEMultipart
|
|
|
8 |
import email.encoders
|
|
|
9 |
import smtplib
|
|
|
10 |
from datetime import datetime
|
|
|
11 |
|
|
|
12 |
mc = MemCache()
|
|
|
13 |
con =None
|
|
|
14 |
DataService.initialize(db_hostname='localhost')
|
|
|
15 |
|
|
|
16 |
class __SkuInfo:
|
|
|
17 |
|
|
|
18 |
def __init__(self, _id, skuBundleId, category_id, mrp, available_price, source_id, rank, maxNlc, minNlc, schemeAmount, minDiscount, \
|
| 14100 |
kshitij.so |
19 |
maxDiscount, discountType, dp, nlcPoints, bestSellerPoints, totalPoints,in_stock,maxprice,showDeal,persPoints, \
|
|
|
20 |
fav_weight,brand_weight,asp_weight):
|
| 14047 |
kshitij.so |
21 |
self._id = _id
|
|
|
22 |
self.skuBundleId = skuBundleId
|
|
|
23 |
self.category_id = category_id
|
|
|
24 |
self.mrp = mrp
|
|
|
25 |
self.available_price = available_price
|
|
|
26 |
self.source_id = source_id
|
|
|
27 |
self.rank = rank
|
|
|
28 |
self.maxNlc = maxNlc
|
|
|
29 |
self.minNlc = minNlc
|
|
|
30 |
self.schemeAmount = schemeAmount
|
|
|
31 |
self.minDiscount = minDiscount
|
|
|
32 |
self.maxDiscount = maxDiscount
|
|
|
33 |
self.discountType = discountType
|
|
|
34 |
self.dp = dp
|
|
|
35 |
self.nlcPoints = nlcPoints
|
|
|
36 |
self.bestSellerPoints = bestSellerPoints
|
|
|
37 |
self.totalPoints = totalPoints
|
|
|
38 |
self.in_stock = in_stock
|
|
|
39 |
self.maxprice = maxprice
|
|
|
40 |
self.showDeal = showDeal
|
|
|
41 |
self.persPoints = persPoints
|
| 14100 |
kshitij.so |
42 |
self.fav_weight = fav_weight
|
|
|
43 |
self.brand_weight = brand_weight
|
|
|
44 |
self.asp_weight = asp_weight
|
|
|
45 |
|
| 14047 |
kshitij.so |
46 |
|
|
|
47 |
def get_mongo_connection(host='localhost', port=27017):
|
|
|
48 |
global con
|
|
|
49 |
if con is None:
|
|
|
50 |
print "Establishing connection %s host and port %d" %(host,port)
|
|
|
51 |
try:
|
|
|
52 |
con = pymongo.MongoClient(host, port)
|
|
|
53 |
except Exception, e:
|
|
|
54 |
print e
|
|
|
55 |
return None
|
|
|
56 |
return con
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
def generateSheet(userId):
|
|
|
60 |
"Generating user specific deals for ",userId
|
|
|
61 |
outer_query = []
|
|
|
62 |
outer_query.append({"showDeal":1})
|
|
|
63 |
query = {}
|
|
|
64 |
query['$gt'] = 0
|
|
|
65 |
outer_query.append({'totalPoints':query})
|
|
|
66 |
brandPrefMap = {}
|
|
|
67 |
pricePrefMap = {}
|
|
|
68 |
actionsMap = {}
|
|
|
69 |
brand_p = session.query(price_preferences).filter_by(user_id=userId).all()
|
|
|
70 |
for x in brand_p:
|
|
|
71 |
pricePrefMap[x.category_id] = [x.min_price,x.max_price]
|
|
|
72 |
for x in session.query(brand_preferences).filter_by(user_id=userId).all():
|
|
|
73 |
temp_map = {}
|
|
|
74 |
if brandPrefMap.has_key((x.brand).strip().upper()):
|
|
|
75 |
val = brandPrefMap.get((x.brand).strip().upper())
|
|
|
76 |
temp_map[x.category_id] = 1 if x.status == 'show' else 0
|
|
|
77 |
val.append(temp_map)
|
|
|
78 |
else:
|
|
|
79 |
temp = []
|
|
|
80 |
temp_map[x.category_id] = 1 if x.status == 'show' else 0
|
|
|
81 |
temp.append(temp_map)
|
|
|
82 |
brandPrefMap[(x.brand).strip().upper()] = temp
|
|
|
83 |
|
|
|
84 |
for x in session.query(user_actions).filter_by(user_id=userId).all():
|
|
|
85 |
actionsMap[x.store_product_id] = 1 if x.action == 'like' else 0
|
|
|
86 |
all_deals = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
|
|
|
87 |
all_category_deals = []
|
|
|
88 |
for deal in all_deals:
|
|
|
89 |
if actionsMap.get(deal['_id']) == 0:
|
|
|
90 |
fav_weight =.25
|
|
|
91 |
elif actionsMap.get(deal['_id']) == 1:
|
|
|
92 |
fav_weight = 1.5
|
|
|
93 |
else:
|
|
|
94 |
fav_weight = 1
|
|
|
95 |
|
|
|
96 |
if brandPrefMap.get(deal['brand'].strip().upper()) is not None:
|
|
|
97 |
brand_weight = 1
|
|
|
98 |
for brandInfo in brandPrefMap.get(deal['brand'].strip().upper()):
|
|
|
99 |
if brandInfo.get(deal['category_id']) is not None:
|
|
|
100 |
if brandInfo.get(deal['category_id']) == 1:
|
| 14054 |
kshitij.so |
101 |
brand_weight = 2.0
|
| 14047 |
kshitij.so |
102 |
else:
|
|
|
103 |
brand_weight = 1
|
|
|
104 |
|
|
|
105 |
if pricePrefMap.get(deal['category_id']) is not None:
|
|
|
106 |
|
|
|
107 |
if deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1]:
|
|
|
108 |
asp_weight = 1.5
|
|
|
109 |
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]:
|
|
|
110 |
asp_weight = 1.2
|
|
|
111 |
else:
|
|
|
112 |
asp_weight = 1
|
|
|
113 |
else:
|
|
|
114 |
asp_weight = 1
|
|
|
115 |
|
|
|
116 |
persPoints = deal['totalPoints'] * fav_weight * brand_weight * asp_weight
|
| 14100 |
kshitij.so |
117 |
|
|
|
118 |
deal['fav_weight'] = fav_weight
|
|
|
119 |
deal['brand_weight'] = brand_weight
|
|
|
120 |
deal['asp_weight'] = asp_weight
|
| 14047 |
kshitij.so |
121 |
deal['persPoints'] = persPoints
|
|
|
122 |
|
|
|
123 |
all_category_deals.append(deal)
|
|
|
124 |
|
|
|
125 |
|
|
|
126 |
sorted_deals = sorted(all_category_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
|
|
|
127 |
p = []
|
|
|
128 |
for x in sorted_deals:
|
|
|
129 |
s_info = __SkuInfo(x['_id'], x['skuBundleId'], x['category_id'],x['mrp'],x['available_price'],x['source_id'],x['rank'],x['maxNlc'], \
|
|
|
130 |
x['minNlc'], x['schemeAmount'],x['minDiscount'],x['maxDiscount'],x['discountType'],x['dp'],x['nlcPoints'],x['bestSellerPoints'], \
|
| 14100 |
kshitij.so |
131 |
x['totalPoints'],x['in_stock'],x['maxprice'],x['showDeal'],x['persPoints'],deal['fav_weight'],deal['brand_weight'],deal['asp_weight'])
|
| 14047 |
kshitij.so |
132 |
p.append(s_info)
|
|
|
133 |
|
|
|
134 |
|
|
|
135 |
wbk = xlwt.Workbook()
|
|
|
136 |
sheet = wbk.add_sheet('User Deals')
|
|
|
137 |
xstr = lambda s: s or ""
|
|
|
138 |
heading_xf = xlwt.easyxf('font: bold on; align: wrap off, vert centre, horiz center')
|
|
|
139 |
|
|
|
140 |
excel_integer_format = '0'
|
|
|
141 |
integer_style = xlwt.XFStyle()
|
|
|
142 |
integer_style.num_format_str = excel_integer_format
|
|
|
143 |
|
|
|
144 |
sheet.write(0, 0, "Item ID", heading_xf)
|
|
|
145 |
sheet.write(0, 1, "Catalog Item Id", heading_xf)
|
|
|
146 |
sheet.write(0, 2, "Category", heading_xf)
|
|
|
147 |
sheet.write(0, 3, "Brand", heading_xf)
|
|
|
148 |
sheet.write(0, 4, "Product Name", heading_xf)
|
|
|
149 |
sheet.write(0, 5, "Item Status", heading_xf)
|
|
|
150 |
sheet.write(0, 6, "Stock Status", heading_xf)
|
|
|
151 |
sheet.write(0, 7, "MarketPlace", heading_xf)
|
|
|
152 |
sheet.write(0, 8, "MarketPlace Identifier", heading_xf)
|
|
|
153 |
sheet.write(0, 9, "MarketPlace Secondary Identifier", heading_xf)
|
|
|
154 |
sheet.write(0, 10, "Product Name (Marketplace)", heading_xf)
|
|
|
155 |
sheet.write(0, 11, "Url", heading_xf)
|
|
|
156 |
sheet.write(0, 12, "Price", heading_xf)
|
|
|
157 |
sheet.write(0, 13, "Mrp", heading_xf)
|
|
|
158 |
sheet.write(0, 14, "DP", heading_xf)
|
|
|
159 |
sheet.write(0, 15, "Scheme Amount", heading_xf)
|
|
|
160 |
sheet.write(0, 16, "Discount Type", heading_xf)
|
|
|
161 |
sheet.write(0, 17, "Min Discount", heading_xf)
|
|
|
162 |
sheet.write(0, 18, "Max Discount", heading_xf)
|
|
|
163 |
sheet.write(0, 19, "Max Nlc", heading_xf)
|
|
|
164 |
sheet.write(0, 20, "Min Nlc", heading_xf)
|
|
|
165 |
sheet.write(0, 21, "Max Price", heading_xf)
|
|
|
166 |
sheet.write(0, 22, "Rank", heading_xf)
|
|
|
167 |
sheet.write(0, 23, "Nlc Points", heading_xf)
|
|
|
168 |
sheet.write(0, 24, "Best Seller Points", heading_xf)
|
|
|
169 |
sheet.write(0, 25, "Total Points", heading_xf)
|
|
|
170 |
sheet.write(0, 26, "Pers Points", heading_xf)
|
|
|
171 |
sheet.write(0, 27, "Show Deals", heading_xf)
|
| 14100 |
kshitij.so |
172 |
sheet.write(0, 28, "Fav Weight", heading_xf)
|
|
|
173 |
sheet.write(0, 29, "Brand Weight", heading_xf)
|
|
|
174 |
sheet.write(0, 30, "Asp Weight", heading_xf)
|
| 14047 |
kshitij.so |
175 |
|
|
|
176 |
it = 1
|
|
|
177 |
for x in p:
|
|
|
178 |
y = list(get_mongo_connection().Catalog.MasterData.find({'_id':x._id}))
|
|
|
179 |
sheet.write(it, 0, x._id)
|
|
|
180 |
sheet.write(it, 1, x.skuBundleId)
|
|
|
181 |
if x.category_id == 3:
|
|
|
182 |
category = 'Mobiles'
|
|
|
183 |
else:
|
|
|
184 |
category = 'Tablets'
|
|
|
185 |
sheet.write(it, 2, category)
|
|
|
186 |
sheet.write(it, 3, y[0]['brand'])
|
|
|
187 |
sheet.write(it, 4, y[0]['product_name'])
|
|
|
188 |
if y[0]['status'] ==1:
|
|
|
189 |
sheet.write(it, 5, 'Active')
|
|
|
190 |
else:
|
|
|
191 |
sheet.write(it, 5, 'EOL')
|
|
|
192 |
if y[0]['in_stock'] ==1:
|
|
|
193 |
sheet.write(it, 6, 'In Stock')
|
|
|
194 |
else:
|
|
|
195 |
sheet.write(it, 6, 'Out Of Stock')
|
|
|
196 |
sheet.write(it, 7, y[0]['source'])
|
|
|
197 |
sheet.write(it, 8, y[0]['identifier'])
|
|
|
198 |
sheet.write(it, 9, y[0]['secondaryIdentifier'])
|
|
|
199 |
sheet.write(it, 10, y[0]['source_product_name'])
|
|
|
200 |
sheet.write(it, 11, y[0]['url'])
|
|
|
201 |
sheet.write(it, 12, y[0]['available_price'])
|
|
|
202 |
sheet.write(it, 13, x.mrp)
|
|
|
203 |
sheet.write(it, 14, x.dp)
|
|
|
204 |
sheet.write(it, 15, x.schemeAmount)
|
|
|
205 |
sheet.write(it, 16, x.discountType)
|
|
|
206 |
sheet.write(it, 17, x.minDiscount)
|
|
|
207 |
sheet.write(it, 18, x.maxDiscount)
|
|
|
208 |
sheet.write(it, 19, x.maxNlc)
|
|
|
209 |
sheet.write(it, 20, x.minNlc)
|
|
|
210 |
sheet.write(it, 21, x.maxprice)
|
|
|
211 |
sheet.write(it, 22, x.rank)
|
|
|
212 |
sheet.write(it, 23, x.nlcPoints)
|
|
|
213 |
sheet.write(it, 24, x.bestSellerPoints)
|
|
|
214 |
sheet.write(it, 25, x.totalPoints)
|
|
|
215 |
sheet.write(it, 26, x.persPoints)
|
|
|
216 |
sheet.write(it, 27, x.showDeal)
|
| 14100 |
kshitij.so |
217 |
sheet.write(it, 28, x.fav_weight)
|
|
|
218 |
sheet.write(it, 29, x.brand_weight)
|
|
|
219 |
sheet.write(it, 30, x.asp_weight)
|
| 14047 |
kshitij.so |
220 |
it+=1
|
|
|
221 |
filename = "/tmp/deal-data-user-specific"+str(datetime.now())+".xls"
|
|
|
222 |
wbk.save(filename)
|
|
|
223 |
smtpServer = smtplib.SMTP('localhost')
|
|
|
224 |
#smtpServer.set_debuglevel(1)
|
|
|
225 |
sender = 'dtr@shop2020.in'
|
| 14101 |
kshitij.so |
226 |
if userId==27:
|
|
|
227 |
recipients = ['kshitij.sood@saholic.com']
|
|
|
228 |
else:
|
|
|
229 |
recipients = ['rajneesh.arora@saholic.com','kshitij.sood@saholic.com']
|
| 14047 |
kshitij.so |
230 |
msg = MIMEMultipart()
|
|
|
231 |
msg['Subject'] = "DTR Deals data (UserOnly) UserId - "+str(userId)+"" + ' - ' + str(datetime.now())
|
|
|
232 |
msg['From'] = sender
|
|
|
233 |
msg['To'] = ",".join(recipients)
|
|
|
234 |
fileMsg = email.mime.base.MIMEBase('application','vnd.ms-excel')
|
|
|
235 |
fileMsg.set_payload(file(filename).read())
|
|
|
236 |
email.encoders.encode_base64(fileMsg)
|
| 14048 |
kshitij.so |
237 |
fileMsg.add_header('Content-Disposition','attachment;filename=User_Deal_Sheet_Dtr.xls')
|
| 14047 |
kshitij.so |
238 |
msg.attach(fileMsg)
|
|
|
239 |
try:
|
|
|
240 |
smtpServer.sendmail(sender, recipients, msg.as_string())
|
|
|
241 |
print "Successfully sent email"
|
|
|
242 |
except:
|
|
|
243 |
print "Error: unable to send email."
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
|
247 |
def main():
|
|
|
248 |
generateSheet(1)
|
|
|
249 |
|
|
|
250 |
if __name__=='__main__':
|
|
|
251 |
main()
|