Subversion Repositories SmartDukaan

Rev

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