示例#1
0
	def from_web(source_key, input, ip, UserMessages):
		# Find the source matching the source key.
		source = SourcePointer.get_source(source_key)

		if not source:
			raise ExceptionUserMessage(source_key + ': No source matches this key')

		# If the source is server disabled, let the calling sysadmin know.
		if not source.enabled:
			raise ExceptionUserMessage(source_key + ': User has this source disabled on the server')

		# Create the message object.
		message_collection = UserMessages.get_user_message_collection(source.owner)
		message = UserMessage(parent=message_collection)
		message.owner = source.owner
		message.source = source
		if input.message:
			message.message = input.message
		else:
			message.message = ''
		message.title = input.title
		if input.url:
			message.url = input.url
		message.timestamp = datetime.datetime.now()
		message.deliveredToGoogle = False
		message.lastDeliveryAttempt = None
		message.sourceIp = ip

		if not message.checksize():
			# Too big! We've done our best, but...
			raise ExceptionUserMessage(source_key + ': Your message is too big. The title and URL were too long and the message could not be trimmed to fit. Maximum size is nearly 500 bytes')

		return message
示例#2
0
	def from_web(source_key, input, ip, UserMessages):
		# Find the source matching the source key.
		source = SourcePointer.get_source(source_key)

		if not source:
			raise ExceptionUserMessage(source_key + ': No source matches this key')

		# If the source is server disabled, let the calling sysadmin know.
		if not source.enabled:
			raise ExceptionUserMessage(source_key + ': User has this source disabled on the server')

		# Create the message object.
		message_collection = UserMessages.get_user_message_collection(source.owner)
		message = UserMessage(parent=message_collection)
		message.owner = source.owner
		message.source = source
		if input.message:
			message.message = input.message
		else:
			message.message = ""

		try:
			input.title.replace('\n', ' ')
			input.title.replace('\r', ' ')
			message.title = input.title
		except:
			logging.error('message title error : %r', input.title);
			message.title = "Bad title format"
			
		if input.url:
			message.url = input.url
		if input.image:
			message.image = input.image
		message.timestamp = datetime.datetime.now()
		message.deliveredToGoogle = False
		message.lastDeliveryAttempt = None
		if input.priority:
			try:
				message.priority = int(input.priority)
			except:
				message.priority = 0
			if message.priority > 3:
				message.priority = 3
			if message.priority < 0:
				message.priority = 0
		else:
			message.priority = 0
		message.sourceIp = ip

		if not message.checksize():
			# Too big! We've done our best, but...
			raise ExceptionUserMessage(source_key + ': Your message is too big. The title and URL were too long and the message could not be trimmed to fit. Maximum size is nearly 1024 bytes')

		return message
示例#3
0
    def from_web(source_key, input, ip, UserMessages):
        # Find the source matching the source key.
        source = SourcePointer.get_source(source_key)

        if not source:
            raise ExceptionUserMessage(source_key +
                                       ': No source matches this key')

        # If the source is server disabled, let the calling sysadmin know.
        if not source.enabled:
            raise ExceptionUserMessage(
                source_key + ': User has this source disabled on the server')

        # Create the message object.
        message_collection = UserMessages.get_user_message_collection(
            source.owner)
        message = UserMessage(parent=message_collection)
        message.owner = source.owner
        message.source = source
        if input.message:
            message.message = input.message
        else:
            message.message = ''
        message.title = input.title
        if input.url:
            message.url = input.url
        message.timestamp = datetime.datetime.now()
        message.deliveredToGoogle = False
        message.lastDeliveryAttempt = None
        message.sourceIp = ip

        if not message.checksize():
            # Too big! We've done our best, but...
            raise ExceptionUserMessage(
                source_key +
                ': Your message is too big. The title and URL were too long and the message could not be trimmed to fit. Maximum size is nearly 500 bytes'
            )

        return message
示例#4
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')
示例#5
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')
示例#6
0
文件: index.py 项目: firehot/notifry
	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')