'Provides error handling for DeviceAdmin send_message and send_bulk_message methods.'

def send_messages(self, request, queryset, bulk=False):

    ret = []
    errors = []
    r = ''
    for device in queryset:
        try:
            if bulk:
                r = queryset.send_message('Test    bulk    notification')
            else:
                r = device.send_message('Test    single    notification')
            if r:
                ret.append(r)
        except GCMError as e:
            errors.append(str(e))
        except APNSServerError as e:
            errors.append(e.status)
        if bulk:
            break
    if (not bulk):
        for r in ret:
            if ('error' in r['results'][0]):
                errors.append(r['results'][0]['error'])
    else:
        try:
            errors = [r['error'] for r in ret[0][0]['results'] if ('error' in r)]
        except TypeError:
            for entry in ret[0][0]:
                errors = (errors + [r['error'] for r in entry['results'] if ('error' in r)])
    if errors:
        self.message_user(request, _(('Some    messages    could    not    be    processed:    %r' % ',    '.join(errors))), level=messages.ERROR)
    if ret:
        if bulk:
            try:
                success = ret[0][0]['success']
            except TypeError:
                success = 0
                for entry in ret[0][0]:
                    success = (success + entry['success'])
            if (success == 0):
                return
        elif (len(errors) == len(ret)):
            return
        if errors:
            msg = _(('Some    messages    were    sent:    %s' % ret))
        else:
            msg = _(('All    messages    were    sent:    %s' % ret))
        self.message_user(request, msg)
