Rev 13778 | Rev 13797 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
import pymongofrom datetime import datetimefrom dtr.utils.utils import to_java_datefrom operator import itemgettercon = Nonedef get_mongo_connection(host='localhost', port=27017):global conif con is None:print "Establishing connection %s host and port %d" %(host,port)try:con = pymongo.MongoClient(host, port)except Exception, e:print ereturn Nonereturn condef addCategoryDiscount(data):collection = get_mongo_connection().Dtr.CategoryDiscountquery = []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.CategoryDiscountcursor = collection.find()for val in cursor:data.append(val)return datadef addSchemeDetailsForSku(data):collection = get_mongo_connection().Dtr.SkuSchemeDetailsdata['addedOn'] = to_java_date(datetime.now())collection.insert(data)return {1:"Data added successfully"}def getAllSkuWiseSchemeDetails():data = []collection = get_mongo_connection().Dtr.SkuSchemeDetailscursor = collection.find()for val in cursor:data.append(val)return datadef addSkuDiscountInfo(data):collection = get_mongo_connection().Dtr.SkuDiscountInfocursor = 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.SkuDiscountInfocursor = collection.find()for val in cursor:data.append(val)return datadef addExceptionalNlc(data):collection = get_mongo_connection().Dtr.ExceptionalNlccursor = 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.ExceptionalNlccursor = collection.find()for val in cursor:data.append(val)return datadef getMerchantOrdersByUser(userId, page=1, window=50):if page==None:page = 1if window==None:window = 50result = {}skip = (page-1)*windowcollection = get_mongo_connection().Dtr.merchantOrdercursor = collection.find({"userId":userId})total_count = cursor.count()pages = total_count/window + (0 if total_count%window==0 else 1)print "total_count", total_countif total_count > skip:cursor = cursor.skip(skip).limit(window)orders = []for order in cursor:del(order["_id"])orders.append(order)result['data'] = ordersresult['window'] = windowresult['totalCount'] = total_countresult['currCount'] = cursor.count()result['totalPages'] = pagesresult['currPage'] = pagereturn resultelse:return resultdef getDeals(category_id, offset, limit):rank = 1deals = {}if category_id == 0:print "Getting all cat"data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0}}).sort([('totalPoints',pymongo.DESCENDING)]).skip(offset).limit(limit))else:data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'category_id':category_id}).sort([('totalPoints',pymongo.DESCENDING)]).skip(offset).limit(limit))for d in data:item = list(get_mongo_connection().Catalog.MasterData.find({'_id':d['_id']}))if not deals.has_key(item[0]['identifier']):item[0]['dealRank'] = rankdeals[item[0]['identifier']] = item[0]rank +=1return sorted(deals.values(), key=itemgetter('dealRank'))def getItem(skuId):skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':skuId}))if len(skuData) > 0:return skuDataelse:[]def main():getDeals("0", "0", "10")if __name__=='__main__':main()