Subversion Repositories SmartDukaan

Rev

Rev 13801 | Rev 13805 | 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
13801 kshitij.so 126
    print category_id
127
    print offset
128
    print limit
129
    print sortField
130
    print direct
13778 kshitij.so 131
    if category_id == 0:
13771 kshitij.so 132
        print "Getting all cat"
13801 kshitij.so 133
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0}}).sort([(sortField,direct)]).skip(offset).limit(limit))
13771 kshitij.so 134
    else:
13801 kshitij.so 135
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'category_id':category_id}).sort([(sortField,direct)]).skip(offset).limit(limit))
13771 kshitij.so 136
    for d in data:
137
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
138
        if not deals.has_key(item[0]['identifier']):
139
            item[0]['dealRank'] = rank 
140
            deals[item[0]['identifier']] = item[0]
141
            rank +=1
13785 kshitij.so 142
    return sorted(deals.values(), key=itemgetter('dealRank'))
143
 
144
def getItem(skuId):
13795 kshitij.so 145
    try:
146
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
13785 kshitij.so 147
        return skuData
13795 kshitij.so 148
    except:
149
        return [{}]
13771 kshitij.so 150
 
13785 kshitij.so 151
 
13771 kshitij.so 152
 
13572 kshitij.so 153
def main():
13801 kshitij.so 154
    getDeals(3, 0, 10,"","")
13572 kshitij.so 155
if __name__=='__main__':
156
    main()