Rev 18671 | Rev 18712 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
from bson import json_utilfrom bson.json_util import dumpsfrom datetime import datetime, timedeltafrom pyshorteners.shorteners import Shortenerfrom dtr import mainfrom dtr.config import PythonPropertyReaderfrom dtr.storage import Mongofrom dtr.storage.DataService import Retailers, Users, CallHistory, RetryConfig, \RetailerLinks, Activation_Codes, Agents, Agent_Roles, AgentLoginTimings, \FetchDataHistory, RetailerContacts, Orders, OnboardedRetailerChecklists,\RetailerAddresses, Pincodeavailability, app_offers, appmasters, user_app_cashbacks, user_app_installs,\Postoffices, UserCrmCallingData, CallHistoryCrm, ProductPricingInputsfrom dtr.storage.Mongo import get_mongo_connectionfrom dtr.storage.Mysql import fetchResultfrom dtr.utils import FetchLivePrices, DealSheet as X_DealSheet, \UserSpecificDeals, utilsfrom dtr.utils.utils import getLogger,encryptMessage,decryptMessage,\get_mongo_connection_dtr_data, to_java_datefrom elixir import *from operator import and_from sqlalchemy.sql.expression import func, func, or_, desc, asc, casefrom urllib import urlencodeimport contextlibimport falconimport jsonimport reimport stringimport tracebackimport urllibimport urllib2import uuidimport gdshortenerfrom dtr.dao import AppOfferObj, UserAppBatchDrillDown, UserAppBatchDateDrillDownimport base64from falcon.util.uri import decodeimport MySQLdbalphalist = list(string.uppercase)alphalist.remove('O')numList = ['1','2','3','4','5','6','7','8','9']codesys = [alphalist, alphalist, numList, numList, numList]CONTACT_PRIORITY = ['sms', 'called', 'ringing']RETRY_MAP = {'fresh':'retry', 'followup':'fretry', 'onboarding':'oretry'}ASSIGN_MAP = {'retry':'assigned', 'fretry':'fassigned', 'oretry':'oassigned'}sticky_agents = [17]def getNextCode(codesys, code=None):if code is None:code = []for charcode in codesys:code.append(charcode[0])return string.join(code, '')carry = Truecode = list(code)lastindex = len(codesys) - 1while carry:listChar = codesys[lastindex]newIndex = (listChar.index(code[lastindex])+1)%len(listChar)print newIndexcode[lastindex] = listChar[newIndex]if newIndex != 0:carry = Falselastindex -= 1if lastindex ==-1:raise BaseException("All codes are exhausted")return string.join(code, '')global RETAILER_DETAIL_CALL_COUNTERRETAILER_DETAIL_CALL_COUNTER = 0global USER_DETAIL_MAPUSER_DETAIL_MAP={}USER_DETAIL_MAP['accs_cart']=0USER_DETAIL_MAP['accs_active']=0USER_DETAIL_MAP['accs_order']=0lgr = getLogger('/var/log/retailer-acquisition-api.log')DEALER_RETRY_FACTOR = int(PythonPropertyReader.getConfig('DEALER_RETRY_FACTOR'))DEALER_FRESH_FACTOR = int(PythonPropertyReader.getConfig('DEALER_FRESH_FACTOR'))USER_CRM_DEFAULT_RETRY_FACTOR = int(PythonPropertyReader.getConfig('USER_CRM_DEFAULT_RETRY_FACTOR'))USER_CRM_DEFAULT_FRESH_FACTOR = int(PythonPropertyReader.getConfig('USER_CRM_DEFAULT_FRESH_FACTOR'))TOTAL = DEALER_RETRY_FACTOR + DEALER_FRESH_FACTORTOTAL_USER = USER_CRM_DEFAULT_FRESH_FACTOR + USER_CRM_DEFAULT_RETRY_FACTORclass CategoryDiscountInfo(object):def on_get(self, req, resp):result = Mongo.getAllCategoryDiscount()json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')def on_post(self, req, resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addCategoryDiscount(result_json)resp.body = json.dumps(result, encoding='utf-8')def on_put(self, req, resp, _id):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.updateCategoryDiscount(result_json, _id)resp.body = json.dumps(result, encoding='utf-8')class SkuSchemeDetails(object):def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllSkuWiseSchemeDetails(offset, limit)json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addSchemeDetailsForSku(result_json)resp.body = json.dumps(result, encoding='utf-8')class SkuDiscountInfo():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getallSkuDiscountInfo(offset,limit)json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addSkuDiscountInfo(result_json)resp.body = json.dumps(result, encoding='utf-8')def on_put(self, req, resp, _id):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.updateSkuDiscount(result_json, _id)resp.body = json.dumps(result, encoding='utf-8')class ExceptionalNlc():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllExceptionlNlcItems(offset, limit)json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addExceptionalNlc(result_json)resp.body = json.dumps(result, encoding='utf-8')def on_put(self, req, resp, _id):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.updateExceptionalNlc(result_json, _id)resp.body = json.dumps(result, encoding='utf-8')class Deals():def on_get(self,req, resp, userId):categoryId = req.get_param_as_int("categoryId")offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")sort = req.get_param("sort")direction = req.get_param_as_int("direction")filterData = req.get_param('filterData')result = Mongo.getNewDeals(int(userId), categoryId, offset, limit, sort, direction, filterData)resp.body = dumps(result)class MasterData():def on_get(self,req, resp, skuId):showDp = req.get_param_as_int("showDp")result = Mongo.getItem(skuId, showDp)try:json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')except:json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')def on_post(self,req, resp):addNew = req.get_param_as_int("addNew")update = req.get_param_as_int("update")addToExisting = req.get_param_as_int("addToExisting")multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')if addNew == 1:result = Mongo.addNewItem(result_json)elif update == 1:result = Mongo.updateMaster(result_json, multi)elif addToExisting == 1:result = Mongo.addItemToExistingBundle(result_json)else:raiseresp.body = dumps(result)class LiveData():def on_get(self,req, resp):if req.get_param_as_int("id") is not None:print "****getting only for id"id = req.get_param_as_int("id")try:result = FetchLivePrices.getLatestPriceById(id)json_docs = json.dumps(result, default=json_util.default)resp.body = json.dumps(json_docs, encoding='utf-8')except:json_docs = json.dumps({}, default=json_util.default)resp.body = json.dumps(json_docs, encoding='utf-8')else:print "****getting only for skuId"skuBundleId = req.get_param_as_int("skuBundleId")source_id = req.get_param_as_int("source_id")try:result = FetchLivePrices.getLatestPrice(skuBundleId, source_id)json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')except:json_docs = [json.dumps(doc, default=json_util.default) for doc in [{}]]resp.body = json.dumps(json_docs, encoding='utf-8')class CashBack():def on_get(self,req, resp):identifier = req.get_param("identifier")source_id = req.get_param_as_int("source_id")try:result = Mongo.getCashBackDetails(identifier, source_id)json_docs = json.dumps(result, default=json_util.default)resp.body = json_docsexcept:json_docs = json.dumps({}, default=json_util.default)resp.body = json_docsclass ImgSrc():def on_get(self,req, resp):identifier = req.get_param("identifier")source_id = req.get_param_as_int("source_id")try:result = Mongo.getImgSrc(identifier, source_id)json_docs = json.dumps(result, default=json_util.default)resp.body = json_docsexcept:json_docs = json.dumps({}, default=json_util.default)resp.body = json_docsclass DealSheet():def on_get(self,req, resp):X_DealSheet.sendMail()json_docs = json.dumps({'True':'Sheet generated, mail sent.'}, default=json_util.default)resp.body = json.dumps(json_docs, encoding='utf-8')class DealerPrice():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllDealerPrices(offset,limit)json_docs = [json.dumps(doc, default=json_util.default) for doc in result]resp.body = json.dumps(json_docs, encoding='utf-8')def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addSkuDealerPrice(result_json)resp.body = json.dumps(result, encoding='utf-8')def on_put(self, req, resp, _id):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.updateSkuDealerPrice(result_json, _id)resp.body = json.dumps(result, encoding='utf-8')class ResetCache():def on_get(self,req, resp, userId):result = Mongo.resetCache(userId)resp.body = json.dumps(result, encoding='utf-8')class UserDeals():def on_get(self,req,resp,userId):UserSpecificDeals.generateSheet(userId)json_docs = json.dumps({'True':'Sheet generated, mail sent.'}, default=json_util.default)resp.body = json.dumps(json_docs, encoding='utf-8')class CommonUpdate():def on_post(self,req,resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.updateCollection(result_json)resp.body = json.dumps(result, encoding='utf-8')resp.content_type = "application/json; charset=utf-8"class NegativeDeals():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllNegativeDeals(offset, limit)resp.body = dumps(result)def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addNegativeDeals(result_json, multi)resp.body = json.dumps(result, encoding='utf-8')class ManualDeals():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllManualDeals(offset, limit)resp.body = dumps(result)def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addManualDeal(result_json, multi)resp.body = json.dumps(result, encoding='utf-8')class CommonDelete():def on_post(self,req,resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.deleteDocument(result_json)resp.body = json.dumps(result, encoding='utf-8')resp.content_type = "application/json; charset=utf-8"class SearchProduct():def on_get(self,req,resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")search_term = req.get_param("search")result = Mongo.searchMaster(offset, limit, search_term)resp.body = dumps(result)class FeaturedDeals():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllFeaturedDeals(offset, limit)resp.body = dumps(result)def on_post(self, req, resp):multi = req.get_param_as_int("multi")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addFeaturedDeal(result_json, multi)resp.body = json.dumps(result, encoding='utf-8')class CommonSearch():def on_get(self,req,resp):class_name = req.get_param("class")sku = req.get_param_as_int("sku")skuBundleId = req.get_param_as_int("skuBundleId")result = Mongo.searchCollection(class_name, sku, skuBundleId)resp.body = dumps(result)class CricScore():def on_get(self,req,resp):result = Mongo.getLiveCricScore()resp.body = dumps(result)class Notification():def on_post(self, req, resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addBundleToNotification(result_json)resp.body = json.dumps(result, encoding='utf-8')def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllNotifications(offset, limit)resp.body = dumps(result)class DealBrands():def on_get(self, req, resp):category_id = req.get_param_as_int("category_id")result = Mongo.getBrandsForFilter(category_id)resp.body = dumps(result)class DealRank():def on_get(self, req, resp):identifier = req.get_param("identifier")source_id = req.get_param_as_int("source_id")user_id = req.get_param_as_int("user_id")result = Mongo.getDealRank(identifier, source_id, user_id)json_docs = json.dumps(result, default=json_util.default)resp.body = json_docsclass OrderedOffers():def on_get(self, req, resp, storeId, storeSku):storeId = int(storeId)result = Mongo.getBundleBySourceSku(storeId, storeSku)json_docs = json.dumps(result, default=json_util.default)resp.body = json_docsclass RetailerDetail():global RETAILER_DETAIL_CALL_COUNTERdef getRetryRetailer(self,failback=True):status = RETRY_MAP.get(self.callType)retailer = session.query(Retailers).filter_by(status=status).filter(Retailers.next_call_time<=datetime.now()).filter(or_(Retailers.agent_id==self.agentId, Retailers.agent_id==None)).order_by(Retailers.agent_id.desc(),Retailers.call_priority).order_by(Retailers.next_call_time).with_lockmode("update").first()if retailer is not None:lgr.info( "getRetryRetailer " + str(retailer.id))else:if failback:retailer = self.getNewRetailer(False)return retailerelse:#No further calls for nowreturn Noneretailer.status = ASSIGN_MAP.get(status)retailer.next_call_time = Nonelgr.info( "getRetryRetailer " + str(retailer.id))return retailerdef getNotActiveRetailer(self):try:user = session.query(Users).filter_by(activated=0).filter_by(status=1).filter(Users.mobile_number != None).filter(~Users.mobile_number.like("0%")).filter(Users.created>datetime(2015,06,29)).order_by(Users.created.desc()).with_lockmode("update").first()if user is None:return Noneelse:retailerContact = session.query(RetailerContacts).filter_by(mobile_number=user.mobile_number).first()if retailerContact is not None:retailer = session.query(Retailers).filter_by(id=retailerContact.retailer_id).first()else:retailer = session.query(Retailers).filter_by(contact1=user.mobile_number).first()if retailer is None:retailer = session.query(Retailers).filter_by(contact2=user.mobile_number).first()if retailer is None:retailer = Retailers()retailer.contact1 = user.mobile_numberretailer.status = 'assigned'retailer.retry_count = 0retailer.invalid_retry_count = 0retailer.is_elavated=1user.status = 2session.commit()print "retailer id", retailer.idretailer.contact = user.mobile_numberreturn retailerfinally:session.close()def getNewRetailer(self,failback=True):if self.callType == 'fresh':retailer = self.getNotActiveRetailer()if retailer is not None:return retailerretry = Trueretailer = Nonetry:while(retry):lgr.info( "Calltype " + self.callType)status=self.callTypequery = session.query(Retailers).filter(Retailers.status==status).filter(or_(Retailers.agent_id==self.agentId, Retailers.agent_id==None))if status=='fresh':query = query.filter_by(is_or=False, is_std=False).filter(Retailers.pin==Pincodeavailability.code).filter(Pincodeavailability.amount > 19999).order_by(Retailers.is_elavated.desc(), Retailers.agent_id.desc())elif status=='followup':query = query.filter(Retailers.next_call_time<=datetime.now()).order_by(Retailers.agent_id.desc(),Retailers.next_call_time)else:query = query.filter(Retailers.modified<=datetime.now()).order_by(Retailers.agent_id.desc(), Retailers.modified)retailer = query.with_lockmode("update").first()if retailer is not None:lgr.info( "retailer " +str(retailer.id))if status=="fresh":userquery = session.query(Users)if retailer.contact2 is not None:userquery = userquery.filter(Users.mobile_number.in_([retailer.contact1,retailer.contact2]))else:userquery = userquery.filter_by(mobile_number=retailer.contact1)user = userquery.first()if user is not None:retailer.status = 'alreadyuser'lgr.info( "retailer.status " + retailer.status)session.commit()continueretailer.status = 'assigned'elif status=='followup':if isActivated(retailer.id):print "Retailer Already %d activated and marked onboarded"%(retailer.id)continueretailer.status = 'fassigned'else:retailer.status = 'oassigned'retailer.retry_count = 0retailer.invalid_retry_count = 0lgr.info( "Found Retailer " + str(retailer.id) + " with status " + status + " assigned to " + str(self.agentId))else:lgr.info( "No fresh/followup retailers found")if failback:retailer = self.getRetryRetailer(False)return retailerretry=Falseexcept:print traceback.print_exc()return retailerdef on_get(self, req, resp, agentId, callType=None, retailerId=None):try:global RETAILER_DETAIL_CALL_COUNTERRETAILER_DETAIL_CALL_COUNTER += 1lgr.info( "RETAILER_DETAIL_CALL_COUNTER " + str(RETAILER_DETAIL_CALL_COUNTER))self.agentId = int(agentId)self.callType = callTypeif retailerId is not None:self.retailerId = int(retailerId)retailerLink = session.query(RetailerLinks).filter_by(retailer_id=self.retailerId).first()if retailerLink is not None:code = retailerLink.codeelse:code = self.getCode()retailerLink = RetailerLinks()retailerLink.code = coderetailerLink.agent_id = self.agentIdretailerLink.retailer_id = self.retailerIdactivationCode=Activation_Codes()activationCode.code = codesession.commit()session.close()resp.body = json.dumps({"result":{"code":code,"link":make_tiny(code)}}, encoding='utf-8')returnretryFlag = Falseif RETAILER_DETAIL_CALL_COUNTER % TOTAL >= DEALER_FRESH_FACTOR:retryFlag=Truetry:if retryFlag:retailer = self.getRetryRetailer()else:retailer = self.getNewRetailer()if retailer is None:resp.body = "{}"returnfetchInfo = FetchDataHistory()fetchInfo.agent_id = self.agentIdfetchInfo.call_type = self.callTypeagent = session.query(Agents).filter_by(id=self.agentId).first()last_disposition = session.query(CallHistory).filter_by(agent_id=self.agentId).order_by(CallHistory.id.desc()).first()if last_disposition is None or last_disposition.created < agent.last_login:fetchInfo.last_action = 'login'fetchInfo.last_action_time = agent.last_loginelse:fetchInfo.last_action = 'disposition'fetchInfo.last_action_time = last_disposition.createdfetchInfo.retailer_id = retailer.idsession.commit()otherContacts = [r for r, in session.query(RetailerContacts.mobile_number).filter_by(retailer_id=retailer.id).order_by(RetailerContacts.contact_type).all()]resp.body = json.dumps(todict(getRetailerObj(retailer, otherContacts, self.callType)), encoding='utf-8')returnfinally:session.close()if retailer is None:resp.body = "{}"else:print "It should never come here"resp.body = json.dumps(todict(getRetailerObj(retailer)), encoding='utf-8')finally:session.close()def on_post(self, req, resp, agentId, callType):returned = Falseself.agentId = int(agentId)self.callType = callTypejsonReq = json.loads(req.stream.read(), encoding='utf-8')lgr.info( "Request ----\n" + str(jsonReq))self.jsonReq = jsonReqinvalidNumber = self.invalidNumbercallLater = self.callLateralreadyUser = self.alReadyUserverifiedLinkSent = self.verifiedLinkSentonboarded = self.onboardedself.address = jsonReq.get('address')self.retailerId = int(jsonReq.get('retailerid'))self.smsNumber = jsonReq.get('smsnumber')if self.smsNumber is not None:self.smsNumber = self.smsNumber.strip().lstrip("0")try:self.retailer = session.query(Retailers).filter_by(id=self.retailerId).first()if self.address:self.retailer.address_new = self.addressself.callDisposition = jsonReq.get('calldispositiontype')self.callHistory = CallHistory()self.callHistory.agent_id=self.agentIdself.callHistory.call_disposition = self.callDispositionself.callHistory.retailer_id=self.retailerIdself.callHistory.call_type=self.callTypeself.callHistory.duration_sec = int(jsonReq.get("callduration"))self.callHistory.disposition_description = jsonReq.get('calldispositiondescritption')self.callHistory.disposition_comments = jsonReq.get('calldispositioncomments')lgr.info(self.callHistory.disposition_comments)self.callHistory.call_time = datetime.strptime(jsonReq.get("calltime"), '%d/%m/%Y %H:%M:%S')self.callHistory.mobile_number = jsonReq.get('number')self.callHistory.sms_verified = int(jsonReq.get("verified"))lastFetchData = session.query(FetchDataHistory).filter_by(agent_id=self.agentId).order_by(FetchDataHistory.id.desc()).first()if self.callDisposition == 'onboarded':self.checkList = jsonReq.get('checklist')if lastFetchData is None:raiseself.callHistory.last_fetch_time= lastFetchData.createddispositionMap = { 'call_later':callLater,'ringing_no_answer':callLater,'not_reachable':callLater,'switch_off':callLater,'not_retailer':invalidNumber,'invalid_no':invalidNumber,'wrong_no':invalidNumber,'hang_up':invalidNumber,'retailer_not_interested':invalidNumber,'recharge_retailer':invalidNumber,'accessory_retailer':invalidNumber,'service_center_retailer':invalidNumber,'alreadyuser':alreadyUser,'verified_link_sent':verifiedLinkSent,'onboarded':onboarded}returned = dispositionMap[jsonReq.get('calldispositiontype')]()finally:session.close()if returned:resp.body = "{\"result\":\"success\"}"else:resp.body = "{\"result\":\"failed\"}"def invalidNumber(self,):#self.retailer.status = 'retry' if self.callType == 'fresh' else 'fretry'if self.callDisposition == 'invalid_no':self.retailer.status='failed'self.callHistory.disposition_description = 'Invalid Number'elif self.callDisposition == 'wrong_no':self.retailer.status='failed'self.callHistory.disposition_description = 'Wrong Number'elif self.callDisposition == 'hang_up':self.retailer.status='failed'self.callHistory.disposition_description = 'Hang Up'elif self.callDisposition == 'retailer_not_interested':self.retailer.status='failed'if self.callHistory.disposition_description is None:self.callHistory.disposition_description = 'NA'self.callHistory.disposition_description = 'Reason Retailer Not Interested ' + self.callHistory.disposition_descriptionelif self.callDisposition == 'recharge_retailer':self.retailer.status='failed'self.callHistory.disposition_description = 'Recharge related. Not a retailer 'elif self.callDisposition == 'accessory_retailer':self.retailer.status='failed'self.callHistory.disposition_description = 'Accessory related. Not a retailer'elif self.callDisposition == 'service_center_retailer':self.retailer.status='failed'self.callHistory.disposition_description = 'Service Center related. Not a retailer'elif self.callDisposition == 'not_retailer':self.retailer.status='failed'self.callHistory.disposition_description = 'Not a retailer'session.commit()return Truedef getCode(self,):newCode = NonelastLink = session.query(RetailerLinks).order_by(RetailerLinks.id.desc()).with_lockmode("update").first()if lastLink is not None:if len(lastLink.code)==len(codesys):newCode=lastLink.codereturn getNextCode(codesys, newCode)def callLater(self,):self.retailer.status = RETRY_MAP.get(self.callType)self.retailer.call_priority = Noneif self.callDisposition == 'call_later':if self.callHistory.disposition_description is not None:self.retailer.call_priority = 'user_initiated'self.retailer.next_call_time = datetime.strptime(self.callHistory.disposition_description, '%d/%m/%Y %H:%M:%S')self.callHistory.disposition_description = 'User requested to call on ' + self.callHistory.disposition_descriptionelse:self.retailer.call_priority = 'system_initiated'self.retailer.next_call_time = self.callHistory.call_time + timedelta(days=1)self.callHistory.disposition_description = 'Call scheduled on ' + datetime.strftime(self.retailer.next_call_time, '%d/%m/%Y %H:%M:%S')else:if self.callDisposition == 'ringing_no_answer':if self.retailer.disposition == 'ringing_no_answer':self.retailer.retry_count += 1else:self.retailer.disposition = 'ringing_no_answer'self.retailer.retry_count = 1else:if self.retailer.disposition == 'ringing_no_answer':passelse:self.retailer.disposition = 'not_reachable'self.retailer.retry_count += 1self.retailer.invalid_retry_count += 1retryConfig = session.query(RetryConfig).filter_by(call_type=self.callType, disposition_type=self.retailer.disposition, retry_count=self.retailer.retry_count).first()if retryConfig is not None:self.retailer.next_call_time = self.callHistory.call_time + timedelta(minutes = retryConfig.minutes_ahead)self.callHistory.disposition_description = 'Call scheduled on ' + datetime.strftime(self.retailer.next_call_time, '%d/%m/%Y %H:%M:%S')else:self.retailer.status = 'failed'self.callHistory.disposition_description = 'Call failed as all attempts exhausted'session.commit()return Truedef alReadyUser(self,):self.retailer.status = self.callDispositionif self.callHistory.disposition_description is None:self.callHistory.disposition_description = 'Retailer already user'session.commit()return Truedef verifiedLinkSent(self,):if self.callType == 'fresh':self.retailer.status = 'followup'if self.retailer.agent_id not in sticky_agents:self.retailer.agent_id = Noneself.retailer.next_call_time = self.callHistory.call_time + timedelta(days=1)self.callHistory.disposition_description = 'App link sent via ' + self.callHistory.disposition_description+ '. followup on' + datetime.strftime(self.retailer.next_call_time, '%d/%m/%Y %H:%M:%S')else:self.retailer.status = 'followup'if self.retailer.agent_id not in sticky_agents:self.retailer.agent_id = Noneself.retailer.next_call_time = self.callHistory.call_time + timedelta(days=7)self.callHistory.disposition_description = 'App link sent via' + self.callHistory.disposition_description + '. Followup again on ' + datetime.strftime(self.retailer.next_call_time, '%d/%m/%Y %H:%M:%S')addContactToRetailer(self.agentId, self.retailerId, self.smsNumber, self.callType, 'sms')session.commit()return Truedef onboarded(self,):self.retailer.status = self.callDispositioncheckList = OnboardedRetailerChecklists()checkList.contact_us = self.checkList.get('contactus')checkList.doa_return_policy = self.checkList.get('doareturnpolicy')checkList.number_verification = self.checkList.get('numberverification')checkList.payment_option = self.checkList.get('paymentoption')checkList.preferences = self.checkList.get('preferences')checkList.product_info = self.checkList.get('productinfo')checkList.redeem = self.checkList.get('redeem')checkList.retailer_id = self.retailerIdsession.commit()return Truedef isActivated(retailerId):retailerLink = session.query(RetailerLinks).filter_by(retailer_id=retailerId).first()user = session.query(Users).filter(or_(func.lower(Users.referrer)==retailerLink.code.lower(), Users.utm_campaign==retailerLink.code)).first()if user is None:mobileNumbers = list(session.query(RetailerContacts.mobile_number).filter_by(retailer_id=retailerId).all())user = session.query(Users).filter(Users.mobile_number.in_(mobileNumbers)).first()if user is None:if retailerLink.created < datetime(2015,5,26):historyNumbers = [number for number, in session.query(CallHistory.mobile_number).filter_by(retailer_id = retailerId).all()]user = session.query(Users).filter(Users.mobile_number.in_(historyNumbers)).first()if user is None:return Falseelse:mapped_with = 'contact'else:return Falseelse:mapped_with = 'contact'else:mapped_with = 'code'retailerLink.mapped_with = mapped_withif user.activation_time is not None:retailerLink.activated = user.activation_timeretailerLink.activated = user.createdretailerLink.user_id = user.idretailer = session.query(Retailers).filter_by(id=retailerId).first()if retailer.status == 'followup' or retailer.status == 'fretry':retailer.status = 'onboarding'if retailer.agent_id not in sticky_agents:retailer.agent_id = Noneretailer.call_priority = Noneretailer.next_call_time = Noneretailer.retry_count = 0retailer.invalid_retry_count = 0session.commit()print "retailerLink.retailer_id", retailerLink.retailer_idprint "retailer", retailer.idsession.close()return Trueclass AddContactToRetailer():def on_post(self,req,resp, agentId):agentId = int(agentId)try:jsonReq = json.loads(req.stream.read(), encoding='utf-8')retailerId = int(jsonReq.get("retailerid"))mobile = jsonReq.get("mobile")callType = jsonReq.get("calltype")contactType = jsonReq.get("contacttype")addContactToRetailer(agentId, retailerId, mobile, callType, contactType)session.commit()finally:session.close()class AddAddressToRetailer():def on_post(self,req,resp, agentId):agentId = int(agentId)jsonReq = json.loads(req.stream.read(), encoding='utf-8')retailerId = int(jsonReq.get("retailerid"))address = str(jsonReq.get("address"))storeName = str(jsonReq.get("storename"))pin = str(jsonReq.get("pin"))city = str(jsonReq.get("city"))state = str(jsonReq.get("state"))updateType = str(jsonReq.get("updatetype"))addAddressToRetailer(agentId, retailerId, address, storeName, pin, city,state, updateType)def addContactToRetailer(agentId, retailerId, mobile, callType, contactType):retailerContact = session.query(RetailerContacts).filter_by(retailer_id=retailerId).filter_by(mobile_number=mobile).first()if retailerContact is None:retailerContact = RetailerContacts()retailerContact.retailer_id = retailerIdretailerContact.agent_id = agentIdretailerContact.call_type = callTyperetailerContact.contact_type = contactTyperetailerContact.mobile_number = mobileelse:if CONTACT_PRIORITY.index(retailerContact.contact_type) > CONTACT_PRIORITY.index(contactType):retailerContact.contact_type = contactTypedef addAddressToRetailer(agentId, retailerId, address, storeName, pin, city,state, updateType):print "I am in addAddress"print agentId, retailerId, address, storeName, pin, city, state, updateTypetry:if updateType=='new':retailer = session.query(Retailers).filter_by(id=retailerId).first()retailer.address = addressretailer.title = storeNameretailer.city = cityretailer.state = stateretailer.pin = pinraddress = RetailerAddresses()raddress.address = addressraddress.title = storeNameraddress.agent_id = agentIdraddress.city = cityraddress.pin = pinraddress.retailer_id = retailerIdraddress.state = statesession.commit()finally:session.close()class Login():def on_get(self, req, resp, agentId, role):try:self.agentId = int(agentId)self.role = roleprint str(self.agentId) + self.role;agents=AgentLoginTimings()lastLoginTime = session.query(Agents).filter(Agents.id==self.agentId).first()print 'lastLogintime' + str(lastLoginTime)agents.loginTime=lastLoginTime.last_loginagents.logoutTime=datetime.now()agents.role =self.roleagents.agent_id = self.agentIdsession.add(agents)session.commit()resp.body = json.dumps({"result":{"success":"true","message":"Success"}}, encoding='utf-8')finally:session.close()def on_post(self,req,resp):try:jsonReq = json.loads(req.stream.read(), encoding='utf-8')lgr.info( "Request ----\n" + str(jsonReq))email=jsonReq.get('email')password = jsonReq.get('password')role=jsonReq.get('role')agent = session.query(Agents).filter(and_(Agents.email==email,Agents.password==password)).first()if agent is None:resp.body = json.dumps({"result":{"success":"false","message":"Invalid User"}}, encoding='utf-8')else:print agent.idcheckRole = session.query(Agent_Roles.id).filter(and_(Agent_Roles.agent_id==agent.id,Agent_Roles.role==role)).first()if checkRole is None:resp.body = json.dumps({"result":{"success":"false","message":"Invalid Role"}}, encoding='utf-8')else:agent.last_login = datetime.now()agent.login_type = roleresp.body = json.dumps({"result":{"success":"true","message":"Valid User","id":agent.id}}, encoding='utf-8')session.commit()#session.query(Agents).filter_by(id = checkUser[0]).finally:session.close()def test(self,email,password,role):checkUser = session.query(Agents.id).filter(and_(Agents.email==email,Agents.password==password)).first()if checkUser is None:print checkUserelse:print checkUser[0]checkRole = session.query(Agent_Roles.id).filter(and_(Agent_Roles.agent_id==checkUser[0],Agent_Roles.role==role)).first()if checkRole is None:passelse:agents=AgentLoginTimings()agents.loginTime=datetime.now()agents.logoutTime=datetime.now()agents.role =roleagents.agent_id = 2#session.query(AgentLoginTimings).filter_by(id = checkUser[0]).update({"last_login":datetime.now()}, synchronize_session=False)session.add(agents)session.commit()session.close()#session.query(Agents).filter(Agents.id==checkUser[0]).update({"last_login":Agents.last_login})class RetailerActivation():def on_get(self, req, resp, userId):res = markDealerActivation(int(userId))if res:resp.body = "{\"activated\":true}"else:resp.body = "{\"activated\":false}"def markDealerActivation(userId):try:user = session.query(Users).filter_by(id=userId).first()result = FalsemappedWith = 'contact'retailer = Noneif user is not None:referrer = None if user.referrer is None else user.referrer.upper()retailerLink = session.query(RetailerLinks).filter(or_(RetailerLinks.code==referrer, RetailerLinks.code==user.utm_campaign)).first()if retailerLink is None:if user.mobile_number is not None:retailerContact = session.query(RetailerContacts).filter_by(mobile_number=user.mobile_number).first()if retailerContact is None:retailer = session.query(Retailers).filter(Retailers.status.in_(['followup', 'fretry', 'fdone'])).filter(or_(Retailers.contact1==user.mobile_number,Retailers.contact2==user.mobile_number)).first()else:retailer = session.query(Retailers).filter_by(id = retailerContact.retailer_id).first()else:retailer = session.query(Retailers).filter_by(id = retailerLink.retailer_id).first()mappedWith='code'if retailer is not None:retailerLink = session.query(RetailerLinks).filter_by(retailer_id=retailer.id).first()if retailerLink is not None:retailerLink.user_id = user.idretailerLink.mapped_with=mappedWithretailer.status = 'onboarding'result = Truesession.commit()return resultfinally:session.close()def todict(obj, classkey=None):if isinstance(obj, dict):data = {}for (k, v) in obj.items():data[k] = todict(v, classkey)return dataelif hasattr(obj, "_ast"):return todict(obj._ast())elif hasattr(obj, "__iter__"):return [todict(v, classkey) for v in obj]elif hasattr(obj, "__dict__"):data = dict([(key, todict(value, classkey))for key, value in obj.__dict__.iteritems()if not callable(value) and not key.startswith('_')])if classkey is not None and hasattr(obj, "__class__"):data[classkey] = obj.__class__.__name__return dataelse:return objdef getRetailerObj(retailer, otherContacts1=None, callType=None):print "before otherContacts1",otherContacts1otherContacts = [] if otherContacts1 is None else otherContacts1print "after otherContacts1",otherContactsobj = Mock()obj.id = retailer.idif retailer.contact1 is not None and retailer.contact1 not in otherContacts:otherContacts.append(retailer.contact1)if retailer.contact2 is not None and retailer.contact2 not in otherContacts:otherContacts.append(retailer.contact2)obj.contact1 = None if len(otherContacts)==0 else otherContacts[0]if obj.contact1 is not None:obj.contact2 = None if len(otherContacts)==1 else otherContacts[1]obj.scheduled = (retailer.call_priority is not None)address = Nonetry:address = session.query(RetailerAddresses).filter_by(retailer_id=retailer.id).order_by(RetailerAddresses.created.desc()).first()finally:session.close()if address is not None:obj.address = address.addressobj.title = address.titleobj.city = address.cityobj.state = address.stateobj.pin = address.pinelse:obj.address = retailer.address_new if retailer.address_new is not None else retailer.addressobj.title = retailer.titleobj.city = retailer.cityobj.state = retailer.stateobj.pin = retailer.pinobj.status = retailer.statusif hasattr(retailer, 'contact'):obj.contact = retailer.contactif callType == 'onboarding':try:userId, activatedTime = session.query(RetailerLinks.user_id, RetailerLinks.activated).filter(RetailerLinks.retailer_id==retailer.id).first()activated, = session.query(Users.activation_time).filter(Users.id==userId).first()if activated is not None:activatedTime = activatedobj.user_id = userIdobj.created = datetime.strftime(activatedTime, '%d/%m/%Y %H:%M:%S')result = fetchResult("select * from useractive where user_id=%d"%(userId))if result == ():obj.last_active = Noneelse:obj.last_active =datetime.strftime(result[0][1], '%d/%m/%Y %H:%M:%S')ordersCount = session.query(Orders).filter_by(user_id = userId).filter(~Orders.status.in_(['ORDER_NOT_CREATED_KNOWN', 'ORDER_ALREADY_CREATED_IGNORED'])).count()obj.orders = ordersCountfinally:session.close()return objdef make_tiny(code):url = 'https://play.google.com/store/apps/details?id=com.saholic.profittill&referrer=utm_source%3D0%26utm_medium%3DCRM%26utm_term%3D001%26utm_campaign%3D' + code#url='https://play.google.com/store/apps/details?id=com.saholic.profittill&referrer=utm_source=0&utm_medium=CRM&utm_term=001&utm_campaign='+code#marketUrl='market://details?id=com.saholic.profittill&referrer=utm_source=0&utm_medium=CRM&utm_term=001&utm_campaign='+code#url = urllib.quote(marketUrl)#request_url = ('http://tinyurl.com/api-create.php?' + urlencode({'url':url}))#filehandle = urllib2.Request(request_url)#x= urllib2.urlopen(filehandle)try:shortener = Shortener('TinyurlShortener')returnUrl = shortener.short(url)except:shortener = Shortener('SentalaShortener')returnlUrl = shortener.short(url)return returnUrlclass SearchUser():def on_post(self, req, resp, agentId, searchType):retailersJsonArray = []try:jsonReq = json.loads(req.stream.read(), encoding='utf-8')lgr.info( "Request in Search----\n" + str(jsonReq))contact=jsonReq.get('searchTerm')if(searchType=="number"):retailer_ids = session.query(RetailerContacts.retailer_id).filter_by(mobile_number=contact).all()retailer_ids = [r for r, in retailer_ids]anotherCondition = or_(Retailers.contact1==contact,Retailers.contact2==contact, Retailers.id.in_(retailer_ids))else:m = re.match("(.*?)(\d{6})(.*?)", contact)if m is not None:pin = m.group(2)contact = m.group(1) if m.group(1) != '' else m.group(3)anotherCondition = and_(Retailers.title.ilike('%%%s%%'%(contact)), Retailers.pin==pin)else:anotherCondition = Retailers.title.ilike('%%%s%%'%(contact))retailers = session.query(Retailers).filter(anotherCondition).limit(20).all()if retailers is None:resp.body = json.dumps("{}")else:for retailer in retailers:otherContacts = [r for r, in session.query(RetailerContacts.mobile_number).filter_by(retailer_id=retailer.id).order_by(RetailerContacts.contact_type).all()]retailersJsonArray.append(todict(getRetailerObj(retailer, otherContacts)))resp.body = json.dumps({"Retailers":retailersJsonArray}, encoding='utf-8')returnfinally:session.close()class Mock(object):passdef tagActivatedReatilers():retailerIds = [r for r, in session.query(RetailerLinks.retailer_id).filter_by(user_id = None).all()]session.close()for retailerId in retailerIds:isActivated(retailerId)session.close()class StaticDeals():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")categoryId = req.get_param_as_int("categoryId")direction = req.get_param_as_int("direction")result = Mongo.getStaticDeals(offset, limit, categoryId, direction)resp.body = dumps(result)class DealNotification():def on_get(self,req,resp,skuBundleIds):result = Mongo.getDealsForNotification(skuBundleIds)resp.body = dumps(result)class DealPoints():def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllBundlesWithDealPoints(offset, limit)resp.body = dumps(result)def on_post(self, req, resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addDealPoints(result_json)resp.body = json.dumps(result, encoding='utf-8')class AppAffiliates():def on_get(self, req, resp, retailerId, appId):retailerId = int(retailerId)appId = int(appId)call_back = req.get_param("callback")result = Mongo.generateRedirectUrl(retailerId, appId)resp.body = call_back+'('+str(result)+')'class AffiliatePayout():def on_get(self, req, resp):payout = req.get_param("payout")transaction_id = req.get_param("transaction_id")result = Mongo.addPayout(payout, transaction_id)resp.body = str(result)class AppOffers():def on_get(self, req, resp, retailerId):try:result = Mongo.getAppOffers(retailerId)offerids = result.values()if offerids is None or len(offerids)==0:resp.body = json.dumps("{}")else:appOffers = session.query(app_offers.id,app_offers.appmaster_id, app_offers.app_name, app_offers.affiliate_offer_id, app_offers.image_url, app_offers.downloads, app_offers.link, app_offers.offer_price, app_offers.offerCategory, app_offers.package_name, app_offers.promoImage, app_offers.ratings, case([(app_offers.override_payout == True, app_offers.overriden_payout)], else_=app_offers.user_payout).label('user_payout'), case([(appmasters.shortDescription != None, appmasters.shortDescription)], else_=None).label('shortDescription'), case([(appmasters.longDescription != None, appmasters.longDescription)], else_=None).label('longDescription'), appmasters.customerOneLiner, appmasters.retailerOneLiner,appmasters.rank, app_offers.offerCondition, app_offers.location).join((appmasters,appmasters.id==app_offers.appmaster_id)).filter(app_offers.id.in_(tuple(offerids))).all()appOffersMap = {}jsonOffersArray=[]for offer in appOffers:appOffersMap[long(offer[0])]= AppOfferObj(offer[0], offer[1], offer[2], offer[3], offer[4], offer[5], offer[6], offer[7], offer[8], offer[9], offer[10], offer[11], offer[12], offer[13], offer[14], offer[15], offer[16], offer[17], offer[18], offer[19]).__dict__for rank in sorted(result):print 'Rank', rank, 'Data', appOffersMap[result[rank]]jsonOffersArray.append(appOffersMap[result[rank]])resp.body = json.dumps({"AppOffers":jsonOffersArray}, encoding='latin1')finally:session.close()class AppUserBatchRefund():def on_get(self, req, resp, batchId, userId):try:batchId = long(batchId)userId = long(userId)userBatchCashback = user_app_cashbacks.get_by(user_id=userId, batchCreditId=batchId)if userBatchCashback is None:resp.body = json.dumps("{}")else:if userBatchCashback.creditedDate is not None:userBatchCashback.creditedDate = str(userBatchCashback.creditedDate)resp.body = json.dumps(todict(userBatchCashback), encoding='utf-8')finally:session.close()class AppUserBatchDrillDown():def on_get(self, req, resp, fortNightOfYear, userId, yearVal):try:fortNightOfYear = long(fortNightOfYear)userId = long(userId)yearVal = long(yearVal)appUserBatchDrillDown = session.query(user_app_installs.transaction_date, func.sum(user_app_installs.installCount).label('downloads'), func.sum(user_app_installs.payoutAmount).label('amount')).join((user_app_cashbacks,user_app_cashbacks.user_id==user_app_installs.user_id)).filter(user_app_cashbacks.fortnightOfYear==user_app_installs.fortnightOfYear).filter(user_app_cashbacks.user_id==userId).filter(user_app_cashbacks.yearVal==yearVal).filter(user_app_cashbacks.fortnightOfYear==fortNightOfYear).group_by(user_app_installs.transaction_date).all()cashbackArray = []if appUserBatchDrillDown is None or len(appUserBatchDrillDown)==0:resp.body = json.dumps("{}")else:for appcashBack in appUserBatchDrillDown:userAppBatchDrillDown = UserAppBatchDrillDown(str(appcashBack[0]),long(appcashBack[1]), long(appcashBack[2]))cashbackArray.append(todict(userAppBatchDrillDown))resp.body = json.dumps({"UserAppCashBackInBatch":cashbackArray}, encoding='utf-8')finally:session.close()class AppUserBatchDateDrillDown():def on_get(self, req, resp, userId, date):try:userId = long(userId)date = str(date)date = datetime.strptime(date, '%Y-%m-%d')appUserBatchDateDrillDown = session.query(user_app_installs.app_name, func.sum(user_app_installs.installCount).label('downloads'), func.sum(user_app_installs.payoutAmount).label('amount')).filter(user_app_installs.user_id==userId).filter(user_app_installs.transaction_date==date).group_by(user_app_installs.app_name).all()cashbackArray = []if appUserBatchDateDrillDown is None or len(appUserBatchDateDrillDown)==0:resp.body = json.dumps("{}")else:for appcashBack in appUserBatchDateDrillDown:userAppBatchDateDrillDown = UserAppBatchDateDrillDown(str(appcashBack[0]),long(appcashBack[1]),long(appcashBack[2]))cashbackArray.append(todict(userAppBatchDateDrillDown))resp.body = json.dumps({"UserAppCashBackDateWise":cashbackArray}, encoding='utf-8')finally:session.close()class AppUserCashBack():def on_get(self, req, resp, userId, status):try:userId = long(userId)status = str(status)appUserApprovedCashBacks = user_app_cashbacks.query.filter(user_app_cashbacks.user_id==userId).filter(user_app_cashbacks.status==status).all()cashbackArray = []if appUserApprovedCashBacks is None or len(appUserApprovedCashBacks)==0:resp.body = json.dumps("{}")else:totalAmount = 0for appUserApprovedCashBack in appUserApprovedCashBacks:totalAmount = totalAmount + appUserApprovedCashBack.amountif appUserApprovedCashBack.creditedDate is not None:appUserApprovedCashBack.creditedDate = str(appUserApprovedCashBack.creditedDate)cashbackArray.append(todict(appUserApprovedCashBack))resp.body = json.dumps({"UserAppCashBack":cashbackArray,"TotalAmount":totalAmount}, encoding='utf-8')finally:session.close()class GetSaleDetail():def on_get(self,req,res,date_val):try:db = get_mongo_connection()sum=0count=0#print date_val, type(date_val)#cursor = db.Dtr.merchantOrder.find({'createdOnInt':{'$lt':long(date_val)},'subOrders':{'$exists':True}})cursor = db.Dtr.merchantOrder.find({"$and": [ { "createdOnInt":{"$gt":long(date_val)} }, {"createdOnInt":{"$lt":long(date_val)+86401} } ],"subOrders":{"$exists":True}})for document in cursor:for doc in document['subOrders']:sum = sum + float(doc['amountPaid'])count = count + int(doc['quantity'])data = {"amount":sum , "quantity":count}res.body= json.dumps(data,encoding='utf-8')finally:session.close()class DummyDeals():def on_get(self,req, resp):categoryId = req.get_param_as_int("categoryId")offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getDummyDeals(categoryId, offset, limit)resp.body = dumps(result)class PincodeValidation():def on_get(self,req,resp,pincode):json_data={}cities=[]print pincodeif len(str(pincode)) ==6:listCities = list(session.query(Postoffices.taluk,Postoffices.state).distinct().filter(Postoffices.pincode==pincode).filter(Postoffices.taluk!='NA').all())if len(listCities)>0:for j in listCities:cities.append(j.taluk)json_data['cities'] = citiesjson_data['state'] = listCities[0][1]resp.body = json.dumps(json_data)else:resp.body = json.dumps("{}")else:resp.body = json.dumps("{}")session.close()class SearchDummyDeals():def on_get(self,req,resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")searchTerm = req.get_param("searchTerm")result = Mongo.searchDummyDeals(searchTerm, limit, offset)resp.body = dumps(result)class DummyPricing:def on_get(self, req, resp):skuBundleId = req.get_param_as_int("skuBundleId")result = Mongo.getDummyPricing(skuBundleId)resp.body = dumps(result)class DealObject:def on_post(self, req, resp):update = req.get_param_as_int("update")try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')if update is None:result = Mongo.addDealObject(result_json)else:result = Mongo.updateDealObject(result_json)resp.body = dumps(result)def on_get(self, req, resp):edit = req.get_param_as_int("edit")if edit is None:offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllDealObjects(offset, limit)resp.body = dumps(result)else:id = req.get_param_as_int("id")result = Mongo.getDealObjectById(id)resp.body = dumps(result)class DeleteDealObject:def on_get(self, req, resp, id):result = Mongo.deleteDealObject(int(id))resp.body = dumps(result)class FeaturedDealObject:def on_get(self, req, resp):offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getAllFeaturedDealsForDealObject(offset, limit)resp.body = dumps(result)def on_post(self, req, resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.addFeaturedDealForDealObject(result_json)resp.body = json.dumps(result, encoding='utf-8')class DeleteFeaturedDealObject:def on_get(self, req, resp, id):result = Mongo.deleteFeaturedDealObject(int(id))resp.body = dumps(result)class SubCategoryFilter:def on_get(self, req, resp):category_id = req.get_param_as_int("category_id")result = Mongo.getSubCategoryForFilter(category_id)resp.body = dumps(result)class DummyLogin:def on_post(self,req,resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.dummyLogin(result_json)resp.body = json.dumps(result, encoding='utf-8')class DummyRegister:def on_post(self,req,resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.dummyRegister(result_json)resp.body = json.dumps(result, encoding='utf-8')class UpdateUser:def on_post(self,req,resp):try:result_json = json.loads(req.stream.read(), encoding='utf-8')except ValueError:raise falcon.HTTPError(falcon.HTTP_400,'Malformed JSON','Could not decode the request body. The ''JSON was incorrect.')result = Mongo.updateDummyUser(result_json)resp.body = json.dumps(result, encoding='utf-8')class FetchUser:def on_get(self,req,resp):user_id = req.get_param_as_int("user_id")result = Mongo.getDummyUser(user_id)resp.body = json.dumps(result, encoding='utf-8')class SearchSubCategory:def on_get(self,req, resp):subCategoryIds = req.get_param("subCategoryId")searchTerm = req.get_param("searchTerm")offset = req.get_param_as_int("offset")limit = req.get_param_as_int("limit")result = Mongo.getDealsForSearchText(subCategoryIds, searchTerm,offset, limit)resp.body = json.dumps(result, encoding='utf-8')class SearchSubCategoryCount:def on_get(self,req, resp):subCategoryIds = req.get_param("subCategoryId")searchTerm = req.get_param("searchTerm")result = Mongo.getCountForSearchText(subCategoryIds, searchTerm)resp.body = json.dumps(result, encoding='utf-8')class MessageEncryption:def on_get(self,req,resp):message_type = req.get_param("type")if message_type == 'encrypt':encryption_data = req.get_param("data")encrypted_data = encryptMessage(base64.decodestring(encryption_data))resp.body = json.dumps({"result":{"value":encrypted_data}}, encoding='utf-8')elif message_type == 'decrypt':decryption_data = req.get_param("data")decrypted_data = decryptMessage(decryption_data)resp.body = json.dumps({"result":{"value":decrypted_data}}, encoding='utf-8')else:resp.body = json.dumps({}, encoding='utf-8')class GetUserCrmApplication:def on_get(self,req,resp):global USER_DETAIL_MAPproject_name = req.get_param("project_name")USER_DETAIL_MAP[project_name]+=1lgr.info( "USER_DETAIL_CALL_COUNTER " + str(USER_DETAIL_MAP[project_name]))retryFlag=Falseif USER_DETAIL_MAP[project_name] % TOTAL_USER >= USER_CRM_DEFAULT_FRESH_FACTOR:retryFlag=Trueif project_name == 'accs_cart':if retryFlag:user = self.getRetryUser(1)else:user = self.getNewUser(1)if user is None:resp.body ={}else:user.status = 'assigned'user.agent_id = req.get_param("agent_id")user.counter=1user.modified = datetime.now()session.commit()resp.body = json.dumps(todict(getUserObject(user)), encoding='utf-8')session.close()if project_name == 'accs_active':if retryFlag:user = self.getRetryUser(2)else:user = self.getNewUser(2)if user is None:resp.body ={}else:user.status = 'assigned'user.agent_id = req.get_param("agent_id")user.counter=1user.modified = datetime.now()session.commit()resp.body = json.dumps(todict(getUserObject(user)), encoding='utf-8')session.close()if project_name == 'accs_order':if retryFlag:user = self.getRetryUser(3)else:user = self.getNewUser(3)if user is None:resp.body ={}else:user.status = 'assigned'user.agent_id = req.get_param("agent_id")user.counter=1user.modified = datetime.now()session.commit()resp.body = json.dumps(todict(getUserObject(user)), encoding='utf-8')session.close()def getRetryUser(self,projectId,failback=True):status = "retry"user = session.query(UserCrmCallingData).filter_by(status=status,project_id=projectId).filter(UserCrmCallingData.next_call_time<=datetime.now()).filter(~(UserCrmCallingData.contact1).like('')).order_by(UserCrmCallingData.next_call_time).with_lockmode("update").first()if user is not None:lgr.info( "getRetryUser " + str(user.id))else:if failback:user = self.getNewUser(projectId,False)return userelse:return Nonereturn userdef getNewUser(self,projectId,failback=True):user = Nonetry:user = session.query(UserCrmCallingData).filter_by(project_id=projectId).filter(UserCrmCallingData.status=='new').filter(UserCrmCallingData.pincode_servicable==True).filter(UserCrmCallingData.user_available==0).filter(UserCrmCallingData.contact1!=None).filter(~(UserCrmCallingData.contact1).like('')).order_by(UserCrmCallingData.next_call_time).order_by(UserCrmCallingData.id).with_lockmode("update").first()if user is None:insertUserCrmData(projectId)user = session.query(UserCrmCallingData).filter_by(project_id=projectId).filter(UserCrmCallingData.status=='new').filter(UserCrmCallingData.pincode_servicable==True).filter(~(UserCrmCallingData.contact1).like('')).filter(UserCrmCallingData.user_available==0).filter(UserCrmCallingData.contact1!=None).order_by(UserCrmCallingData.next_call_time).order_by(UserCrmCallingData.id).with_lockmode("update").first()if user is not None:lgr.info( "getNewUser " + str(user.id))else:if failback:user = self.getRetryUser(projectId,False)return userelse:return Noneexcept:print traceback.print_exc()return userdef on_post(self, req, resp):returned = Falseself.agentId = req.get_param("agent_id")project_name = req.get_param("project_name")if project_name == 'accs_cart':project_id=1elif project_name == 'accs_active':project_id=2elif project_name == 'accs_order':project_id=3jsonReq = json.loads(req.stream.read(), encoding='utf-8')lgr.info( "Request ----\n" + str(jsonReq))self.jsonReq = jsonReqself.callType="default"callLaterAccs = self.callLaterAccsaccsDisposition = self.accsDispositionaccsOrderDisposition=self.accsOrderDispositionself.userId = int(jsonReq.get('user_id'))try:self.user = session.query(UserCrmCallingData).filter_by(user_id=self.userId).filter(UserCrmCallingData.project_id==project_id).first()self.callDisposition = jsonReq.get('calldispositiontype')self.callHistoryCrm = CallHistoryCrm()self.callHistoryCrm.agent_id=self.agentIdself.callHistoryCrm.project_id = project_idself.callHistoryCrm.call_disposition = self.callDispositionself.callHistoryCrm.user_id=self.userIdself.callHistoryCrm.duration_sec = int(jsonReq.get("callduration"))self.callHistoryCrm.disposition_comments = jsonReq.get('calldispositioncomments')self.callHistoryCrm.call_time = datetime.strptime(jsonReq.get("calltime"), '%d/%m/%Y %H:%M:%S')self.callHistoryCrm.mobile_number = jsonReq.get('number')self.inputs = jsonReq.get("inputs")dispositionMap = { 'call_later':callLaterAccs,'ringing_no_answer':callLaterAccs,'not_reachable':callLaterAccs,'switch_off':callLaterAccs,'technical_issue':accsDisposition,'pricing_issue':accsDisposition,'shipping_issue':accsDisposition,'internet_issue':accsDisposition,'checking_price':accsDisposition,'order_process':accsDisposition,'placed_order':accsDisposition,'place_order':accsDisposition,'product_availability':accsOrderDisposition,'return_replacement':accsOrderDisposition,'already_purchased':accsOrderDisposition,'product_quality_issue':accsOrderDisposition,'delayed_delivery':accsOrderDisposition,'other_complaint':accsOrderDisposition,'not_dealing_accessories':accsOrderDisposition}returned = dispositionMap[jsonReq.get('calldispositiontype')]()finally:session.close()if returned:resp.body = "{\"result\":\"success\"}"else:resp.body = "{\"result\":\"failed\"}"def accsOrderDisposition(self):self.user.status='done'self.user.user_available = 1self.user.disposition=self.callDispositionself.user.modified = datetime.now()if self.callDisposition == 'product_availability':self.callHistoryCrm.disposition_description='Product Not available'elif self.callDisposition == 'return_replacement':self.callHistoryCrm.disposition_description='Return or replacement pending'utils.sendCrmProjectMail(self.userId,self.callHistoryCrm.disposition_description,self.callHistoryCrm.disposition_comments)CrmTicketDtr.addTicket(self, self.userId, self.callHistoryCrm.disposition_description, self.callHistoryCrm.disposition_comments)elif self.callDisposition == 'already_purchased':self.callHistoryCrm.disposition_description='Already purchased required stock'elif self.callDisposition == 'product_quality_issue':self.callHistoryCrm.disposition_description='Product Quality issue'utils.sendCrmProjectMail(self.userId,self.callHistoryCrm.disposition_description,self.callHistoryCrm.disposition_comments)elif self.callDisposition == 'delayed_delivery':self.callHistoryCrm.disposition_description='Delayed Delivery'utils.sendCrmProjectMail(self.userId,self.callHistoryCrm.disposition_description,self.callHistoryCrm.disposition_comments)elif self.callDisposition == 'other_complaint':self.callHistoryCrm.disposition_description='Other complaint with profitmandi'utils.sendCrmProjectMail(self.userId,self.callHistoryCrm.disposition_description,self.callHistoryCrm.disposition_comments)elif self.callDisposition == 'not_dealing_accessories':self.callHistoryCrm.disposition_description='Not Dealing in Accessories/Not Interested'session.commit()jdata = self.inputsjdata = json.loads(jdata)if jdata:for d in jdata:for key, value in d.iteritems():productpricingInputs = ProductPricingInputs()productpricingInputs.user_id = self.callHistoryCrm.user_idproductpricingInputs.agent_id = self.callHistoryCrm.agent_idproductpricingInputs.disposition_id = self.callHistoryCrm.idproductpricingInputs.user_id = self.callHistoryCrm.user_idproductpricingInputs.call_disposition = self.callHistoryCrm.call_dispositionproductpricingInputs.project_id = self.callHistoryCrm.project_idproductpricingInputs.product_input = keyproductpricingInputs.pricing_input = valuesession.commit()return Truedef accsDisposition(self):self.user.status='done'self.user.user_available = 1self.user.disposition=self.callDispositionself.user.modified = datetime.now()if self.callDisposition == 'technical_issue':self.callHistoryCrm.disposition_description='Technical issue at Profitmandi'utils.sendCrmProjectMail(self.userId,self.callHistoryCrm.disposition_description,self.callHistoryCrm.disposition_comments)elif self.callDisposition == 'pricing_issue':self.callHistoryCrm.disposition_description='Pricing Issue(High)'elif self.callDisposition == 'shipping_issue':self.callHistoryCrm.disposition_description='Shipping charges related issue'elif self.callDisposition == 'internet_issue':self.callHistoryCrm.disposition_description='Internet issue at user end'elif self.callDisposition == 'checking_price':self.callHistoryCrm.disposition_description='Checking price'elif self.callDisposition == 'order_process':self.callHistoryCrm.disposition_description='Order Process inquery'elif self.callDisposition == 'placed_order':self.callHistoryCrm.disposition_description='Placed Order from Different Account'elif self.callDisposition == 'place_order':self.callHistoryCrm.disposition_description='Will Place Order'session.commit()jdata = self.inputsjdata = json.loads(jdata)if jdata:for d in jdata:for key, value in d.iteritems():productpricingInputs = ProductPricingInputs()productpricingInputs.user_id = self.callHistoryCrm.user_idproductpricingInputs.agent_id = self.callHistoryCrm.agent_idproductpricingInputs.disposition_id = self.callHistoryCrm.idproductpricingInputs.user_id = self.callHistoryCrm.user_idproductpricingInputs.call_disposition = self.callHistoryCrm.call_dispositionproductpricingInputs.project_id = self.callHistoryCrm.project_idproductpricingInputs.product_input = keyproductpricingInputs.pricing_input = valuesession.commit()return Truedef callLaterAccs(self):self.user.status='retry'self.user.modified = datetime.now()if self.callDisposition == 'call_later':if self.callHistoryCrm.disposition_comments is not None:self.user.next_call_time = datetime.strptime(self.callHistoryCrm.disposition_comments, '%d/%m/%Y %H:%M:%S')self.callHistoryCrm.disposition_description = 'User requested to call'self.callHistoryCrm.disposition_comments = self.callHistoryCrm.disposition_commentselse:self.user.next_call_time = self.callHistoryCrm.call_time + timedelta(days=1)self.callHistoryCrm.disposition_description = 'Call Scheduled'self.callHistoryCrm.disposition_comments = datetime.strftime(self.user.next_call_time, '%d/%m/%Y %H:%M:%S')else:self.callHistoryCrm.disposition_description = 'User was not contactable'if self.callDisposition == 'ringing_no_answer':if self.user.disposition == 'ringing_no_answer':self.user.retry_count += 1else:self.user.disposition = 'ringing_no_answer'self.user.retry_count = 1else:if self.user.disposition == 'ringing_no_answer':passelse:self.user.disposition = 'not_reachable'self.user.retry_count += 1self.user.invalid_retry_count += 1retryConfig = session.query(RetryConfig).filter_by(call_type=self.callType, disposition_type=self.user.disposition, retry_count=self.user.retry_count).first()if retryConfig is not None:self.user.next_call_time = self.callHistoryCrm.call_time + timedelta(minutes = retryConfig.minutes_ahead)self.callHistoryCrm.disposition_description = 'Call scheduled on ' + datetime.strftime(self.user.next_call_time, '%d/%m/%Y %H:%M:%S')else:self.user.status = 'failed'self.user.user_available=1self.callHistoryCrm.disposition_description = 'Call failed as all attempts exhausted'self.user.disposition=self.callDispositionsession.commit()jdata =self.inputsjdata = json.loads(jdata)if jdata:for d in jdata:for key, value in d.iteritems():productpricingInputs = ProductPricingInputs()productpricingInputs.user_id = self.callHistoryCrm.user_idproductpricingInputs.agent_id = self.callHistoryCrm.agent_idproductpricingInputs.disposition_id = self.callHistoryCrm.idproductpricingInputs.user_id = self.callHistoryCrm.user_idproductpricingInputs.call_disposition = self.callHistoryCrm.call_dispositionproductpricingInputs.project_id = self.callHistoryCrm.project_idproductpricingInputs.product_input = keyproductpricingInputs.pricing_input = valuesession.commit()return Truedef insertUserCrmData(project_id):if project_id==1:getCartDetailsUser()if project_id==2:getCartTabsUser()if project_id==3:getAccsRepeatUsers(project_id)def getAccsRepeatUsers(project_id):usersMasterList=[]ACCS_ALL_ORDERS = "select distinct user_id from allorder where category in ('Accs','Accessories') and store_id='spice';"result = fetchResult(ACCS_ALL_ORDERS)for r in result:usersMasterList.append(r[0])ACCS_LAST_FIFTEEN_DAYS = "select distinct user_id from allorder where category in ('Accs','Accessories') and store_id='spice' and (date(created_on) between date(curdate() - interval 15 day) and date(curdate())) order by user_id;"result = fetchResult(ACCS_LAST_FIFTEEN_DAYS)for r in result:if r[0] in usersMasterList:usersMasterList.remove(r[0])PRIOPRITIZING_USER_QUERY = "select user_id from allorder where category in ('Accs','Accessories')and store_id ='spice' and user_id in (%s)" % ",".join(map(str,usersMasterList)) + "group by user_id order by sum(amount_paid) desc;"userFinalList=[]result = fetchResult(PRIOPRITIZING_USER_QUERY)for r in result:userFinalList.append(r[0])counter=0for i in userFinalList:try:userId=iif counter==20:breakuserPresent = session.query(UserCrmCallingData).filter_by(user_id=userId).order_by(desc(UserCrmCallingData.modified)).first()if userPresent is not None:if userPresent.user_available==1:if userPresent.project_id==project_id:if userPresent.modified.date()>=(datetime.now().date()-timedelta(days=30)):continueelse:passelif userPresent.modified.date()>=(datetime.now().date()-timedelta(days=3)):continueelse:continuecounter=counter+1userMasterData = UserCrmCallingData()userMasterData.user_id = userIduserMasterData.name =getUsername(userId)userMasterData.project_id = project_iduserMasterData.user_available=0userMasterData.contact1 = getUserContactDetails(userId)userMasterData.counter = 0userMasterData.retry_count=0userMasterData.invalid_retry_count=0userMasterData.created = datetime.now()userMasterData.modified = datetime.now()userMasterData.status = 'new'userMasterData.pincode_servicable = checkPincodeServicable(userId)session.commit()except:print traceback.print_exc()finally:session.close()def getCartDetailsUser():userList=[]orderUserList=[]userMasterMap={}queryfilter = {"$and":[{'created':{"$gte":(to_java_date(datetime.now())-3*86400000)}},{'created':{"$lte":(to_java_date(datetime.now())- 43200000)}},{"url":{"$regex" : "http://api.profittill.com/cartdetails"}}]}result = get_mongo_connection_dtr_data().User.browsinghistories.find(queryfilter).distinct('user_id')for r in result:userList.append(r)myquery = "select a.user_id from allorder a join users u on a.user_id=u.id where a.store_id='spice' and (a.category='Accs' or a.category='Accessories') and u.referrer not like 'emp%%' and u.mobile_number IS NOT NULL and a.user_id in (%s)" % ",".join(map(str,userList)) + " UNION select id from users where lower(referrer) like 'emp%'"result = fetchResult(myquery)for r in result:orderUserList.append(r[0])finalUserList = list(set(userList) - set(orderUserList))queryfilternew = {"$and":[{'user_id':{"$in":finalUserList}},{'created':{"$gte":(to_java_date(datetime.now())-2*86400000)}},{'created':{"$lte":(to_java_date(datetime.now())- 43200000)}},{"url":{"$regex" : "http://api.profittill.com/cartdetails"}}]}itemIds = list(get_mongo_connection_dtr_data().User.browsinghistories.find(queryfilternew))for i in itemIds:if(userMasterMap.has_key(i.get('user_id'))):if userMasterMap.get(i.get('user_id')) > i.get('created'):userMasterMap[i.get('user_id')]=i.get('created')else:userMasterMap[i.get('user_id')]=i.get('created')d_sorted = sorted(zip(userMasterMap.values(), userMasterMap.keys()))addUserToTable(d_sorted,1)def addUserToTable(userList,projectId):counter=0for i in userList:try:userId=i[1]if counter==20:breakuserPresent = session.query(UserCrmCallingData).filter_by(user_id=userId).order_by(desc(UserCrmCallingData.modified)).first()if userPresent is not None:if userPresent.user_available==1:if userPresent.project_id==projectId:continueelif userPresent.modified.date()>=(datetime.now().date()-timedelta(days=30)):continueelse:continuecounter=counter+1userMasterData = UserCrmCallingData()userMasterData.user_id = userIduserMasterData.name =getUsername(userId)userMasterData.project_id = projectIduserMasterData.user_available=0userMasterData.contact1 = getUserContactDetails(userId)userMasterData.counter = 0userMasterData.retry_count=0userMasterData.invalid_retry_count=0userMasterData.created = datetime.now()userMasterData.modified = datetime.now()userMasterData.status = 'new'userMasterData.pincode_servicable = checkPincodeServicable(userId)session.commit()except:print traceback.print_exc()finally:session.close()def getCartTabsUser():userMasterList=[]userList = []userMasterMap={}queryfilter = {"$and":[{'created':{"$gte":(to_java_date(datetime.now())-2*86400000)}},{'created':{"$lte":(to_java_date(datetime.now())- 43200000)}},{"url":{"$regex" : "http://api.profittill.com/category/6"}}]}result = get_mongo_connection_dtr_data().User.browsinghistories.find(queryfilter).distinct('user_id')for r in result:userMasterList.append(r)queryfilterVisistedCart = {"$and":[{'user_id':{"$in":userMasterList}},{"url":{"$regex" : "http://api.profittill.com/cartdetails"}}]}result = get_mongo_connection_dtr_data().User.browsinghistories.find(queryfilterVisistedCart).distinct('user_id')for r in result:userList.append(r)myquery = "select user_id from allorder where store_id='spice' and (category='Accs' or category='Accessories') and user_id in (%s)" % ",".join(map(str,userMasterList)) + " UNION select id from users where lower(referrer) like 'emp%'"result = fetchResult(myquery)for r in result:if r[0] in userList:continueuserList.append(r[0])finalUserList = list(set(userMasterList) - set(userList))queryfilternew = {"$and":[{'user_id':{"$in":finalUserList}},{'created':{"$gte":(to_java_date(datetime.now())-2*86400000)}},{'created':{"$lte":(to_java_date(datetime.now())- 43200000)}},{"url":{"$regex" : "http://api.profittill.com/category/6"}}]}itemIds = list(get_mongo_connection_dtr_data().User.browsinghistories.find(queryfilternew))for i in itemIds:if(userMasterMap.has_key(i.get('user_id'))):if userMasterMap.get(i.get('user_id')) > i.get('created'):userMasterMap[i.get('user_id')]=i.get('created')else:userMasterMap[i.get('user_id')]=i.get('created')d_sorted = sorted(zip(userMasterMap.values(), userMasterMap.keys()))addUserToTable(d_sorted,2)def getUserContactDetails(userId):r = session.query(Users.mobile_number).filter_by(id=userId).first()if r is None:return Noneelse:return r[0]def getUsername(userId):r = session.query(Users.first_name).filter_by(id=userId).first()if r is None:return Noneelse:return r[0]def checkPincodeServicable(userId):checkAddressUser = "select distinct pincode from all_user_addresses where user_id= (%s)"result = fetchResult(checkAddressUser,userId)if len(result)==0:return Trueelse:myquery = "select count(*) from (select distinct pincode from all_user_addresses where user_id= (%s))a join pincodeavailability p on a.pincode=p.code"result = fetchResult(myquery,userId)if result[0][0]==0:return Falseelse:return Truedef getUserObject(user):obj = Mock()obj.user_id = user.user_idresult = fetchResult("select * from useractive where user_id=%d"%(user.user_id))if result == ():obj.last_active = Noneelse:obj.last_active =datetime.strftime(result[0][1], '%d/%m/%Y %H:%M:%S')obj.name = user.nameobj.contact = user.contact1obj.lastOrderTimestamp=Nonedetails = session.query(Orders).filter_by(user_id = user.user_id).filter(~Orders.status.in_(['ORDER_NOT_CREATED_KNOWN','ORDER_NOT_CREATED_UNKNOWN', 'ORDER_ALREADY_CREATED_IGNORED'])).order_by(desc(Orders.created)).first()if details is None:obj.lastOrderTimestamp =Noneelse:obj.lastOrderTimestamp =datetime.strftime(details.created, '%d/%m/%Y %H:%M:%S')obj.totalOrders = session.query(Orders).filter_by(user_id = user.user_id).filter(~Orders.status.in_(['ORDER_NOT_CREATED_KNOWN','ORDER_NOT_CREATED_UNKNOWN', 'ORDER_ALREADY_CREATED_IGNORED'])).count()cartResult = fetchResult("select account_key from user_accounts where account_type ='cartId' and user_id=%d"%(user.user_id))if cartResult == ():obj.lastActiveCartTime = Noneelse:conn = MySQLdb.connect("192.168.190.114", "root","shop2020", "user")cursor = conn.cursor()cursor.execute("select updated_on from cart where id=%s",(cartResult[0][0]))result = cursor.fetchall()if result ==():obj.lastActiveCartTime = Noneelse:print resultobj.lastActiveCartTime =datetime.strftime(result[0][0], '%d/%m/%Y %H:%M:%S')conn.close()return objclass CrmTicketDtr():def on_post(self,req,resp):customerFeedbackBackMap = {}customerFeedbackBackMap['user_id']=req.get_param("user_id")user = session.query(Users).filter_by(id=customerFeedbackBackMap.get('user_id')).first()if user is not None:customerFeedbackBackMap['email']=user.emailcustomerFeedbackBackMap['mobile_number']=user.mobile_numbercustomerFeedbackBackMap['customer_name']=user.first_name + ' ' + user.last_namecustomerFeedbackBackMap['subject'] = req.get_param("subject")customerFeedbackBackMap['message'] = req.get_param("message")customerFeedbackBackMap['created'] = datetime.now()if utils.generateCrmTicket(customerFeedbackBackMap):resp.body={"result":"success"}else:resp.body={"result":"failure"}else:resp.body={"result":"failure"}def addTicket(self,user_id,subject,message):customerFeedbackBackMap = {}customerFeedbackBackMap['user_id']=user_iduser = session.query(Users).filter_by(id=user_id).first()if user is not None:customerFeedbackBackMap['email']=user.emailcustomerFeedbackBackMap['mobile_number']=user.mobile_numbercustomerFeedbackBackMap['customer_name']=user.first_name + ' ' + user.last_namecustomerFeedbackBackMap['subject'] = subjectcustomerFeedbackBackMap['message'] = messagecustomerFeedbackBackMap['created'] = datetime.now()utils.generateCrmTicket(customerFeedbackBackMap)else:print 'User is not present'class UnitDeal():def on_get(self,req,resp, id):result = Mongo.getDealById(id)resp.body = dumps(result)def main():a = RetailerDetail()retailer = a.getNotActiveRetailer()otherContacts = [r for r, in session.query(RetailerContacts.mobile_number).filter_by(retailer_id=retailer.id).order_by(RetailerContacts.contact_type).all()]print json.dumps(todict(getRetailerObj(retailer, otherContacts, 'fresh')), encoding='utf-8')# a = GetUserCrmApplication()# a.getUser("accs_cart", 1)if __name__ == '__main__':main()