Subversion Repositories SmartDukaan

Rev

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