Subversion Repositories SmartDukaan

Rev

Rev 19117 | Rev 19123 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 19117 Rev 19122
Line 65... Line 65...
65
        handleCampaignRequest(self.name, self.recordsList)
65
        handleCampaignRequest(self.name, self.recordsList)
66
        logging.debug('Completed')
66
        logging.debug('Completed')
67
 
67
 
68
def handleCampaignRequest(threadName, recordsList ):
68
def handleCampaignRequest(threadName, recordsList ):
69
    for record in recordsList:
69
    for record in recordsList:
70
        userGcmRegIdDetails = userGcmRegIdMap.get(record.get('user_id'))
70
        userGcmRegIdDetails = userGcmRegIdMap.get(long(record.get('user_id')))
71
        campaign = notificationCampaignsMap.get(record.get('notification_campaign_id')) 
71
        campaign = notificationCampaignsMap.get(record.get('notification_campaign_id')) 
72
        for gcm_id, detailsMap in userGcmRegIdDetails.items():
72
        for gcm_id, detailsMap in userGcmRegIdDetails.items():
73
            result_url = ""
73
            result_url = ""
74
            if campaign.get('type')=='url':
74
            if campaign.get('type')=='url':
75
                parsed_uri = urlparse(campaign.get('url'))
75
                parsed_uri = urlparse(campaign.get('url'))
Line 116... Line 116...
116
            logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
116
            logging.debug('User Id:- '+str(record.get('user_id'))+' Post Data Json :- '+str(post_data_json))
117
            response = requests.post(GCM_URL, data=post_data_json, headers=headers)
117
            response = requests.post(GCM_URL, data=post_data_json, headers=headers)
118
            logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Response :-'+str(response.text))
118
            logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Response :-'+str(response.text))
119
            result = json.loads(response.text)
119
            result = json.loads(response.text)
120
            if result["success"]:
120
            if result["success"]:
121
                get_mongo_connection(host=mongoHost).User.pushnotificationsnew.update({'_id':record.get('_id')},{"$set":{'message':'success','type':'sent','sent_timestamp':to_java_date(datetime.now())}})
121
                get_mongo_connection(host=mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':'success','type':'sent','sent_timestamp':to_java_date(datetime.now())}})
122
                logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Success True')
122
                logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Success True')
123
            else:
123
            else:
124
                get_mongo_connection(host=mongoHost).User.pushnotificationsnew.update({'_id':record.get('_id')},{"$set":{'message':result["results"][0]["error"],'type':'failed','sent_timestamp':to_java_date(datetime.now())}})
124
                get_mongo_connection(host=mongoHost).User.pushnotifications.update({'_id':record.get('_id')},{"$set":{'message':result["results"][0]["error"],'type':'failed','sent_timestamp':to_java_date(datetime.now())}})
125
                logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Fail True')
125
                logging.debug('User Id:- '+str(record.get('user_id'))+' GCM_ID:- '+str(gcm_id)+' Update Response :- Notification Fail True')
126
                updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(detailsMap.get('gcm_regid'))
126
                updateGcmUserSql = "update gcm_users set failurecount=failurecount+1 where gcm_regid='%s'"%(detailsMap.get('gcm_regid'))
127
                logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
127
                logging.debug('Update GCM User Query :-'+str(updateGcmUserSql))
128
                try:
128
                try:
129
                    dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
129
                    dtrdb = MySQLdb.connect('localhost',"root","shop2020","dtr" )
Line 177... Line 177...
177
        notificationCampaignsMap[campaign['_id']] = campaign
177
        notificationCampaignsMap[campaign['_id']] = campaign
178
        
178
        
179
def insertPushNotificationEntriesToSent():
179
def insertPushNotificationEntriesToSent():
180
    global userGcmRegIdMap
180
    global userGcmRegIdMap
181
    for userList in campaignUsersMap.values():
181
    for userList in campaignUsersMap.values():
182
        print GCM_REG_ID_SQL1%(str(tuple(userList)))
182
        logging.debug("GCM_REG_SQL_1:- "+GCM_REG_ID_SQL1%(str(tuple(userList))))
183
        cursor.execute(GCM_REG_ID_SQL1%(str(tuple(userList))))
183
        cursor.execute(GCM_REG_ID_SQL1%(str(tuple(userList))))
184
        result_data = cursor.fetchall()
184
        result_data = cursor.fetchall()
185
        
185
        
186
        if result_data and len(result_data)>0:
186
        if result_data and len(result_data)>0:
-
 
187
            user_list = []
-
 
188
            for userId in userList:
-
 
189
                user_list.append(userId)
-
 
190
                
187
            for dataRec in result_data:
191
            for dataRec in result_data:
188
                if dataRec[0] in userList:
192
                if dataRec[0] in user_list:
189
                    userList.remove(str(dataRec[0]))
193
                    user_list.remove(str(dataRec[0]))
190
                if userGcmRegIdMap.has_key(dataRec[0]):
194
                if userGcmRegIdMap.has_key(dataRec[0]):
191
                    detailMap = {}
195
                    detailMap = {}
192
                    gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
196
                    gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
193
                    detailMap['gcm_regid'] = dataRec[1]
197
                    detailMap['gcm_regid'] = dataRec[1]
194
                    if dataRec[3] is not None:
198
                    if dataRec[3] is not None:
Line 201... Line 205...
201
                    detailMap['gcm_regid'] = dataRec[1]
205
                    detailMap['gcm_regid'] = dataRec[1]
202
                    if dataRec[3] is not None:
206
                    if dataRec[3] is not None:
203
                        detailMap['android_id'] = dataRec[3]
207
                        detailMap['android_id'] = dataRec[3]
204
                    gcmRegIdMap[dataRec[2]]= detailMap
208
                    gcmRegIdMap[dataRec[2]]= detailMap
205
                    userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
209
                    userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
206
                    
210
            
-
 
211
            logging.debug("Old Users.."+user_list) 
-
 
212
            logging.debug("GCM_REG_SQL_2:- "+GCM_REG_ID_SQL2%(str(tuple(user_list))))       
207
            cursor.execute(GCM_REG_ID_SQL2%(str(tuple(userList))))
213
            cursor.execute(GCM_REG_ID_SQL2%(str(tuple(user_list))))
208
            result_data = cursor.fetchall()
214
            result_data = cursor.fetchall()
209
            for dataRec in result_data:
215
            for dataRec in result_data:
210
                if userGcmRegIdMap.has_key(dataRec[0]):
216
                if userGcmRegIdMap.has_key(dataRec[0]):
211
                    detailMap = {}
217
                    detailMap = {}
212
                    gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
218
                    gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
Line 222... Line 228...
222
                    if dataRec[3] is not None:
228
                    if dataRec[3] is not None:
223
                        detailMap['android_id'] = dataRec[3]
229
                        detailMap['android_id'] = dataRec[3]
224
                    gcmRegIdMap[dataRec[2]]= detailMap
230
                    gcmRegIdMap[dataRec[2]]= detailMap
225
                    userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
231
                    userGcmRegIdMap[dataRec[0]] = gcmRegIdMap
226
        else:
232
        else:
-
 
233
            logging.debug("GCM_REG_SQL_2:- "+GCM_REG_ID_SQL2%(str(tuple(userList))))
227
            cursor.execute(GCM_REG_ID_SQL2%(str(tuple(userList))))
234
            cursor.execute(GCM_REG_ID_SQL2%(str(tuple(userList))))   
228
            result_data = cursor.fetchall()
235
            result_data = cursor.fetchall()
229
            for dataRec in result_data:
236
            for dataRec in result_data:
230
                if userGcmRegIdMap.has_key(dataRec[0]):
237
                if userGcmRegIdMap.has_key(dataRec[0]):
231
                    detailMap = {}
238
                    detailMap = {}
232
                    gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
239
                    gcmRegIdMap = userGcmRegIdMap.get(dataRec[0])
Line 246... Line 253...
246
                    
253
                    
247
    logging.debug("CampaignUsersMap"+str(campaignUsersMap)) 
254
    logging.debug("CampaignUsersMap"+str(campaignUsersMap)) 
248
        
255
        
249
    for campaignId, userList in campaignUsersMap.items():
256
    for campaignId, userList in campaignUsersMap.items():
250
        for userId in userList:
257
        for userId in userList:
251
            gcmRegIdMap = userGcmRegIdMap.get(userId)
258
            gcmRegIdMap = userGcmRegIdMap.get(long(userId))
252
            if gcmRegIdMap is None:
259
            if gcmRegIdMap is None:
253
                gcmRegIdMap = {}
260
                gcmRegIdMap = {}
254
            for gcm_id, details in gcmRegIdMap.items():
261
            for gcm_id, details in gcmRegIdMap.items():
255
                android_id = None
262
                android_id = None
-
 
263
                logging.debug("User Id:- "+str(userId)+" ..User Details:- "+str(details))
256
                if details.has_key('android_id'):
264
                if details.has_key('android_id'):
257
                    android_id = details['android_id']
265
                    android_id = details['android_id']
258
                pushNotificationObj = __PushNotification(campaignId, userId, None, 'pending', \
266
                pushNotificationObj = __PushNotification(campaignId, userId, None, 'pending', \
259
                                    None, None, android_id, "php", None, None, None, None, gcm_id, to_java_date(datetime.now()),0)
267
                                    None, None, android_id, "php", None, None, None, None, gcm_id, to_java_date(datetime.now()),0)
260
                get_mongo_connection(host=mongoHost).User.pushnotificationsnew.insert(pushNotificationObj.__dict__)
268
                get_mongo_connection(host=mongoHost).User.pushnotifications.insert(pushNotificationObj.__dict__)
261
        
269
        
262
def populatePendingNotificationEntriesToBeSent():
270
def populatePendingNotificationEntriesToBeSent():
263
    global pendingNotificationEntryMap
271
    global pendingNotificationEntryMap
264
    pendingNotificationEntries = list(get_mongo_connection(host=mongoHost).User.pushnotificationsnew.find({'type':'pending'}))
272
    pendingNotificationEntries = list(get_mongo_connection(host=mongoHost).User.pushnotifications.find({'type':'pending'}))
265
    for entry in pendingNotificationEntries:
273
    for entry in pendingNotificationEntries:
266
        if pendingNotificationEntryMap.has_key(entry['notification_campaign_id']):
274
        if pendingNotificationEntryMap.has_key(entry['notification_campaign_id']):
267
            entries = pendingNotificationEntryMap.get(entry['notification_campaign_id'])
275
            entries = pendingNotificationEntryMap.get(entry['notification_campaign_id'])
268
            entries.append(entry)
276
            entries.append(entry)
269
            pendingNotificationEntryMap[entry['notification_campaign_id']] = entries
277
            pendingNotificationEntryMap[entry['notification_campaign_id']] = entries