Subversion Repositories SmartDukaan

Rev

Rev 13797 | Rev 13801 | 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
13795 kshitij.so 7
sortDirection = {'asc':1,'dsc':-1}
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
 
20
def addCategoryDiscount(data):
21
    collection = get_mongo_connection().Dtr.CategoryDiscount
22
    query = []
23
    data['brand'] = data['brand'].strip().upper()
24
    query.append({"brand":data['brand']})
25
    query.append({"category_id":data['category_id']})
26
    r = collection.find({"$and":query})
27
    if r.count() > 0:
13639 kshitij.so 28
        return {0:"Brand & Category info already present."}
13572 kshitij.so 29
    else:
30
        collection.insert(data)
13639 kshitij.so 31
        return {1:"Data added successfully"}
13572 kshitij.so 32
 
33
def getAllCategoryDiscount():
34
    data = []
35
    collection = get_mongo_connection().Dtr.CategoryDiscount
36
    cursor = collection.find()
37
    for val in cursor:
38
        data.append(val)
39
    return data
40
 
41
def addSchemeDetailsForSku(data):
42
    collection = get_mongo_connection().Dtr.SkuSchemeDetails
43
    data['addedOn'] = to_java_date(datetime.now())
44
    collection.insert(data)
13639 kshitij.so 45
    return {1:"Data added successfully"}
13572 kshitij.so 46
 
47
def getAllSkuWiseSchemeDetails():
48
    data = []
49
    collection = get_mongo_connection().Dtr.SkuSchemeDetails
50
    cursor = collection.find()
51
    for val in cursor:
52
        data.append(val)
53
    return data
54
 
55
def addSkuDiscountInfo(data):
56
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
57
    cursor = collection.find({"sku":data['sku']})
13642 kshitij.so 58
    if cursor.count() > 0:
13639 kshitij.so 59
        return {0:"Sku information already present."}
13572 kshitij.so 60
    else:
61
        collection.insert(data)
13639 kshitij.so 62
        return {1:"Data added successfully"}
13572 kshitij.so 63
 
64
def getallSkuDiscountInfo():
65
    data = []
66
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
67
    cursor = collection.find()
68
    for val in cursor:
69
        data.append(val)
70
    return data
71
 
72
def addExceptionalNlc(data):
73
    collection = get_mongo_connection().Dtr.ExceptionalNlc
74
    cursor = collection.find({"sku":data['sku']})
13642 kshitij.so 75
    if cursor.count() > 0:
13639 kshitij.so 76
        return {0:"Sku information already present."}
13572 kshitij.so 77
    else:
78
        collection.insert(data)
13639 kshitij.so 79
        return {1:"Data added successfully"}
13572 kshitij.so 80
 
81
def getAllExceptionlNlcItems():
82
    data = []
13639 kshitij.so 83
    collection = get_mongo_connection().Dtr.ExceptionalNlc
13572 kshitij.so 84
    cursor = collection.find()
85
    for val in cursor:
86
        data.append(val)
87
    return data
88
 
13582 amit.gupta 89
def getMerchantOrdersByUser(userId, page=1, window=50):
13603 amit.gupta 90
    if page==None:
91
        page = 1
92
 
93
    if window==None:
94
        window = 50
95
    result = {}
13582 amit.gupta 96
    skip = (page-1)*window
13603 amit.gupta 97
    collection = get_mongo_connection().Dtr.merchantOrder
98
    cursor = collection.find({"userId":userId})
99
    total_count = cursor.count()
100
    pages = total_count/window + (0 if total_count%window==0 else 1)  
101
    print "total_count", total_count
102
    if total_count > skip:
103
        cursor = cursor.skip(skip).limit(window)
104
        orders = []
105
        for order in cursor:
106
            del(order["_id"])
107
            orders.append(order)
108
        result['data'] = orders
109
        result['window'] = window
110
        result['totalCount'] = total_count 
111
        result['currCount'] = cursor.count()
112
        result['totalPages'] = pages
113
        result['currPage'] = page    
114
        return result
115
    else:
116
        return result
13630 kshitij.so 117
 
13795 kshitij.so 118
def getDeals(category_id, offset, limit, sort, direction):
13771 kshitij.so 119
    rank = 1
120
    deals = {}
13795 kshitij.so 121
    if len(sort) * len(direction) == 0:
122
        sortField = "totalPoints"
13797 kshitij.so 123
        direct = sortDirection.get('dsc')
13795 kshitij.so 124
    else:
125
        sortField = sort
13797 kshitij.so 126
        direct = sortDirection.get(direction)
13778 kshitij.so 127
    if category_id == 0:
13771 kshitij.so 128
        print "Getting all cat"
13800 kshitij.so 129
        data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0}}).sort({sortField,direct}).skip(offset).limit(limit))
13771 kshitij.so 130
    else:
13800 kshitij.so 131
        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 132
    for d in data:
133
        item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))
134
        if not deals.has_key(item[0]['identifier']):
135
            item[0]['dealRank'] = rank 
136
            deals[item[0]['identifier']] = item[0]
137
            rank +=1
13785 kshitij.so 138
    return sorted(deals.values(), key=itemgetter('dealRank'))
139
 
140
def getItem(skuId):
13795 kshitij.so 141
    try:
142
        skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))
13785 kshitij.so 143
        return skuData
13795 kshitij.so 144
    except:
145
        return [{}]
13771 kshitij.so 146
 
13785 kshitij.so 147
 
13771 kshitij.so 148
 
13572 kshitij.so 149
def main():
13771 kshitij.so 150
    getDeals("0", "0", "10")
13572 kshitij.so 151
if __name__=='__main__':
152
    main()