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_push_manager_init_with_all_services(self):
        pm = PushManager(
            [GCMPushService(GCM_OPS),
             BBPushService(BLACKBERRY_OPS)])

        self.assertTrue(pm.get_service(GCMPushService.type))
        self.assertTrue(pm.get_service(BBPushService.type))

        self.assertTrue(pm.get_service('Must NOT exist') == None)
    def test_push_manager_init_with_all_services(self):
        pm = PushManager([
            GCMPushService(GCM_OPS),
            BBPushService(BLACKBERRY_OPS)
        ])

        self.assertTrue(pm.get_service(GCMPushService.type))
        self.assertTrue(pm.get_service(BBPushService.type))

        self.assertTrue(pm.get_service('Must NOT exist') == None)
    def test_push_manager_add_all_push_services(self):
        pm = PushManager()

        pm.add_service(GCMPushService(GCM_OPS))
        pm.add_service(BBPushService(BLACKBERRY_OPS))

        self.assertTrue(pm.get_service(GCMPushService.type))
        self.assertTrue(pm.get_service(BBPushService.type))

        self.assertTrue(pm.get_service('Must NOT exist') == None)
    def test_push_manager_add_all_push_services(self):
        pm = PushManager()

        pm.add_service(GCMPushService(GCM_OPS))
        pm.add_service(BBPushService(BLACKBERRY_OPS))

        self.assertTrue(pm.get_service(GCMPushService.type))
        self.assertTrue(pm.get_service(BBPushService.type))

        self.assertTrue(pm.get_service('Must NOT exist') == None)
    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))
Exemple #7
0
def send(request):
    #Extracts the devices ids and converts it to integers:
    device_ids = map(lambda id: int(id), request.POST.getlist('device'))

    if len(device_ids) > 0:
        device_list = Device.objects.filter(id__in=device_ids)

        push_manager = PushManager([
            GCMPushService({'api_id': 'AIzaSyCwR74jMF8Ls0CXvJzKHMpHVQzwml9xmTI'}),
            BBPushService({'api_id': '2974-Mie72996c2B7m3t87M17o8172i80r505273', 'password': '******'})
        ])

        # if exists data to be sync we save it
        sync_content = request.POST['scontent']
        if sync_content != '':
            for dev in device_list:
                chunk = Chunck(device=dev, data=sync_content)
                chunk.save()

        status_dict = push_manager.send(Message(), device_list)

        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:
                if errors == '':
                    errors = 'Error sending request.<br>'
                errors += '&lt;Type: %s Code: %i&#92;\&gt;<br>Content:<br>%s<br><br>' % (type, status.code, status.raw)

        if errors == '':
            return redirect('index')
        else:
            return HttpResponse(errors)

    else:
        return HttpResponse('Select a device to send the push')