Ejemplo n.º 1
0
	def get_device(self):
		# If ID supplied, find and update that ID.
		input = web.input(id = None)

		devices = UserDevices.get_user_device_collection(users.get_current_user())
		device = UserDevice(parent=devices)
		if input.id and long(input.id) > 0:
			# Load device from ID.
			device = UserDevice.get_by_id(long(input.id), devices)

			if not device:
				# Invalid ID. 404.
				raise web.notfound()

			# Check that the device belongs to the logged in user.
			if device.owner.user_id() != users.get_current_user().user_id():
				# It's not theirs. 404.
				# TODO: Test this more and better.
				raise web.notfound()

		return device
Ejemplo n.º 2
0
    def get_device(self):
        # If ID supplied, find and update that ID.
        input = web.input(id=None)

        devices = UserDevices.get_user_device_collection(
            users.get_current_user())
        device = UserDevice(parent=devices)
        if input.id and long(input.id) > 0:
            # Load device from ID.
            device = UserDevice.get_by_id(long(input.id), devices)

            if not device:
                # Invalid ID. 404.
                raise web.notfound()

            # Check that the device belongs to the logged in user.
            if device.owner.user_id() != users.get_current_user().user_id():
                # It's not theirs. 404.
                # TODO: Test this more and better.
                raise web.notfound()

        return device
Ejemplo n.º 3
0
    def POST(self, action):
        # You must be logged in.
        login_required()

        if action == 'delete':
            device = self.get_device()

            # Let the device know it's been deleted.
            ac2dm = AC2DM.factory()
            ac2dm.notify_device_delete(device)

            devices = UserDevices.get_user_device_collection(
                users.get_current_user())

            # Now delete it.
            def transaction(device):
                devices = UserDevices.get_user_device_collection_static(
                    users.get_current_user())
                devices.remove_device(device)
                device.delete()
                devices.put()

            db.run_in_transaction(transaction, device)

            renderer.addData('success', True)
            return renderer.render('device/deletecomplete.html')
        elif action == 'deregister':
            device = self.get_device()
            devices = UserDevices.get_user_device_collection(
                users.get_current_user())

            def transaction(device):
                devices = UserDevices.get_user_device_collection_static(
                    users.get_current_user())
                devices.remove_device(device)
                device.delete()
                devices.put()

            db.run_in_transaction(transaction, device)
            renderer.addData('success', True)
            return renderer.render('apionly.html')
        elif action == 'register':
            # And we need the following variables.
            # The defaults are provided below.
            input = web.input(devicekey=None,
                              devicetype=None,
                              deviceversion=None,
                              nickname=None)

            # We must have the following keys passed,
            # otherwise this is an invalid request.
            if not input.devicekey and not input.devicetype:
                # Fail with an error.
                renderer.addData(
                    'error',
                    'Missing required parameters "devicekey" and "devicetype".'
                )
                return renderer.render('apionly.html')

            # Check 'devicetype' is 'android' - nothing else is supported right now.
            if input.devicetype != 'android':
                renderer.addData(
                    'error',
                    'Only Android devices are supported at the moment, sorry.')
                return renderer.render('apionly.html')

            # Get the users's device collection.
            devices = UserDevices.get_user_device_collection(
                users.get_current_user())

            # If ID supplied, find and update that ID.
            device = self.get_device()

            # Is it a new device? Double check that the key isn't already
            # attached to another record (with the same owner!)
            if not device.dict().has_key('id'):
                # Attempt to find another device with the same key.
                for existingDevice in devices.get_devices():
                    if existingDevice.deviceKey == input.devicekey:
                        # Found one - update that object instead.
                        device = existingDevice

            device.updated = datetime.datetime.now()
            device.owner = users.get_current_user()
            device.deviceKey = input.devicekey
            device.deviceType = input.devicetype
            device.deviceVersion = input.deviceversion
            device.deviceNickname = input.nickname

            def transaction(device):
                devices = UserDevices.get_user_device_collection_static(
                    users.get_current_user())
                device.put()
                devices.add_device(device)
                devices.put()

            db.run_in_transaction(transaction, device)

            renderer.addData('device', device)
            return renderer.render('apionly.html')
Ejemplo n.º 4
0
	def POST(self, action):
		# You must be logged in.
		login_required()

		if action == 'delete':
			device = self.get_device()

			# Let the device know it's been deleted.
			ac2dm = AC2DM.factory()
			ac2dm.notify_device_delete(device)

			devices = UserDevices.get_user_device_collection(users.get_current_user())

			# Now delete it.
			def transaction(device):
				devices = UserDevices.get_user_device_collection_static(users.get_current_user())
				devices.remove_device(device)
				device.delete()
				devices.put()
			db.run_in_transaction(transaction, device)
	
			renderer.addData('success', True)
			return renderer.render('device/deletecomplete.html')
		elif action == 'deregister':
			device = self.get_device()
			devices = UserDevices.get_user_device_collection(users.get_current_user())

			def transaction(device):
				devices = UserDevices.get_user_device_collection_static(users.get_current_user())
				devices.remove_device(device)
				device.delete()
				devices.put()
			db.run_in_transaction(transaction, device)
			renderer.addData('success', True)
			return renderer.render('apionly.html')
		elif action == 'register':
			# And we need the following variables.
			# The defaults are provided below.
			input = web.input(devicekey = None, devicetype = None, deviceversion = None, nickname = None)

			# We must have the following keys passed,
			# otherwise this is an invalid request.
			if not input.devicekey and not input.devicetype:
				# Fail with an error.
				renderer.addData('error', 'Missing required parameters "devicekey" and "devicetype".')
				return renderer.render('apionly.html')

			# Check 'devicetype' is 'android' - nothing else is supported right now.
			if input.devicetype != 'android':
				renderer.addData('error', 'Only Android devices are supported at the moment, sorry.')
				return renderer.render('apionly.html')

			# Get the users's device collection.
			devices = UserDevices.get_user_device_collection(users.get_current_user())

			# If ID supplied, find and update that ID.
			device = self.get_device()

			# Is it a new device? Double check that the key isn't already
			# attached to another record (with the same owner!)
			if not device.dict().has_key('id'):
				# Attempt to find another device with the same key.
				for existingDevice in devices.get_devices():
					if existingDevice.deviceKey == input.devicekey:
						# Found one - update that object instead.
						device = existingDevice

			device.updated = datetime.datetime.now()
			device.owner = users.get_current_user()
			device.deviceKey = input.devicekey
			device.deviceType = input.devicetype
			device.deviceVersion = input.deviceversion
			device.deviceNickname = input.nickname

			def transaction(device):
				devices = UserDevices.get_user_device_collection_static(users.get_current_user())
				device.put()
				devices.add_device(device)
				devices.put()
			db.run_in_transaction(transaction, device)
			
			renderer.addData('device', device)
			return renderer.render('apionly.html')