Example #1
0
	def Register(regid, uuid, name, platform):
		existingDevice = None
		if(platform == 'android'):
			existingDevice = GCMDevice.objects.filter(registration_id=regid)
		else:
			existingDevice = APNSDevice.objects.filter(registration_id=regid)

		if len(existingDevice) > 0:
			return False

		newDevice = None
		if(platform == 'android'):
			newDevice = GCMDevice()
		else:
			newDevice = APNSDevice()

		try:
			newDevice.registration_id = regid
			newDevice.device_id = uuid
			newDevice.name = name
			newDevice.save()
			return True
		except Exception, e:
			print e
			return False
Example #2
0
def register_device(request, device_type):
    if request.method == 'POST':
        json_data = simplejson.loads(request.body)
        if device_type == 'gcm':
            existingDevices = GCMDevice.objects.filter(registration_id=json_data['registration_id'])
            if existingDevices:
                device = existingDevices[0]
            else:
                device = GCMDevice()
        elif device_type == 'apns':
            existingDevices = APNSDevice.objects.filter(registration_id=json_data['registration_id'])
            if existingDevices:
                device = existingDevices[0]
            else:
                device = APNSDevice()
        else:
            return HttpResponseBadRequest('Invalid device type')
        device.name = request.user.username
        device.user = request.user
        device.registration_id = json_data['registration_id']
        device.save()

    return HttpResponse("OK")
Example #3
0
    def Register(regid, uuid, name, platform):
        existingDevice = None
        if platform == 'android':
            existingDevice = GCMDevice.objects.filter(registration_id=regid)
        else:
            existingDevice = APNSDevice.objects.filter(registration_id=regid)
        if len(existingDevice) > 0:
            return False

        newDevice = None
        if platform == 'android':
            newDevice = GCMDevice()
        else:
            newDevice = APNSDevice()

        try:
            newDevice.registration_id = regid
            newDevice.device_id = uuid
            newDevice.name = name
            newDevice.save()
            return True
        except Exception, e:
            print e
            return False
def register(request):
    logging.warn('register')
    os_type = get_request_var(request, 'os')
    username = get_request_var(request, 'username')
    registration_id = get_request_var(request, 'registration_id')

    if not username:
        response_dict = error_dict()
        response_dict['error_code'] = 100
        response_dict['error_msg'] = 'Missing username'
        return render_json(request, response_dict)

    if not registration_id:
        response_dict = error_dict()
        response_dict['error_code'] = 200
        response_dict['error_msg'] = 'Missing registration_id'
        return render_json(request, response_dict)

    if not os_type:
        response_dict = error_dict()
        response_dict['error_code'] = 300
        response_dict['error_msg'] = 'Missing os_type'
        return render_json(request, response_dict)

    logging.warn('fetching user')
    user = User.objects.get(username=username)
    if not user:
        response_dict = error_dict()
        response_dict['error_code'] = 400
        response_dict['error_msg'] = 'No user'
        return render_json(request, response_dict)

    if os_type == 'a':
        apns_device = None
        try:
            apns_device = APNSDevice.objects.get(user=user)
            logging.warn('apns device already registered')
        except:
            logging.exception('error getting apns device')
            logging.warn('no existing apns device')

        if apns_device:
            if registration_id != apns_device.registration_id:
                logging.warn('updating apns device with new registration_id')
                apns_device.registration_id = registration_id
                apns_device.save()
        else:
            logging.warn('creating apns device')
            try:
                apns_device = APNSDevice()
                apns_device.user = user
                apns_device.name = user.first_name
                apns_device.registration_id = registration_id
                apns_device.save()
            except:
                logging.exception('error creating apns device')
                response_dict = error_dict()
                response_dict['error_code'] = 500
                response_dict['error_msg'] = 'Error creating apns device'
                return render_json(request, response_dict)
    elif os_type == 'g':
        gcm_device = None
        try:
            gcm_device = GCMDevice.objects.get(user=user)
            logging.warn('gcm device already registered')
        except:
            logging.exception('error getting gcm device')
            logging.warn('no existing gcm device')

        if gcm_device:
            if registration_id != gcm_device.registration_id:
                logging.warn('updating gcm device with new registration_id')
                gcm_device.registration_id = registration_id
                gcm_device.save()
        else:
            logging.warn('creating gcm device')
            try:
                gcm_device = GCMDevice()
                gcm_device.user = user
                gcm_device.name = user.first_name
                gcm_device.registration_id = registration_id
                gcm_device.save()
            except:
                logging.exception('error creating gcm device')
                response_dict = error_dict()
                response_dict['error_code'] = 500
                response_dict['error_msg'] = 'Error creating gcm device'
                return render_json(request, response_dict)
    else:
        response_dict = error_dict()
        response_dict['error_code'] = 600
        response_dict['error_msg'] = 'Unrecognized os_type'
        return render_json(request, response_dict)

    logging.warn('registration done')
    response_dict = success_dict()
    return render_json(request, response_dict)