Exemplo n.º 1
0
class notifry:
    def GET(self):
        # GET does the same thing as POST. Allows for easy testing.
        return self.POST()

    def POST(self):
        # And we need the following variables.
        # The defaults are provided below.
        input = web.input(source=None, message=None, title=None, url=None)

        renderer.addData('messages', 0)

        # We must have the following keys passed,
        # otherwise this is an invalid request.
        if not input.source or not input.title:
            # Fail with an error.
            renderer.addData(
                'error',
                'Missing required parameters - need at least source and title.'
            )
            return renderer.render('messages/send.html')

        source_keys = input.source.split(",")
        messages = []
        errors = []

        if len(source_keys) > 10:
            renderer.addData(
                'error', 'You can not send to more than 10 sources at a time.')

        # Try to make a message for each.
        for key in source_keys:
            try:
                messages.append(
                    UserMessage.from_web(key.strip(), input, web.ctx.ip,
                                         UserMessages))
            except ExceptionUserMessage, ex:
                errors.append(ex.args[0])

        if len(errors) > 0:
            renderer.addData('error', ", ".join(errors))

        if len(messages) > 0:
            # Get the AC2DM sender.
            sender = AC2DM.factory()

            # At least something went through.
            for message in messages:

                def transaction(message):
                    message_collection = UserMessages.get_user_message_collection_static(
                        message.source.owner)
                    message.put()
                    message_collection.add_message(message)
                    message_collection.put()

                db.run_in_transaction(transaction, message)

                sender.send_to_all(message)

                renderer.addData('size', message.getsize())
                renderer.addData('truncated', message.wasTruncated)

        renderer.addData('messages', len(messages))
        return renderer.render('messages/send.html')
Exemplo n.º 2
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')
Exemplo n.º 3
0
    def POST(self, action):
        if action == 'list':
            sources = UserSources.get_user_sources(users.get_current_user())
            renderer.addDataList('sources', sources)
            return renderer.render('sources/list.html')
        elif action == 'get':
            source = self.get_source()
            renderer.addData('source', source)
            return renderer.render('sources/detail.html')
        elif action == 'test':
            # Send a test message to the source.
            source = self.get_source()

            # Fix up the source pointer. Useful if it's broken somehow.
            SourcePointer.persist(source)

            # Now create the test message.
            message_collection = UserMessages.get_user_message_collection(
                users.get_current_user())
            message = UserMessage.create_test(source, web.ctx.ip,
                                              message_collection)

            def transaction(message):
                message.put()
                message_collection = UserMessages.get_user_message_collection_static(
                    users.get_current_user())
                message_collection.add_message(message)
                message_collection.put()

            db.run_in_transaction(transaction, message)

            sender = AC2DM.factory()
            sender.send_to_all(message)

            # And we're done.
            return renderer.render('sources/test.html')
        elif action == 'delete':
            source = self.get_source()
            message_collection = UserMessages.get_user_message_collection(
                source.owner)
            message_collection.delete_for_source(source)
            source_collection = UserSources.get_user_source_collection(
                users.get_current_user())

            # Notify devices that something changed.
            # Also, if given a device, exclude that device from
            # the notification.
            input = web.input(device=None)
            source.notify_delete(input.device)

            def transaction(source):
                source_collection = UserSources.get_user_source_collection_static(
                    users.get_current_user())
                source_collection.remove_source(source)
                source.delete()
                source_collection.put()

            db.run_in_transaction(transaction, source)

            # Remove the pointer. If this fails, it's not a big issue, as it's
            # checked anyway before use.
            SourcePointer.remove(source)

            renderer.addData('success', True)
            return renderer.render('sources/deletecomplete.html')
        else:
            source = self.get_source()

            # Get the form and the form data.
            form = self.get_form()
            form.fill(source.dict())

            if not form.validates():
                # Failed to validate. Display the form again.
                renderer.addTemplate('action', self.get_pretty_action(action))
                renderer.addTemplate('form', form)
                errors = form.getnotes()
                renderer.addDataList('errors', errors)
                return renderer.render('sources/edit.html')
            else:
                # Validated - proceed.
                source.updated = datetime.datetime.now()
                source.title = form.title.get_value()
                #source.description = form.description.get_value()
                source.enabled = False
                source.owner = users.get_current_user()
                if form.enabled.get_value():
                    source.enabled = True

                # Make sure the source collection exists.
                UserSources.get_user_source_collection(
                    users.get_current_user())

                # Place into source collection.
                def transaction(source):
                    source_collection = UserSources.get_user_source_collection_static(
                        users.get_current_user())
                    source.put()
                    source_collection.add_source(source)
                    source_collection.put()

                db.run_in_transaction(transaction, source)

                # Set up the source pointer.
                SourcePointer.persist(source)

                # Notify devices that something changed.
                # Also, if given a device, exclude that device from
                # the notification.
                input = web.input(device=None)
                source.notify(input.device)

                if renderer.get_mode() == 'html':
                    # Redirect to the source list.
                    raise web.found('/profile')
                else:
                    # Send back the source data.
                    renderer.addData('source', source)
                    return renderer.render('apionly.html')
Exemplo n.º 4
0
	def notify(self, originating_device_id):
		# Let the user's devices know that the source has been added or changed.
		ac2dm = AC2DM.factory()
		ac2dm.notify_all_source_change(self, originating_device_id)
Exemplo n.º 5
0
	def notify_delete(self, originating_device_id):
		# Let the user's devices know that the source has been deleted. It can
		# resync their list with the server.
		ac2dm = AC2DM.factory()
		ac2dm.notify_all_source_delete(self, originating_device_id)
Exemplo n.º 6
0
	def POST(self, action):
		if action == 'list':
			sources = UserSources.get_user_sources(users.get_current_user())
			renderer.addDataList('sources', sources)
			return renderer.render('sources/list.html')
		elif action == 'get':
			source = self.get_source()
			renderer.addData('source', source)
			return renderer.render('sources/detail.html')
		elif action == 'test':
			# Send a test message to the source and the caller device id.
			source = self.get_source()
			input = web.input(deviceid=None, language=None, src=None)
			logging.debug("Test DDB language : " + get_user_language(users.get_current_user()))
			if input.src:
				logging.debug("Test src : " + input.src)
			if input.language:
				logging.debug("Test input language : " + input.language)

			if input.src and input.src == "web":
				#get language from User DDB
				input.language = get_user_language(users.get_current_user());
			else:
				if get_user_language(users.get_current_user()) == "English":
					logging.debug("DDB is English")
				if get_user_language(users.get_current_user()) == "English" and input.language and input.language != "English":
					logging.debug("Store new language in DDB")
					set_user_language(users.get_current_user(), input.language)
			
			# Fix up the source pointer. Useful if it's broken somehow.
			SourcePointer.persist(source)

			# Now create the test message.
			message_collection = UserMessages.get_user_message_collection(users.get_current_user())
			message = UserMessage.create_test(source, web.ctx.ip, message_collection, input.language)
			
#			def transaction(message):
#				message.put()
#				message_collection = UserMessages.get_user_message_collection_static(users.get_current_user())
#				message_collection.add_message(message)
#				message_collection.put()
#			db.run_in_transaction(transaction, message)

			sender = AC2DM.factory()
			result = sender.send_to_all(message, input.deviceid)
			renderer.addData('messagescount', get_message_count(message.source.owner));
			renderer.addData('result', result);


			# And we're done.
			return renderer.render('sources/test.html')
		elif action == 'delete':
			source = self.get_source()
			message_collection = UserMessages.get_user_message_collection(source.owner)
			message_collection.delete_for_source(source)
			source_collection = UserSources.get_user_source_collection(users.get_current_user())

			# Notify devices that something changed.
			# Also, if given a device, exclude that device from
			# the notification.
			input = web.input(device = None)
			source.notify_delete(input.device)
			def transaction(source):
				source_collection = UserSources.get_user_source_collection_static(users.get_current_user())
				source_collection.remove_source(source)
				source.delete()
				source_collection.put()
			db.run_in_transaction(transaction, source)

			# Remove the pointer. If this fails, it's not a big issue, as it's
			# checked anyway before use.
			SourcePointer.remove(source)

			renderer.addData('success', True)
			return renderer.render('sources/deletecomplete.html')
		else:
			source = self.get_source()

			# Get the form and the form data.
			form = self.get_form()
			form.fill(source.dict())

			if not form.validates():
				# Failed to validate. Display the form again.
				renderer.addTemplate('action', self.get_pretty_action(action))
				renderer.addTemplate('form', form)
				errors = form.getnotes()
				renderer.addDataList('errors', errors)
				return renderer.render('sources/edit.html')
			else:
				# Validated - proceed.
				source.updated = datetime.datetime.now()
				try:
					title = form.title.get_value()
					title.replace('\n', ' ')
					title.replace('\r', ' ')
					source.title = title
				except:
					logging.error('source name error' + form.title.get_value());
					source.title = "Bad source name format"
					
				#source.description = form.description.get_value()
				#source.enabled = False
				source.owner = users.get_current_user()
				#if form.enabled.get_value():
				#  source.enabled = True
				source.enabled = True
				# Make sure the source collection exists.
				UserSources.get_user_source_collection(users.get_current_user())
				
				# Place into source collection.
				def transaction(source):
					source_collection = UserSources.get_user_source_collection_static(users.get_current_user())
					source.put()
					source_collection.add_source(source)
					source_collection.put()
				db.run_in_transaction(transaction, source)

				# Set up the source pointer.
				SourcePointer.persist(source)

				# Notify devices that something changed.
				# Also, if given a device, exclude that device from
				# the notification.
				input = web.input(device = None)
				source.notify(input.device)

				if renderer.get_mode() == 'html':
					# Redirect to the source list.
					raise web.found('/profile')
				else:
					# Send back the source data.
					renderer.addData('source', source)
					return renderer.render('apionly.html')
Exemplo n.º 7
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')
Exemplo n.º 8
0
 def notify_delete(self, originating_device_id):
     # Let the user's devices know that the source has been deleted. It can
     # resync their list with the server.
     ac2dm = AC2DM.factory()
     ac2dm.notify_all_source_delete(self, originating_device_id)
Exemplo n.º 9
0
 def notify(self, originating_device_id):
     # Let the user's devices know that the source has been added or changed.
     ac2dm = AC2DM.factory()
     ac2dm.notify_all_source_change(self, originating_device_id)
Exemplo n.º 10
0
	def POST(self, action):
		if action == 'list':
			sources = UserSources.get_user_sources(users.get_current_user())
			renderer.addDataList('sources', sources)
			return renderer.render('sources/list.html')
		elif action == 'get':
			source = self.get_source()
			renderer.addData('source', source)
			return renderer.render('sources/detail.html')
		elif action == 'test':
			# Send a test message to the source.
			source = self.get_source()

			# Fix up the source pointer. Useful if it's broken somehow.
			SourcePointer.persist(source)

			# Now create the test message.
			message_collection = UserMessages.get_user_message_collection(users.get_current_user())
			message = UserMessage.create_test(source, web.ctx.ip, message_collection)
			def transaction(message):
				message.put()
				message_collection = UserMessages.get_user_message_collection_static(users.get_current_user())
				message_collection.add_message(message)
				message_collection.put()
			db.run_in_transaction(transaction, message)

			sender = AC2DM.factory()
			sender.send_to_all(message)

			# And we're done.
			return renderer.render('sources/test.html')
		elif action == 'delete':
			source = self.get_source()
			message_collection = UserMessages.get_user_message_collection(source.owner)
			message_collection.delete_for_source(source)
			source_collection = UserSources.get_user_source_collection(users.get_current_user())

			# Notify devices that something changed.
			# Also, if given a device, exclude that device from
			# the notification.
			input = web.input(device = None)
			source.notify_delete(input.device)
			def transaction(source):
				source_collection = UserSources.get_user_source_collection_static(users.get_current_user())
				source_collection.remove_source(source)
				source.delete()
				source_collection.put()
			db.run_in_transaction(transaction, source)

			# Remove the pointer. If this fails, it's not a big issue, as it's
			# checked anyway before use.
			SourcePointer.remove(source)

			renderer.addData('success', True)
			return renderer.render('sources/deletecomplete.html')
		else:
			source = self.get_source()

			# Get the form and the form data.
			form = self.get_form()
			form.fill(source.dict())

			if not form.validates():
				# Failed to validate. Display the form again.
				renderer.addTemplate('action', self.get_pretty_action(action))
				renderer.addTemplate('form', form)
				errors = form.getnotes()
				renderer.addDataList('errors', errors)
				return renderer.render('sources/edit.html')
			else:
				# Validated - proceed.
				source.updated = datetime.datetime.now()
				source.title = form.title.get_value()
				#source.description = form.description.get_value()
				source.enabled = False
				source.owner = users.get_current_user()
				if form.enabled.get_value():
					source.enabled = True

				# Make sure the source collection exists.
				UserSources.get_user_source_collection(users.get_current_user())
				
				# Place into source collection.
				def transaction(source):
					source_collection = UserSources.get_user_source_collection_static(users.get_current_user())
					source.put()
					source_collection.add_source(source)
					source_collection.put()
				db.run_in_transaction(transaction, source)

				# Set up the source pointer.
				SourcePointer.persist(source)

				# Notify devices that something changed.
				# Also, if given a device, exclude that device from
				# the notification.
				input = web.input(device = None)
				source.notify(input.device)

				if renderer.get_mode() == 'html':
					# Redirect to the source list.
					raise web.found('/profile')
				else:
					# Send back the source data.
					renderer.addData('source', source)
					return renderer.render('apionly.html')