Subversion Repositories SmartDukaan

Rev

Rev 15376 | Rev 15459 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
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, \
15074 kshitij.so 8
    user_actions, Brands
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
14791 kshitij.so 15
import traceback
15375 kshitij.so 16
import random
13572 kshitij.so 17
 
18
con = None
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"
38
    cashBackMap = {}
39
    itemCashBackMap = {}
13907 kshitij.so 40
    cashBack = list(get_mongo_connection().Catalog.CategoryCashBack.find())
41
    for row in cashBack:
13970 kshitij.so 42
        temp_map = {}
43
        temp_list = []
13907 kshitij.so 44
        if cashBackMap.has_key(row['source_id']):
45
            arr = cashBackMap.get(row['source_id'])
46
            for val in arr:
47
                temp_list.append(val)
48
            temp_map[row['category_id']] = row
49
            temp_list.append(temp_map)
50
            cashBackMap[row['source_id']] = temp_list 
51
        else:
52
            temp_map[row['category_id']] = row
53
            temp_list.append(temp_map)
54
            cashBackMap[row['source_id']] = temp_list
13921 kshitij.so 55
    itemCashBack = list(get_mongo_connection().Catalog.ItemCashBack.find())
56
    for row in itemCashBack:
57
        if not itemCashBackMap.has_key(row['skuId']):
58
            itemCashBackMap[row['skuId']] = row
14761 kshitij.so 59
    mc.set("item_cash_back", itemCashBackMap, 24 * 60 * 60)
60
    mc.set("category_cash_back", cashBackMap, 24 * 60 * 60)
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"}
13572 kshitij.so 211
 
13970 kshitij.so 212
def getAllExceptionlNlcItems(offset, limit):
13572 kshitij.so 213
    data = []
13970 kshitij.so 214
    collection = get_mongo_connection().Catalog.ExceptionalNlc
14071 kshitij.so 215
    cursor = collection.find().skip(offset).limit(limit)
13572 kshitij.so 216
    for val in cursor:
13970 kshitij.so 217
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
218
        if len(master) > 0:
14069 kshitij.so 219
            val['brand'] = master[0]['brand']
220
            val['source_product_name'] = master[0]['source_product_name']
221
            val['skuBundleId'] = master[0]['skuBundleId']
13970 kshitij.so 222
        else:
223
            val['brand'] = ""
224
            val['source_product_name'] = ""
225
            val['skuBundleId'] = ""
13572 kshitij.so 226
        data.append(val)
227
    return data
228
 
14005 amit.gupta 229
def getMerchantOrdersByUser(userId, page=1, window=50, searchMap={}):
230
    if searchMap is None:
231
        searchMap = {}
13603 amit.gupta 232
    if page==None:
233
        page = 1
234
 
235
    if window==None:
236
        window = 50
237
    result = {}
13582 amit.gupta 238
    skip = (page-1)*window
14005 amit.gupta 239
 
14353 amit.gupta 240
    if userId is not None:
241
        searchMap['userId'] = userId
13603 amit.gupta 242
    collection = get_mongo_connection().Dtr.merchantOrder
14609 amit.gupta 243
    cursor = collection.find(searchMap).sort("orderId",-1)
13603 amit.gupta 244
    total_count = cursor.count()
245
    pages = total_count/window + (0 if total_count%window==0 else 1)  
246
    print "total_count", total_count
247
    if total_count > skip:
13999 amit.gupta 248
        cursor = cursor.skip(skip).limit(window)
13603 amit.gupta 249
        orders = []
250
        for order in cursor:
14002 amit.gupta 251
            del(order["_id"])
252
            orders.append(order)
13603 amit.gupta 253
        result['data'] = orders
254
        result['window'] = window
255
        result['totalCount'] = total_count 
256
        result['currCount'] = cursor.count()
257
        result['totalPages'] = pages
258
        result['currPage'] = page    
259
        return result
260
    else:
261
        return result
13630 kshitij.so 262
 
13927 amit.gupta 263
def getRefunds(userId, page=1, window=10):
264
    if page==None:
265
        page = 1
266
 
267
    if window==None:
13995 amit.gupta 268
        window = 10
13927 amit.gupta 269
    result = {}
270
    skip = (page-1)*window
271
    collection = get_mongo_connection().Dtr.refund
272
    cursor = collection.find({"userId":userId})
273
    total_count = cursor.count()
274
    pages = total_count/window + (0 if total_count%window==0 else 1)  
275
    print "total_count", total_count
276
    if total_count > skip:
14668 amit.gupta 277
        cursor = cursor.skip(skip).limit(window).sort([('batch',-1)])
13927 amit.gupta 278
        refunds = []
279
        for refund in cursor:
280
            del(refund["_id"])
281
            refunds.append(refund)
282
        result['data'] = refunds
283
        result['window'] = window
284
        result['totalCount'] = total_count 
285
        result['currCount'] = cursor.count()
286
        result['totalPages'] = pages
287
        result['currPage'] = page    
288
        return result
289
    else:
290
        return result
291
 
292
def getPendingRefunds(userId):
13991 amit.gupta 293
    print type(userId)
13927 amit.gupta 294
    result = get_mongo_connection().Dtr.merchantOrder\
295
        .aggregate([
296
                    {'$match':{'subOrders.cashBackStatus':Store.CB_APPROVED, 'userId':userId}},
297
                    {'$unwind':"$subOrders"},
14670 amit.gupta 298
                    {'$match':{'subOrders.cashBackStatus':Store.CB_APPROVED}},
13927 amit.gupta 299
                    { 
300
                     '$group':{
301
                               '_id':None,
302
                               'amount': { '$sum':'$subOrders.cashBackAmount'},
303
                               }
304
                     }
13987 amit.gupta 305
                ])['result']
306
 
307
    if len(result)>0:
308
        result = result[0]        
309
        result.pop("_id")
310
    else:
311
        result={}
312
        result['amount'] = 0.0
14305 amit.gupta 313
    result['nextCredit'] = datetime.strftime(next_weekday(datetime.now(), int(PythonPropertyReader.getConfig('CREDIT_DAY_OF_WEEK'))),"%Y-%m-%d %H:%M:%S")
13927 amit.gupta 314
    return result
14037 kshitij.so 315
 
14671 amit.gupta 316
def getPendingCashbacks(userId):
317
    result = get_mongo_connection().Dtr.merchantOrder\
318
        .aggregate([
319
                    {'$match':{'subOrders.cashBackStatus':Store.CB_PENDING, 'userId':userId}},
320
                    {'$unwind':"$subOrders"},
321
                    {'$match':{'subOrders.cashBackStatus':Store.CB_PENDING}},
322
                    { 
323
                     '$group':{
324
                               '_id':None,
325
                               'amount': { '$sum':'$subOrders.cashBackAmount'},
326
                               }
327
                     }
328
                ])['result']
329
 
330
    if len(result)>0:
331
        result = result[0]        
332
        result.pop("_id")
333
    else:
334
        result={}
335
        result['amount'] = 0.0
336
    return result
337
 
14037 kshitij.so 338
def __populateCache(userId):
339
    print "Populating memcache for userId",userId
340
    outer_query = []
341
    outer_query.append({"showDeal":1})
342
    query = {}
15095 kshitij.so 343
    query['$gte'] = -100
14037 kshitij.so 344
    outer_query.append({'totalPoints':query})
345
    brandPrefMap = {}
346
    pricePrefMap = {}
347
    actionsMap = {}
348
    brand_p = session.query(price_preferences).filter_by(user_id=userId).all()
349
    for x in brand_p:
350
        pricePrefMap[x.category_id] = [x.min_price,x.max_price]
351
    for x in session.query(brand_preferences).filter_by(user_id=userId).all():
352
        temp_map = {}
353
        if brandPrefMap.has_key((x.brand).strip().upper()):
354
            val = brandPrefMap.get((x.brand).strip().upper())
355
            temp_map[x.category_id] = 1 if x.status == 'show' else 0
356
            val.append(temp_map)
357
        else:
358
            temp = []
359
            temp_map[x.category_id] = 1 if x.status == 'show' else 0
360
            temp.append(temp_map)
361
            brandPrefMap[(x.brand).strip().upper()] = temp
362
 
363
    for x in session.query(user_actions).filter_by(user_id=userId).all():
364
        actionsMap[x.store_product_id] = 1 if x.action == 'like' else 0
15345 kshitij.so 365
    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,'brand_id':1,'skuBundleId':1}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
14037 kshitij.so 366
    all_category_deals = []
367
    mobile_deals = []
368
    tablet_deals = []
369
    for deal in all_deals:
370
        if actionsMap.get(deal['_id']) == 0:
371
            fav_weight =.25
372
        elif actionsMap.get(deal['_id']) == 1:
373
            fav_weight = 1.5
374
        else:
375
            fav_weight = 1
376
 
377
        if brandPrefMap.get(deal['brand'].strip().upper()) is not None:
378
            brand_weight = 1
379
            for brandInfo in brandPrefMap.get(deal['brand'].strip().upper()):
380
                if brandInfo.get(deal['category_id']) is not None:
381
                    if brandInfo.get(deal['category_id']) == 1:
14055 kshitij.so 382
                        brand_weight = 2.0
14037 kshitij.so 383
        else:
384
            brand_weight = 1
385
 
386
        if pricePrefMap.get(deal['category_id']) is not None:
387
 
388
            if deal['available_price'] >= pricePrefMap.get(deal['category_id'])[0] and deal['available_price'] <= pricePrefMap.get(deal['category_id'])[1]:
389
                asp_weight = 1.5
390
            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]:
391
                asp_weight = 1.2
392
            else:
393
                asp_weight = 1
394
        else:
395
            asp_weight = 1
396
 
397
        persPoints = deal['totalPoints'] * fav_weight * brand_weight * asp_weight
398
        deal['persPoints'] = persPoints
399
 
400
        if deal['category_id'] ==3:
401
            mobile_deals.append(deal)
402
        elif deal['category_id'] ==5:
403
            tablet_deals.append(deal)
404
        else:
405
            continue
406
        all_category_deals.append(deal)
407
 
14144 kshitij.so 408
    session.close()
14037 kshitij.so 409
    mem_cache_val = {3:mobile_deals, 5:tablet_deals, 0:all_deals}
410
    mc.set(str(userId), mem_cache_val)
411
 
412
 
14531 kshitij.so 413
def __populateFeaturedDeals():
414
    all_category_fd = []
415
    mobile_fd = []
416
    tablet_fd = []
14550 kshitij.so 417
    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 418
    for activeFeaturedDeal in activeFeaturedDeals:
419
        for k,v in activeFeaturedDeal['rankDetails']:
420
            featuredDeal = FeaturedDeals(activeFeaturedDeal['sku'], int(k), activeFeaturedDeal['thresholdPrice'], int(v))
421
            if featuredDeal.category_id == 0:
422
                all_category_fd.append(featuredDeal)
423
            elif featuredDeal.category_id == 3:
424
                mobile_fd.append(featuredDeal)
425
            elif featuredDeal.category_id == 5:
426
                tablet_fd.append(featuredDeal)
427
            else:
428
                continue
14550 kshitij.so 429
    mc.set("featured_deals_category_"+str(0), all_category_fd, 3600)
430
    mc.set("featured_deals_category_"+str(3), mobile_fd, 3600)
431
    mc.set("featured_deals_category_"+str(5), tablet_fd, 3600)
14037 kshitij.so 432
 
14791 kshitij.so 433
def getNewDeals(userId, category_id, offset, limit, sort, direction, filterData=None):
14761 kshitij.so 434
    if not bool(mc.get("category_cash_back")):
13921 kshitij.so 435
        populateCashBack()
14037 kshitij.so 436
 
14550 kshitij.so 437
    try:
438
        if mc.get("featured_deals_category_"+str(category_id)) is None:
439
            __populateFeaturedDeals()
440
    except:
441
        pass 
14531 kshitij.so 442
 
13771 kshitij.so 443
    rank = 1
14037 kshitij.so 444
    dealsMap = {}
445
    user_specific_deals = mc.get(str(userId))
446
    if user_specific_deals is None:
447
        __populateCache(userId)
448
        user_specific_deals = mc.get(str(userId))
14038 kshitij.so 449
    else:
450
        print "Getting user deals from cache"
14037 kshitij.so 451
    category_specific_deals = user_specific_deals.get(category_id)
452
 
453
    if sort is None or direction is None:
454
        sorted_deals = sorted(category_specific_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
455
    else:
456
        if sort == "bestSellerPoints":
457
            sorted_deals = sorted(category_specific_deals, key = lambda x: (x['bestSellerPoints'], x['rank'], x['nlcPoints']),reverse=True)
458
        else:
459
            if direction == -1:
460
                rev = True
461
            else:
462
                rev = False
463
            sorted_deals = sorted(category_specific_deals, key = lambda x: (x['available_price']),reverse=rev)
464
 
14791 kshitij.so 465
 
466
    print "============================"
467
    if filterData is not None:
468
        try:
469
            sorted_deals = filterDeals(sorted_deals, filterData)
470
        except:
15095 kshitij.so 471
            traceback.print_exc()    
14791 kshitij.so 472
 
14037 kshitij.so 473
    for d in sorted_deals[offset:offset+limit]:
474
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
475
        if not dealsMap.has_key(item[0]['identifier']):
476
            item[0]['dealRank'] = rank
477
            item[0]['persPoints'] = d['persPoints']
14322 kshitij.so 478
            if d['dealType'] == 1 and d['source_id'] ==1:
15040 kshitij.so 479
                item[0]['marketPlaceUrl'] = "http://www.amazon.in/dp/%s"%(item[0]['identifier'].strip())
480
            elif d['source_id'] ==3:
481
                item[0]['marketPlaceUrl'] = item[0]['marketPlaceUrl']+'?supc='+item[0].get('identifier')
482
            else:
483
                pass 
14037 kshitij.so 484
            try:
14761 kshitij.so 485
                cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
14037 kshitij.so 486
                if not cashBack or cashBack.get('cash_back_status')!=1:
487
                    item[0]['cash_back_type'] = 0
488
                    item[0]['cash_back'] = 0
489
                else:
14766 kshitij.so 490
                    item[0]['cash_back_type'] = int(cashBack['cash_back_type'])
14037 kshitij.so 491
                    item[0]['cash_back'] = cashBack['cash_back']
492
            except:
493
                print "Error in adding cashback to deals"
494
                item[0]['cash_back_type'] = 0
495
                item[0]['cash_back'] = 0
496
            dealsMap[item[0]['identifier']] = item[0]
497
 
498
            rank +=1
499
    return sorted(dealsMap.values(), key=itemgetter('dealRank'))
500
 
14791 kshitij.so 501
 
502
def filterDeals(deals, filterData):
503
    dealFiltered = []
504
    brandsFiltered = []
505
    filterArray = filterData.split('|')
506
    for data in filterArray:
507
        try:
508
            filter, info = data.split(':')
509
        except Exception as ex:
510
            traceback.print_exc()
511
            continue
512
        if filter == 'dealFilter':
513
            toFilter = info.split('^')
514
            print "deal filter ",toFilter
515
            if 'deals' in toFilter:
516
                dealFiltered = deals
517
                continue
518
            for filterVal in toFilter:
519
                if filterVal == 'dod':
520
                    for deal in deals:
521
                        if deal['dealType'] == 1:
522
                            dealFiltered.append(deal)
523
        elif filter == 'brandFilter':
524
            toFilter = info.split('^')
525
            print "brand filter ",toFilter
526
            if len(toFilter) == 0 or (len(toFilter)==1 and toFilter[0]==''):
527
                brandsFiltered = deals
528
            for deal in deals:
15151 kshitij.so 529
                if str(int(deal['brand_id'])) in toFilter:
14791 kshitij.so 530
                    brandsFiltered.append(deal)
15040 kshitij.so 531
    if len(dealFiltered) == 0:
532
        return brandsFiltered
14791 kshitij.so 533
    return [i for i in dealFiltered for j in brandsFiltered if i['_id']==j['_id']]
534
 
535
 
14037 kshitij.so 536
def getDeals(userId, category_id, offset, limit, sort, direction):
14761 kshitij.so 537
    if not bool(mc.get("category_cash_back")):
14037 kshitij.so 538
        populateCashBack()
539
    rank = 1
13771 kshitij.so 540
    deals = {}
13910 kshitij.so 541
    outer_query = []
542
    outer_query.append({"showDeal":1})
543
    query = {}
544
    query['$gt'] = 0
545
    outer_query.append({'totalPoints':query})
546
    if category_id in (3,5):
547
        outer_query.append({'category_id':category_id})
13803 kshitij.so 548
    if sort is None or direction is None:
549
        direct = -1
13910 kshitij.so 550
        print outer_query
551
        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 552
    else:
13910 kshitij.so 553
        print outer_query
13803 kshitij.so 554
        direct = direction
13910 kshitij.so 555
        if sort == "bestSellerPoints":
556
            print "yes,sorting by bestSellerPoints"
557
            data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([('bestSellerPoints',direct),('rank',direct),('nlcPoints',direct)]).skip(offset).limit(limit))
558
        else:
559
            data = list(get_mongo_connection().Catalog.Deals.find({"$and":outer_query}).sort([(sort,direct)]).skip(offset).limit(limit))
13771 kshitij.so 560
    for d in data:
561
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
562
        if not deals.has_key(item[0]['identifier']):
13921 kshitij.so 563
            item[0]['dealRank'] = rank
564
            try:
14761 kshitij.so 565
                cashBack = getCashBack(item[0]['_id'], item[0]['source_id'], item[0]['category_id'])
13921 kshitij.so 566
                if not cashBack or cashBack.get('cash_back_status')!=1:
13928 kshitij.so 567
                    item[0]['cash_back_type'] = 0
568
                    item[0]['cash_back'] = 0
13921 kshitij.so 569
                else:
14766 kshitij.so 570
                    item[0]['cash_back_type'] = int(cashBack['cash_back_type'])
13928 kshitij.so 571
                    item[0]['cash_back'] = cashBack['cash_back']
13921 kshitij.so 572
            except:
573
                print "Error in adding cashback to deals"
13928 kshitij.so 574
                item[0]['cash_back_type'] = 0
575
                item[0]['cash_back'] = 0
13771 kshitij.so 576
            deals[item[0]['identifier']] = item[0]
13921 kshitij.so 577
 
13771 kshitij.so 578
            rank +=1
13785 kshitij.so 579
    return sorted(deals.values(), key=itemgetter('dealRank'))
580
 
581
def getItem(skuId):
14761 kshitij.so 582
    if not bool(mc.get("category_cash_back")):
14113 kshitij.so 583
        populateCashBack()
13795 kshitij.so 584
    try:
585
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
14107 kshitij.so 586
        for sku in skuData:
587
            try:
14761 kshitij.so 588
                cashBack = getCashBack(sku['_id'], sku['source_id'], sku['category_id'])
14107 kshitij.so 589
                if not cashBack or cashBack.get('cash_back_status')!=1:
590
                    sku['cash_back_type'] = 0
591
                    sku['cash_back'] = 0
592
                else:
593
                    sku['cash_back_type'] = cashBack['cash_back_type']
594
                    sku['cash_back'] = cashBack['cash_back']
595
            except:
596
                print "Error in adding cashback to deals"
597
                sku['cash_back_type'] = 0
598
                sku['cash_back'] = 0
14629 kshitij.so 599
            sku['in_stock'] = int(sku['in_stock'])
600
            sku['is_shortage'] = int(sku['is_shortage'])
601
            sku['category_id'] = int(sku['category_id'])
602
            sku['status'] = int(sku['status'])
13785 kshitij.so 603
        return skuData
13795 kshitij.so 604
    except:
605
        return [{}]
13836 kshitij.so 606
 
14761 kshitij.so 607
def getCashBack(skuId, source_id, category_id):
608
    if not bool(mc.get("category_cash_back")):
609
        populateCashBack()
610
    itemCashBackMap = mc.get("item_cash_back")
13921 kshitij.so 611
    itemCashBack = itemCashBackMap.get(skuId)
612
    if itemCashBack is not None:
613
        return itemCashBack
14761 kshitij.so 614
    cashBackMap = mc.get("category_cash_back")
13921 kshitij.so 615
    sourceCashBack = cashBackMap.get(source_id)
616
    if sourceCashBack is not None and len(sourceCashBack) > 0:
617
        for cashBack in sourceCashBack:
618
            if cashBack.get(category_id) is None:
619
                continue
620
            else:
621
                return cashBack.get(category_id)
622
    else:
623
        return {}
624
 
15129 kshitij.so 625
def getDealRank(identifier, source_id, userId):
626
    if source_id in (1,2,4,5):
627
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
628
    elif source_id == 3:
629
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
630
    else:
15349 kshitij.so 631
        return {'rank':0, 'description':'Source not valid'}
632
    if len(skuData) == 0:
633
        return {'rank':0, 'description':'No matching product identifier found'}
15129 kshitij.so 634
    user_specific_deals = mc.get(str(userId))
635
    if user_specific_deals is None:
636
        __populateCache(userId)
637
        user_specific_deals = mc.get(str(userId))
638
    else:
639
        print "Getting user deals from cache"
640
    category_id = skuData[0]['category_id']
641
    category_specific_deals = user_specific_deals.get(category_id)
642
    if category_specific_deals is None or len(category_specific_deals) ==0:
15347 amit.gupta 643
        return {'rank':0,'description':'Category specific deals is empty'}
15129 kshitij.so 644
    sorted_deals = sorted(category_specific_deals, key = lambda x: (x['persPoints'],x['totalPoints'],x['bestSellerPoints'], x['nlcPoints'], x['rank']),reverse=True)
645
    rank = 1
646
    for sorted_deal in sorted_deals:
15345 kshitij.so 647
        if sorted_deal['skuBundleId'] == skuData[0]['skuBundleId']:
15347 amit.gupta 648
            return {'rank':rank, 'description':'Rank found'}
15129 kshitij.so 649
        rank+=1
15347 amit.gupta 650
    return {'rank':0,'description':'Rank not found'}
15129 kshitij.so 651
 
652
 
13836 kshitij.so 653
def getCashBackDetails(identifier, source_id):
14761 kshitij.so 654
    if not bool(mc.get("category_cash_back")):
13921 kshitij.so 655
        populateCashBack()
656
 
13836 kshitij.so 657
    """Need to add item level cashback, no data available right now."""
13771 kshitij.so 658
 
13921 kshitij.so 659
    if source_id in (1,2,4,5):
13839 kshitij.so 660
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
13840 kshitij.so 661
    elif source_id == 3:
13839 kshitij.so 662
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
13840 kshitij.so 663
    else:
664
        return {}
13836 kshitij.so 665
    if len(skuData) > 0:
14761 kshitij.so 666
        itemCashBackMap = mc.get("item_cash_back")
13921 kshitij.so 667
        itemCashBack = itemCashBackMap.get(skuData[0]['_id'])
668
        if itemCashBack is not None:
669
            return itemCashBack
14761 kshitij.so 670
        cashBackMap = mc.get("category_cash_back")
13921 kshitij.so 671
        sourceCashBack = cashBackMap.get(source_id)
672
        if sourceCashBack is not None and len(sourceCashBack) > 0:
673
            for cashBack in sourceCashBack:
674
                if cashBack.get(skuData[0]['category_id']) is None:
675
                    continue
676
                else:
677
                    return cashBack.get(skuData[0]['category_id'])
13836 kshitij.so 678
        else:
679
            return {} 
680
    else:
681
        return {}
13986 amit.gupta 682
 
14398 amit.gupta 683
def getImgSrc(identifier, source_id):
684
    skuData = None
685
    if source_id in (1,2,4,5):
14414 amit.gupta 686
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'identifier':identifier.strip(), 'source_id':source_id})
14398 amit.gupta 687
    elif source_id == 3:
14414 amit.gupta 688
        skuData = get_mongo_connection().Catalog.MasterData.find_one({'secondaryIdentifier':identifier.strip(), 'source_id':source_id})
14398 amit.gupta 689
    if skuData is None:
690
        return {}
691
    else:
692
        return {'thumbnail':skuData.get('thumbnail')}
13986 amit.gupta 693
 
694
def next_weekday(d, weekday):
695
    days_ahead = weekday - d.weekday()
696
    if days_ahead <= 0: # Target day already happened this week
697
        days_ahead += 7
698
    return d + timedelta(days_ahead)
699
 
13771 kshitij.so 700
 
13970 kshitij.so 701
def getAllDealerPrices(offset, limit):
702
    data = []
703
    collection = get_mongo_connection().Catalog.SkuDealerPrices
704
    cursor = collection.find().skip(offset).limit(limit)
705
    for val in cursor:
706
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
707
        if len(master) > 0:
14069 kshitij.so 708
            val['brand'] = master[0]['brand']
709
            val['source_product_name'] = master[0]['source_product_name']
710
            val['skuBundleId'] = master[0]['skuBundleId']
13970 kshitij.so 711
        else:
712
            val['brand'] = ""
713
            val['source_product_name'] = ""
714
            val['skuBundleId'] = ""
715
        data.append(val)
716
    return data
717
 
14553 kshitij.so 718
def addSkuDealerPrice(data, multi):
719
    if multi != 1:
720
        collection = get_mongo_connection().Catalog.SkuDealerPrices
721
        cursor = collection.find({"sku":data['sku']})
722
        if cursor.count() > 0:
723
            return {0:"Sku information already present."}
724
        else:
725
            collection.insert(data)
726
            get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
727
            return {1:"Data added successfully"}
13970 kshitij.so 728
    else:
14553 kshitij.so 729
        skuIds = __getBundledSkusfromSku(data['sku'])
730
        for sku in skuIds:
731
            data['sku'] = sku.get('_id')
732
            collection = get_mongo_connection().Catalog.SkuDealerPrices
733
            cursor = collection.find({"sku":data['sku']})
734
            if cursor.count() > 0:
735
                continue
736
            else:   
14558 kshitij.so 737
                data.pop('_id',None)
14553 kshitij.so 738
                collection.insert(data)
739
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
13970 kshitij.so 740
        return {1:"Data added successfully"}
14553 kshitij.so 741
 
742
 
13970 kshitij.so 743
 
744
def updateSkuDealerPrice(data, _id):
745
    try:
746
        collection = get_mongo_connection().Catalog.SkuDealerPrices
747
        collection.update({'_id':ObjectId(_id)},{"$set":{'dp':data['dp']}},upsert=False, multi = False)
748
        get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
749
        return {1:"Data updated successfully"}
750
    except:
751
        return {0:"Data not updated."}
752
 
753
def updateExceptionalNlc(data, _id):
754
    try:
755
        collection = get_mongo_connection().Catalog.ExceptionalNlc
756
        collection.update({'_id':ObjectId(_id)},{"$set":{'maxNlc':data['maxNlc'], 'minNlc':data['minNlc'], 'overrideNlc':data['overrideNlc']}},upsert=False, multi = False)
757
        get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
758
        return {1:"Data updated successfully"}
759
    except:
760
        return {0:"Data not updated."}
761
 
14041 kshitij.so 762
def resetCache(userId):
14043 kshitij.so 763
    try:
764
        mc.delete(userId)
765
        return {1:'Cache cleared.'}
766
    except:
767
        return {0:'Unable to clear cache.'}
768
 
14575 kshitij.so 769
def updateCollection(data, multi):
14083 kshitij.so 770
    print data
14575 kshitij.so 771
    if multi!=1:
772
        try:
773
            collection = get_mongo_connection().Catalog[data['class']]
774
            class_name = data.pop('class')
775
            if class_name == "SkuSchemeDetails":
776
                data['addedOn'] = to_java_date(datetime.now())
777
            _id = data.pop('oid')
778
            result = collection.update({'_id':ObjectId(_id)},{"$set":data},upsert=False, multi = False)
15069 kshitij.so 779
            if class_name != "Notifications":
14852 kshitij.so 780
                record = list(collection.find({'_id':ObjectId(_id)}))
781
                if class_name !="CategoryDiscount":
782
                    get_mongo_connection().Catalog.MasterData.update({'_id':record[0]['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}})
783
                else:
784
                    get_mongo_connection().Catalog.MasterData.update({'brand':re.compile(record[0]['brand'], re.IGNORECASE),'category_id':record[0]['category_id']}, \
785
                                                                     {"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False,multi=True)
14575 kshitij.so 786
            return {1:"Data updated successfully"}
787
        except Exception as e:
788
            print e
789
            return {0:"Data not updated."}
790
    else:
791
        try:
792
            collection = get_mongo_connection().Catalog[data['class']]
793
            class_name = data.pop('class')
794
            _id = data.pop('oid')
795
            record = list(collection.find({'_id':ObjectId(_id)}))
796
            skuIds = __getBundledSkusfromSku(record[0]['sku'])
797
            for sku in skuIds:
798
                if class_name == "SkuSchemeDetails":
799
                    data['addedOn'] = to_java_date(datetime.now())
14582 kshitij.so 800
                data['sku'] = sku.get('_id')
14575 kshitij.so 801
                data.pop('_id',None)
14583 kshitij.so 802
                collection.update({'sku':sku.get('_id')},{"$set":data},upsert=False,multi=False)
14575 kshitij.so 803
            get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
14580 kshitij.so 804
            return {1:"Data updatedsuccessfully"}
14575 kshitij.so 805
        except Exception as e:
806
            print e
807
            return {0:"Data not updated."}
808
 
14076 kshitij.so 809
 
14553 kshitij.so 810
def addNegativeDeals(data, multi):
811
    if multi !=1: 
812
        collection = get_mongo_connection().Catalog.NegativeDeals
813
        cursor = collection.find({"sku":data['sku']})
814
        if cursor.count() > 0:
815
            return {0:"Sku information already present."}
816
        else:
817
            collection.insert(data)
818
            return {1:"Data added successfully"}
14481 kshitij.so 819
    else:
14553 kshitij.so 820
        skuIds = __getBundledSkusfromSku(data['sku'])
821
        for sku in skuIds:
822
            data['sku'] = sku.get('_id')
823
            collection = get_mongo_connection().Catalog.NegativeDeals
824
            cursor = collection.find({"sku":data['sku']})
825
            if cursor.count() > 0:
826
                continue
827
            else:
14558 kshitij.so 828
                data.pop('_id',None)
14553 kshitij.so 829
                collection.insert(data)
14481 kshitij.so 830
        return {1:"Data added successfully"}
831
 
832
def getAllNegativeDeals(offset, limit):
833
    data = []
834
    collection = get_mongo_connection().Catalog.NegativeDeals
835
    cursor = collection.find().skip(offset).limit(limit)
836
    for val in cursor:
837
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
838
        if len(master) > 0:
839
            val['brand'] = master[0]['brand']
840
            val['source_product_name'] = master[0]['source_product_name']
841
            val['skuBundleId'] = master[0]['skuBundleId']
842
        else:
843
            val['brand'] = ""
844
            val['source_product_name'] = ""
845
            val['skuBundleId'] = ""
846
        data.append(val)
847
    return data
848
 
849
def getAllManualDeals(offset, limit):
850
    data = []
851
    collection = get_mongo_connection().Catalog.ManualDeals
15090 kshitij.so 852
    cursor = collection.find().skip(offset).limit(limit)
14481 kshitij.so 853
    for val in cursor:
854
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
855
        if len(master) > 0:
856
            val['brand'] = master[0]['brand']
857
            val['source_product_name'] = master[0]['source_product_name']
858
            val['skuBundleId'] = master[0]['skuBundleId']
859
        else:
860
            val['brand'] = ""
861
            val['source_product_name'] = ""
862
            val['skuBundleId'] = ""
863
        data.append(val)
864
    return data
14076 kshitij.so 865
 
14553 kshitij.so 866
def addManualDeal(data, multi):
867
    if multi !=1:
868
        collection = get_mongo_connection().Catalog.ManualDeals
15090 kshitij.so 869
        cursor = collection.find({'sku':data['sku']})
14553 kshitij.so 870
        if cursor.count() > 0:
871
            return {0:"Sku information already present."}
872
        else:
873
            collection.insert(data)
874
            get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
875
            return {1:"Data added successfully"}
14481 kshitij.so 876
    else:
14553 kshitij.so 877
        skuIds = __getBundledSkusfromSku(data['sku'])
878
        for sku in skuIds:
879
            data['sku'] = sku.get('_id')
880
            collection = get_mongo_connection().Catalog.ManualDeals
15090 kshitij.so 881
            cursor = collection.find({'sku':data['sku']})
14553 kshitij.so 882
            if cursor.count() > 0:
883
                continue
884
            else:
14558 kshitij.so 885
                data.pop('_id',None)
14553 kshitij.so 886
                collection.insert(data)
887
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
14481 kshitij.so 888
        return {1:"Data added successfully"}
14553 kshitij.so 889
 
14481 kshitij.so 890
def deleteDocument(data):
891
    print data
892
    try:
893
        collection = get_mongo_connection().Catalog[data['class']]
894
        class_name = data.pop('class')
895
        _id = data.pop('oid')
896
        record = list(collection.find({'_id':ObjectId(_id)}))
897
        collection.remove({'_id':ObjectId(_id)})
15075 kshitij.so 898
        if class_name != "Notifications":
899
            if class_name !="CategoryDiscount":
900
                get_mongo_connection().Catalog.MasterData.update({'_id':record[0]['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}})
901
            else:
902
                get_mongo_connection().Catalog.MasterData.update({'brand':re.compile(record[0]['brand'], re.IGNORECASE),'category_id':record[0]['category_id']}, \
903
                                                                 {"$set":{'updatedOn':to_java_date(datetime.now())}},upsert=False,multi=True)
14481 kshitij.so 904
        return {1:"Document deleted successfully"}
905
    except Exception as e:
906
        print e
907
        return {0:"Document not deleted."}
13970 kshitij.so 908
 
14482 kshitij.so 909
def searchMaster(offset, limit, search_term):
910
    data = []
14531 kshitij.so 911
    if search_term is not None:
14551 kshitij.so 912
        terms = search_term.split(' ')
913
        outer_query = []
914
        for term in terms:
915
            outer_query.append({"source_product_name":re.compile(term, re.IGNORECASE)})
14531 kshitij.so 916
        try:
14551 kshitij.so 917
            collection = get_mongo_connection().Catalog.MasterData.find({"$and":outer_query,'source_id':{'$in':SOURCE_MAP.keys()}}).skip(offset).limit(limit)
14531 kshitij.so 918
            for record in collection:
919
                data.append(record)
920
        except:
921
            pass
922
    else:
923
        collection = get_mongo_connection().Catalog.MasterData.find({'source_id':{'$in':SOURCE_MAP.keys()}}).skip(offset).limit(limit)
14482 kshitij.so 924
        for record in collection:
925
            data.append(record)
926
    return data
14481 kshitij.so 927
 
14495 kshitij.so 928
def getAllFeaturedDeals(offset, limit):
929
    data = []
930
    collection = get_mongo_connection().Catalog.FeaturedDeals
931
    cursor = collection.find({'endDate':{'$gte':to_java_date(datetime.now())}}).skip(offset).limit(limit)
932
    for val in cursor:
933
        master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
934
        if len(master) > 0:
935
            val['brand'] = master[0]['brand']
936
            val['source_product_name'] = master[0]['source_product_name']
937
            val['skuBundleId'] = master[0]['skuBundleId']
938
        else:
939
            val['brand'] = ""
940
            val['source_product_name'] = ""
941
            val['skuBundleId'] = ""
942
        data.append(val)
943
    return data
944
 
14553 kshitij.so 945
def addFeaturedDeal(data, multi):
946
    if multi !=1:
947
        collection = get_mongo_connection().Catalog.FeaturedDeals
948
        cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
949
        if cursor.count() > 0:
950
            return {0:"Sku information already present."}
951
        else:
952
            collection.insert(data)
953
            get_mongo_connection().Catalog.MasterData.update({'_id':data['sku']},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=False)
954
            return {1:"Data added successfully"}
14495 kshitij.so 955
    else:
14553 kshitij.so 956
        skuIds = __getBundledSkusfromSku(data['sku'])
957
        for sku in skuIds:
958
            data['sku'] = sku.get('_id')
959
            collection = get_mongo_connection().Catalog.FeaturedDeals
960
            cursor = collection.find({'sku':data['sku'],'startDate':{'$lte':data['startDate']},'endDate':{'$gte':data['endDate']}})
961
            if cursor.count() > 0:
962
                continue
963
            else:
14558 kshitij.so 964
                data.pop('_id',None)
14553 kshitij.so 965
                collection.insert(data)
966
        get_mongo_connection().Catalog.MasterData.update({'skuBundleId':sku.get('skuBundleId')},{"$set":{'updatedOn':to_java_date(datetime.now())}},multi=True)
14495 kshitij.so 967
        return {1:"Data added successfully"}
968
 
14499 kshitij.so 969
def searchCollection(class_name, sku, skuBundleId):
14497 kshitij.so 970
    data = []
971
    collection = get_mongo_connection().Catalog[class_name]
15076 kshitij.so 972
    if class_name == "Notifications":
973
        cursor = collection.find({'skuBundleId':skuBundleId})
974
        for val in cursor:
975
            master = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':val['skuBundleId']}))
976
            if len(master) > 0:
977
                val['brand'] = master[0]['brand']
978
                val['model_name'] = master[0]['model_name']
979
                val['skuBundleId'] = master[0]['skuBundleId']
980
            else:
981
                val['brand'] = ""
982
                val['model_name'] = ""
983
                val['skuBundleId'] = val['skuBundleId']
984
            data.append(val)
15095 kshitij.so 985
        return data
14499 kshitij.so 986
    if sku is not None:
987
        cursor = collection.find({'sku':sku})
988
        for val in cursor:
989
            master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
990
            if len(master) > 0:
991
                val['brand'] = master[0]['brand']
992
                val['source_product_name'] = master[0]['source_product_name']
993
                val['skuBundleId'] = master[0]['skuBundleId']
994
            else:
995
                val['brand'] = ""
996
                val['source_product_name'] = ""
997
                val['skuBundleId'] = ""
998
            data.append(val)
999
        return data
1000
    else:
14562 kshitij.so 1001
        skuIds = get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId}).distinct('_id')
14499 kshitij.so 1002
        for sku in skuIds:
1003
            cursor = collection.find({'sku':sku})
1004
            for val in cursor:
1005
                master = list(get_mongo_connection().Catalog.MasterData.find({'_id':val['sku']}))
1006
                if len(master) > 0:
1007
                    val['brand'] = master[0]['brand']
1008
                    val['source_product_name'] = master[0]['source_product_name']
1009
                    val['skuBundleId'] = master[0]['skuBundleId']
1010
                else:
1011
                    val['brand'] = ""
1012
                    val['source_product_name'] = ""
1013
                    val['skuBundleId'] = ""
1014
                data.append(val)
1015
        return data
14495 kshitij.so 1016
 
15074 kshitij.so 1017
def __getBrandIdForBrand(brandName, category_id):
1018
    brandInfo = Brands.query.filter(Brands.category_id==category_id).filter(Brands.name == brandName).all()
1019
    if brandInfo is None or len(brandInfo)!=1:
1020
        raise
1021
    else:
1022
        return brandInfo[0].id
1023
 
14588 kshitij.so 1024
def addNewItem(data):
14594 kshitij.so 1025
    try:
1026
        data['updatedOn'] = to_java_date(datetime.now())
1027
        data['addedOn'] = to_java_date(datetime.now())
1028
        data['priceUpdatedOn'] = to_java_date(datetime.now())
1029
        max_id = list(get_mongo_connection().Catalog.MasterData.find().sort([('_id',pymongo.DESCENDING)]).limit(1))
1030
        max_bundle = list(get_mongo_connection().Catalog.MasterData.find().sort([('skuBundleId',pymongo.DESCENDING)]).limit(1))
1031
        data['_id'] = max_id[0]['_id'] + 1
1032
        data['skuBundleId'] = max_bundle[0]['skuBundleId'] + 1
1033
        data['identifier'] = str(data['identifier'])
1034
        data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
15074 kshitij.so 1035
        data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
14594 kshitij.so 1036
        get_mongo_connection().Catalog.MasterData.insert(data)
1037
        return {1:'Data added successfully'}
1038
    except Exception as e:
1039
        print e
1040
        return {0:'Unable to add data.'}
15130 kshitij.so 1041
    finally:
1042
        session.close()
14588 kshitij.so 1043
 
1044
def addItemToExistingBundle(data):
1045
    try:
1046
        data['updatedOn'] = to_java_date(datetime.now())
1047
        data['addedOn'] = to_java_date(datetime.now())
1048
        data['priceUpdatedOn'] = to_java_date(datetime.now())
14593 kshitij.so 1049
        max_id = list(get_mongo_connection().Catalog.MasterData.find().sort([('_id',pymongo.DESCENDING)]).limit(1))
14590 kshitij.so 1050
        data['_id'] = max_id[0]['_id'] + 1
14594 kshitij.so 1051
        data['identifier'] = str(data['identifier'])
1052
        data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
15074 kshitij.so 1053
        data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
14588 kshitij.so 1054
        get_mongo_connection().Catalog.MasterData.insert(data)
1055
        return {1:'Data added successfully.'}
14594 kshitij.so 1056
    except Exception as e:
1057
        print e
14588 kshitij.so 1058
        return {0:'Unable to add data.'}
15130 kshitij.so 1059
    finally:
1060
        session.close()
14588 kshitij.so 1061
 
1062
def updateMaster(data, multi):
15130 kshitij.so 1063
    try:
1064
        print data
1065
        if multi != 1:
1066
            _id = data.pop('_id')
1067
            skuBundleId = data.pop('skuBundleId')
1068
            data['updatedOn'] = to_java_date(datetime.now())
1069
            data['identifier'] = str(data['identifier'])
1070
            data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
1071
            data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
1072
            get_mongo_connection().Catalog.MasterData.update({'_id':_id},{"$set":data},upsert=False)
1073
            return {1:'Data updated successfully.'}
1074
        else:
1075
            _id = data.pop('_id')
1076
            skuBundleId = data.pop('skuBundleId')
1077
            data['updatedOn'] = to_java_date(datetime.now())
1078
            data['identifier'] = str(data['identifier'])
1079
            data['secondaryIdentifier'] = str(data['secondaryIdentifier'])
1080
            data['brand_id'] = __getBrandIdForBrand(data['brand'], data['category_id'])
1081
            get_mongo_connection().Catalog.MasterData.update({'_id':_id},{"$set":data},upsert=False)
1082
            similarItems = get_mongo_connection().Catalog.MasterData.find({'skuBundleId':skuBundleId})
1083
            for item in similarItems:
1084
                if item['_id'] == _id:
1085
                    continue
1086
                item['updatedOn'] = to_java_date(datetime.now())
1087
                item['thumbnail'] = data['thumbnail']
1088
                item['category'] = data['category']
1089
                item['category_id'] = data['category_id']
1090
                item['tagline'] = data['tagline']
1091
                item['is_shortage'] = data['is_shortage']
1092
                item['mrp'] = data['mrp']
1093
                item['status'] = data['status']
1094
                item['maxPrice'] = data['maxPrice']
1095
                item['brand_id'] = data['brand_id']
1096
                similar_item_id = item.pop('_id')
1097
                get_mongo_connection().Catalog.MasterData.update({'_id':similar_item_id},{"$set":item},upsert=False)
1098
            return {1:'Data updated successfully.'}
1099
    finally:
1100
        session.close()
14619 kshitij.so 1101
 
1102
def getLiveCricScore():
1103
    return mc.get('liveScore')
14852 kshitij.so 1104
 
1105
def addBundleToNotification(data):
1106
    try:
15069 kshitij.so 1107
        collection = get_mongo_connection().Catalog.Notifications
14852 kshitij.so 1108
        cursor = collection.find({'skuBundleId':data['skuBundleId']})
1109
        if cursor.count() > 0:
1110
            return {0:"SkuBundleId information already present."}
1111
        else:
1112
            collection.insert(data)
1113
            return {1:'Data updated successfully.'}
1114
    except:
1115
        return {0:'Unable to add data.'}
1116
 
1117
def getAllNotifications(offset, limit):
1118
    data = []
15069 kshitij.so 1119
    collection = get_mongo_connection().Catalog.Notifications
14852 kshitij.so 1120
    cursor = collection.find().skip(offset).limit(limit)
1121
    for val in cursor:
15072 kshitij.so 1122
        master = list(get_mongo_connection().Catalog.MasterData.find({'skuBundleId':val['skuBundleId']}))
14852 kshitij.so 1123
        if len(master) > 0:
1124
            val['brand'] = master[0]['brand']
15071 kshitij.so 1125
            val['model_name'] = master[0]['model_name']
14852 kshitij.so 1126
            val['skuBundleId'] = master[0]['skuBundleId']
1127
        else:
1128
            val['brand'] = ""
15071 kshitij.so 1129
            val['model_name'] = ""
1130
            val['skuBundleId'] = val['skuBundleId']
14852 kshitij.so 1131
        data.append(val)
1132
    return data
14997 kshitij.so 1133
 
1134
def getBrandsForFilter(category_id):
1135
    if mc.get("brandFilter") is None:
15095 kshitij.so 1136
        print "Populating brand data for category_id %d" %(category_id)
14997 kshitij.so 1137
        tabData, mobData = [], []
1138
        mobileDeals = get_mongo_connection().Catalog.Deals.aggregate([
15095 kshitij.so 1139
                                                                      {"$match":{"category_id":3,"showDeal":1,"totalPoints":{"$gte":-100}}
14997 kshitij.so 1140
                                                                    },
1141
                                                                 {"$group" : 
1142
                                                                  {'_id':{'brand_id':'$brand_id','brand':'$brand'},'count':{'$sum':1}}
1143
                                                                  }
1144
                                                                ])
14588 kshitij.so 1145
 
14997 kshitij.so 1146
        tabletDeals = get_mongo_connection().Catalog.Deals.aggregate([
15095 kshitij.so 1147
                                                                      {"$match":{"category_id":5,"showDeal":1,"totalPoints":{"$gte":-100}}
14997 kshitij.so 1148
                                                                    },
1149
                                                                 {"$group" : 
1150
                                                                  {'_id':{'brand_id':'$brand_id','brand':'$brand'},'count':{'$sum':1}}
1151
                                                                  }
1152
                                                                ])
1153
 
1154
        allDeals = get_mongo_connection().Catalog.Deals.aggregate([
15095 kshitij.so 1155
                                                                   {"$match":{"showDeal":1,"totalPoints":{"$gte":-100}}
14997 kshitij.so 1156
                                                                    },
1157
                                                                 {"$group" : 
1158
                                                                  {'_id':{'brand_id':'$brand_id','brand':'$brand'},'count':{'$sum':1}}
1159
                                                                  }
1160
                                                                ])
1161
        #print mobileDeals
1162
        #print "==========Mobile data ends=========="
1163
 
1164
        #print tabletDeals
1165
        #print "==========Tablet data ends=========="
1166
 
1167
        #print allDeals
1168
        #print "==========All deal data ends========="
1169
 
1170
        for mobileDeal in mobileDeals['result']:
1171
            if mobileDeal.get('_id').get('brand_id') != 0:
1172
                tempMap = {}
1173
                tempMap['brand'] = mobileDeal.get('_id').get('brand')
1174
                tempMap['brand_id'] = mobileDeal.get('_id').get('brand_id')
1175
                tempMap['count'] = mobileDeal.get('count')
1176
                mobData.append(tempMap)
1177
 
1178
        for tabletDeal in tabletDeals['result']:
1179
            if tabletDeal.get('_id').get('brand_id') != 0:
1180
                tempMap = {}
1181
                tempMap['brand'] = tabletDeal.get('_id').get('brand')
1182
                tempMap['brand_id'] = tabletDeal.get('_id').get('brand_id')
1183
                tempMap['count'] = tabletDeal.get('count')
1184
                tabData.append(tempMap)
1185
 
1186
 
1187
        brandMap = {}
1188
        for allDeal in allDeals['result']:
1189
            if allDeal.get('_id').get('brand_id') != 0:
1190
                if brandMap.has_key(allDeal.get('_id').get('brand')):
15002 kshitij.so 1191
                    brand_ids = brandMap.get(allDeal.get('_id').get('brand')).get('brand_ids')
14997 kshitij.so 1192
                    brand_ids.append(allDeal.get('_id').get('brand_id'))
15003 kshitij.so 1193
                    brandMap[allDeal.get('_id').get('brand')] = {'brand_ids':brand_ids,'count':brandMap.get(allDeal.get('_id').get('brand')).get('count') + allDeal.get('count')}
14997 kshitij.so 1194
                else:
1195
                    temp = []
1196
                    temp.append(allDeal.get('_id').get('brand_id'))
15002 kshitij.so 1197
                    brandMap[allDeal.get('_id').get('brand')] = {'brand_ids':temp,'count':allDeal.get('count')}
14997 kshitij.so 1198
 
1199
        mc.set("brandFilter",{0:brandMap, 3:mobData, 5:tabData}, 600)  
1200
 
15062 kshitij.so 1201
    return sorted(mc.get("brandFilter").get(category_id), key = lambda x: (-x['count'], x['brand']))
15375 kshitij.so 1202
 
1203
def getStaticDeals(offset, limit, category_id):
1204
    user_specific_deals = mc.get("staticDeals")
1205
    if user_specific_deals is None:
1206
        __populateStaticDeals()
1207
        user_specific_deals = mc.get("staticDeals")
1208
    return (user_specific_deals.get(category_id))[offset:offset+limit]
1209
 
1210
def __populateStaticDeals():
1211
    print "Populating memcache for static deals"
1212
    outer_query = []
1213
    outer_query.append({"showDeal":1})
1214
    query = {}
1215
    query['$gte'] = -100
1216
    outer_query.append({'totalPoints':query})
1217
    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,'brand_id':1,'skuBundleId':1}).sort([('totalPoints',pymongo.DESCENDING),('bestSellerPoints',pymongo.DESCENDING),('nlcPoints',pymongo.DESCENDING),('rank',pymongo.DESCENDING)]))
1218
    mobile_deals = []
1219
    tablet_deals = []
1220
    for deal in all_deals:
1221
        item = get_mongo_connection().Catalog.MasterData.find({'_id':deal['_id']})
1222
        if deal['category_id'] ==3:
1223
            mobile_deals.append(getItemObjForStaticDeals(item[0]))
1224
        elif deal['category_id'] ==5:
1225
            tablet_deals.append(getItemObjForStaticDeals(item[0]))
1226
        else:
1227
            continue
1228
 
1229
    random.shuffle(mobile_deals,random.random)
1230
    random.shuffle(tablet_deals,random.random)
1231
 
1232
    mem_cache_val = {3:mobile_deals, 5:tablet_deals}
1233
    mc.set("staticDeals", mem_cache_val, 3600)
1234
 
1235
def getItemObjForStaticDeals(item):
15379 kshitij.so 1236
    return {'marketPlaceUrl':item.get('marketPlaceUrl'),'available_price':item.get('available_price'),'source_product_name':item.get('source_product_name'),'thumbnail':item.get('thumbnail'),'source_id':int(item.get('source_id'))}
15375 kshitij.so 1237
 
13572 kshitij.so 1238
def main():
15375 kshitij.so 1239
    #print getNewDeals(47, 3, 0, 20, None, None, "brandFilter:64")
1240
    #mc.delete(str(-1))
1241
    staticDeals = getStaticDeals(0,20,3)
1242
    print "========="
1243
    for y in staticDeals:
1244
        print y
13811 kshitij.so 1245
 
13921 kshitij.so 1246
 
15375 kshitij.so 1247
 
13572 kshitij.so 1248
if __name__=='__main__':
13932 amit.gupta 1249
    main()