Subversion Repositories SmartDukaan

Rev

Rev 20172 | Details | Compare with Previous | Last modification | View Log | RSS feed

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