Subversion Repositories SmartDukaan

Rev

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

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