Subversion Repositories SmartDukaan

Rev

Rev 13582 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

import pymongo
from datetime import datetime
from dtr.utils.utils import to_java_date 

con = None

def get_mongo_connection(host='localhost', port=27017):
    global con
    if con is None:
        print "Establishing connection %s host and port %d" %(host,port)
        try:
            con = pymongo.MongoClient(host, port)
        except Exception, e:
            print e
            return None
    return con

def addCategoryDiscount(data):
    collection = get_mongo_connection().Dtr.CategoryDiscount
    query = []
    data['brand'] = data['brand'].strip().upper()
    query.append({"brand":data['brand']})
    query.append({"category_id":data['category_id']})
    r = collection.find({"$and":query})
    if r.count() > 0:
        return {"0":"Brand & Category info already present."}
    else:
        collection.insert(data)
        return {"1":"Data added successfully"}

def getAllCategoryDiscount():
    data = []
    collection = get_mongo_connection().Dtr.CategoryDiscount
    cursor = collection.find()
    for val in cursor:
        data.append(val)
    return data

def addSchemeDetailsForSku(data):
    collection = get_mongo_connection().Dtr.SkuSchemeDetails
    data['addedOn'] = to_java_date(datetime.now())
    collection.insert(data)
    return {"1":"Data added successfully"}

def getAllSkuWiseSchemeDetails():
    data = []
    collection = get_mongo_connection().Dtr.SkuSchemeDetails
    cursor = collection.find()
    for val in cursor:
        data.append(val)
    return data

def addSkuDiscountInfo(data):
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
    cursor = collection.find({"sku":data['sku']})
    if cursor.count > 0:
        return {"0":"Sku information already present."}
    else:
        collection.insert(data)
        return {"1":"Data added successfully"}

def getallSkuDiscountInfo():
    data = []
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
    cursor = collection.find()
    for val in cursor:
        data.append(val)
    return data

def addExceptionalNlc(data):
    collection = get_mongo_connection().Dtr.ExceptionalNlc
    cursor = collection.find({"sku":data['sku']})
    if cursor.count > 0:
        return {"0":"Sku information already present."}
    else:
        collection.insert(data)
        return {"1":"Data added successfully"}

def getAllExceptionlNlcItems():
    data = []
    collection = get_mongo_connection().Dtr.SkuDiscountInfo
    cursor = collection.find()
    for val in cursor:
        data.append(val)
    return data

def main():
    pass
    
if __name__=='__main__':
    main()