Subversion Repositories SmartDukaan

Rev

Rev 13840 | Rev 13910 | 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 = {}
13803 kshitij.so 146
    if sort is None or direction is None:
13795 kshitij.so 147
        sortField = "totalPoints"
13803 kshitij.so 148
        direct = -1
13795 kshitij.so 149
    else:
150
        sortField = sort
13803 kshitij.so 151
        direct = direction
13778 kshitij.so 152
    if category_id == 0:
13907 kshitij.so 153
        print "All categories deals"
154
        print category_id
155
        print sortField
156
        print offset
157
        print limit
158
        print direct
13825 kshitij.so 159
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'showDeal':1}).sort([(sortField,direct)]).skip(offset).limit(limit))
13771 kshitij.so 160
    else:
13907 kshitij.so 161
        print "selective category deals"
162
        print category_id
163
        print sortField
164
        print offset
165
        print limit
166
        print direct
13825 kshitij.so 167
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'category_id':category_id,'showDeal':1}).sort([(sortField,direct)]).skip(offset).limit(limit))
13806 kshitij.so 168
    print data[0]['_id']
13771 kshitij.so 169
    for d in data:
170
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
171
        if not deals.has_key(item[0]['identifier']):
172
            item[0]['dealRank'] = rank 
173
            deals[item[0]['identifier']] = item[0]
174
            rank +=1
13785 kshitij.so 175
    return sorted(deals.values(), key=itemgetter('dealRank'))
176
 
177
def getItem(skuId):
13795 kshitij.so 178
    try:
179
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
13785 kshitij.so 180
        return skuData
13795 kshitij.so 181
    except:
182
        return [{}]
13836 kshitij.so 183
 
184
def getCashBackDetails(identifier, source_id):
185
    """Need to add item level cashback, no data available right now."""
13771 kshitij.so 186
 
13839 kshitij.so 187
    if source_id in (1,2,4):
188
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))
13840 kshitij.so 189
    elif source_id == 3:
13839 kshitij.so 190
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))
13840 kshitij.so 191
    else:
192
        return {}
13836 kshitij.so 193
    if len(skuData) > 0:
194
        cashback = list(get_mongo_connection().Catalog.CategoryCashBack.find({'category_id':skuData[0]['category_id'], 'source_id':source_id}))
195
        if len(cashback) > 0:
196
            return cashback[0]
197
        else:
198
            return {} 
199
    else:
200
        return {}
13785 kshitij.so 201
 
13836 kshitij.so 202
 
203
 
13771 kshitij.so 204
 
13572 kshitij.so 205
def main():
13907 kshitij.so 206
    populateCashBack()
13811 kshitij.so 207
 
13572 kshitij.so 208
if __name__=='__main__':
209
    main()