Rev 13839 | Rev 13910 | 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, sort, direction):rank = 1deals = {}if sort is None or direction is None:sortField = "totalPoints"direct = -1else:sortField = sortdirect = directionif category_id == 0:data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'showDeal':1}).sort([(sortField,direct)]).skip(offset).limit(limit))else:data = list(get_mongo_connection().Catalog.Deals.find({'rank':{'$gt':0},'category_id':category_id,'showDeal':1}).sort([(sortField,direct)]).skip(offset).limit(limit))print data[0]['_id']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):try:skuData = list(get_mongo_connection().Catalog.MasterData.find({'_id':int(skuId)}))return skuDataexcept:return [{}]def getCashBackDetails(identifier, source_id):"""Need to add item level cashback, no data available right now."""if source_id in (1,2,4):skuData = list(get_mongo_connection().Catalog.MasterData.find({'identifier':identifier.strip(), 'source_id':source_id}))elif source_id == 3:skuData = list(get_mongo_connection().Catalog.MasterData.find({'secondaryIdentifier':identifier.strip(), 'source_id':source_id}))else:return {}if len(skuData) > 0:cashback = list(get_mongo_connection().Catalog.CategoryCashBack.find({'category_id':skuData[0]['category_id'], 'source_id':source_id}))if len(cashback) > 0:return cashback[0]else:return {}else:return {}def main():getDeals(3, 0, 10,"","")if __name__=='__main__':main()