def POST_devices(request, format): """ Respond to the "POST devices" request. We create a new device in the system, overwriting the existing device if it exists. """ # Get the request parameters. params = apiHelper.get_params(request, resource_name="device") error = apiHelper.check_params(params, required_params=["token", "notification_type", "notification_token"]) if error != None: return error token = params['token'] notification_type = params['notification_type'] notification_token = params['notification_token'] if not session.validate(token): return HttpResponseBadRequest("Invalid token") user = session.get_user(token) # Delete any existing devices with the given notification type and token. Device.objects.filter(notification_type=notification_type, notification_token=notification_token).delete() # Create the new device. device = Device() device.user = user device.notification_type = notification_type device.notification_token = notification_token device.created_at = datetime.datetime.now() device.updated_at = datetime.datetime.now() device.save() # Finally, return the device back to our caller. return apiHelper.response({'device' : device.to_dict()}, format=format, status=HTTP_RESPONSE_POST_OK)