Subversion Repositories SmartDukaan

Rev

Rev 13771 | Rev 13785 | 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
3
from dtr.utils.utils import to_java_date 
4
 
5
con = None
6
 
7
def get_mongo_connection(host='localhost', port=27017):
8
    global con
9
    if con is None:
10
        print "Establishing connection %s host and port %d" %(host,port)
11
        try:
12
            con = pymongo.MongoClient(host, port)
13
        except Exception, e:
14
            print e
15
            return None
16
    return con
17
 
18
def addCategoryDiscount(data):
19
    collection = get_mongo_connection().Dtr.CategoryDiscount
20
    query = []
21
    data['brand'] = data['brand'].strip().upper()
22
    query.append({"brand":data['brand']})
23
    query.append({"category_id":data['category_id']})
24
    r = collection.find({"$and":query})
25
    if r.count() > 0:
13639 kshitij.so 26
        return {0:"Brand & Category info already present."}
13572 kshitij.so 27
    else:
28
        collection.insert(data)
13639 kshitij.so 29
        return {1:"Data added successfully"}
13572 kshitij.so 30
 
31
def getAllCategoryDiscount():
32
    data = []
33
    collection = get_mongo_connection().Dtr.CategoryDiscount
34
    cursor = collection.find()
35
    for val in cursor:
36
        data.append(val)
37
    return data
38
 
39
def addSchemeDetailsForSku(data):
40
    collection = get_mongo_connection().Dtr.SkuSchemeDetails
41
    data['addedOn'] = to_java_date(datetime.now())
42
    collection.insert(data)
13639 kshitij.so 43
    return {1:"Data added successfully"}
13572 kshitij.so 44
 
45
def getAllSkuWiseSchemeDetails():
46
    data = []
47
    collection = get_mongo_connection().Dtr.SkuSchemeDetails
48
    cursor = collection.find()
49
    for val in cursor:
50
        data.append(val)
51
    return data
52
 
53
def addSkuDiscountInfo(data):
54
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
55
    cursor = collection.find({"sku":data['sku']})
13642 kshitij.so 56
    if cursor.count() > 0:
13639 kshitij.so 57
        return {0:"Sku information 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 getallSkuDiscountInfo():
63
    data = []
64
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
65
    cursor = collection.find()
66
    for val in cursor:
67
        data.append(val)
68
    return data
69
 
70
def addExceptionalNlc(data):
71
    collection = get_mongo_connection().Dtr.ExceptionalNlc
72
    cursor = collection.find({"sku":data['sku']})
13642 kshitij.so 73
    if cursor.count() > 0:
13639 kshitij.so 74
        return {0:"Sku information already present."}
13572 kshitij.so 75
    else:
76
        collection.insert(data)
13639 kshitij.so 77
        return {1:"Data added successfully"}
13572 kshitij.so 78
 
79
def getAllExceptionlNlcItems():
80
    data = []
13639 kshitij.so 81
    collection = get_mongo_connection().Dtr.ExceptionalNlc
13572 kshitij.so 82
    cursor = collection.find()
83
    for val in cursor:
84
        data.append(val)
85
    return data
86
 
13582 amit.gupta 87
def getMerchantOrdersByUser(userId, page=1, window=50):
13603 amit.gupta 88
    if page==None:
89
        page = 1
90
 
91
    if window==None:
92
        window = 50
93
    result = {}
13582 amit.gupta 94
    skip = (page-1)*window
13603 amit.gupta 95
    collection = get_mongo_connection().Dtr.merchantOrder
96
    cursor = collection.find({"userId":userId})
97
    total_count = cursor.count()
98
    pages = total_count/window + (0 if total_count%window==0 else 1)  
99
    print "total_count", total_count
100
    if total_count > skip:
101
        cursor = cursor.skip(skip).limit(window)
102
        orders = []
103
        for order in cursor:
104
            del(order["_id"])
105
            orders.append(order)
106
        result['data'] = orders
107
        result['window'] = window
108
        result['totalCount'] = total_count 
109
        result['currCount'] = cursor.count()
110
        result['totalPages'] = pages
111
        result['currPage'] = page    
112
        return result
113
    else:
114
        return result
13630 kshitij.so 115
 
13771 kshitij.so 116
def getDeals(category_id, offset, limit):
117
    rank = 1
118
    deals = {}
13778 kshitij.so 119
    if category_id == 0:
13771 kshitij.so 120
        print "Getting all cat"
13778 kshitij.so 121
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0}}).sort([('totalPoints',pymongo.DESCENDING)]).skip(offset).limit(limit))
13771 kshitij.so 122
    else:
13778 kshitij.so 123
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'category_id':category_id}).sort([('totalPoints',pymongo.DESCENDING)]).skip(offset).limit(limit))
13771 kshitij.so 124
    for d in data:
125
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
126
        if not deals.has_key(item[0]['identifier']):
127
            item[0]['dealRank'] = rank 
128
            deals[item[0]['identifier']] = item[0]
129
            rank +=1
130
    return deals.values()
131
 
132
 
13572 kshitij.so 133
def main():
13771 kshitij.so 134
    getDeals("0", "0", "10")
13572 kshitij.so 135
if __name__=='__main__':
136
    main()