def __init__(self): self.devices = [] self.sync_data = [] self.message = Message() self.message.payload = { 'type': 'osync' }
def test_send_collapsible_message(self): msg = Message() msg.set_option('collapse_key', 'test') gcm_srv = GCMPushService({'api_id': API_ID}) device_list = [Device(GCMPushService.type, TOKEN)] push_response = gcm_srv.send(msg, device_list) self.assertIsInstance(push_response, PushResponse) self.assertTrue(push_response.code in (200, 503)) if(push_response.code == 200): self.assertTrue( push_response.success == len(device_list) - push_response.failure )
def test_send_message_no_expiration_date(self): msg = Message() gcm_srv = BBPushService({'api_id': API_ID, 'password': PASSWORD}) device_list = [Device(BBPushService.type, TOKEN)] push_response = gcm_srv.send(msg, device_list) self.assertIsInstance(push_response, PushResponse) self.assertTrue( push_response.code in (1000, 1001, 2001, 2002, 2004, 4001, 21000), '\nStatus Code %i:\n%s\n' % (push_response.code, push_response.description))
def test_push_manager_send(self): pm = PushManager( [GCMPushService(GCM_OPS), BBPushService(BLACKBERRY_OPS)]) msg = Message() device_list = [ Device(BBPushService.type, TOKEN['Blackberry']), Device(GCMPushService.type, TOKEN['GCM']) ] status_dict = pm.send(msg, device_list) self.assertTrue( status_dict[BBPushService.type].code in (1000, 1001, 2001, 2002, 2004, 4001, 21000), '\nBlackberry Status Code %i:\n%s\n' % (status_dict[BBPushService.type].code, status_dict[BBPushService.type].description)) self.assertTrue( status_dict[GCMPushService.type].code in (200, 503), '\nGCM Status Code %i:\n' % (status_dict[GCMPushService.type].code))
def test_message_set_option(self): msg = Message() msg.set_option('key', 'value') self.assertTrue('key' in msg.options) self.assertTrue(msg.options['key'] == 'value')
class BarachielPushManager: push_manager = PushManager([ #TODO: Pasar estos datos a settings.py GCMPushService({'api_id': 'AIzaSyB5NvAZuGrhbpCVxszC6IESbHcYLBDtxz0'}), ParseAIService({'app_id': 'GwiELlzA5upc3gGjpzvbx4AW2CmMLNJd40vx14sz', 'rest_key': 'oSJtjfFZcqjNOZ5xWW2RjVUViIDaZ8B9J9LU7l9i'}), BBPushService({'api_id': '2974-Mie72996c2B7m3t87M17o8172i80r505273', 'password': '******'}), ]) message = None devices = None sync_data = None def __init__(self): self.devices = [] self.sync_data = [] self.message = Message() self.message.payload = { 'type': 'osync' } def add_user(self, user): #Se puede optimizar para agregar listas de usuarios en lote desde la BD: new_devices = Device.objects.filter(user=user) self.devices += list(new_devices) def add_sync_data(self, data): chunk = Chunk(data=data, expiration=datetime.now() + timedelta(days=30)) self.sync_data.append(chunk) def set_like(self, like): self.message.payload['type'] = 'like' self.message.payload['like_id'] = like.id self.add_user(like.liked) self.add_sync_data(json.dumps({ # Wrapped Model: 'model': like.serialize(like.liked), 'class': 'Like' })) if(not like.anonymous): self.message.payload['user_name'] = like.liker.name[:16] self.message.set_option('alert', { 'loc-key': '%@ has Waved at you', 'loc-args': [self.message.payload['user_name']] }) else: self.message.set_option('alert', { 'loc-key': 'Someone has sent you an anonymous wave.' }) def send(self): import socket if len(self.devices) > 0: for chunk in self.sync_data: chunk.save() chunk.add_devices(self.devices) try: status_dict = self.push_manager.send(self.message, self.devices) print 'Sending push:\n' errors = [] for type, status in status_dict.iteritems(): print ' Service %s: Status %i, Content="%s"\n' % (type, status.code, status.raw) if not status.is_ok: errors += (type, status.code, status.raw) if len(errors) > 0: return errors except socket.error, e: print 'Push Service not found %s' % (e.strerror) else: