Subversion Repositories SmartDukaan

Rev

Rev 13582 | Rev 13630 | 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:
26
        return {"0":"Brand & Category info already present."}
27
    else:
28
        collection.insert(data)
29
        return {"1":"Data added successfully"}
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)
43
    return {"1":"Data added successfully"}
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']})
56
    if cursor.count > 0:
57
        return {"0":"Sku information already present."}
58
    else:
59
        collection.insert(data)
60
        return {"1":"Data added successfully"}
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']})
73
    if cursor.count > 0:
74
        return {"0":"Sku information already present."}
75
    else:
76
        collection.insert(data)
77
        return {"1":"Data added successfully"}
78
 
79
def getAllExceptionlNlcItems():
80
    data = []
81
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
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
13572 kshitij.so 115
def main():
116
    pass
117
 
118
if __name__=='__main__':
119
    main()