Ejemplo n.º 1
0
    def GET(self):
        # Older than 1 day.
        older_than = datetime.datetime.now() - datetime.timedelta(1)
        # Find all messages.
        messages = UserMessage.all(keys_only=True)
        messages.filter("timestamp < ", older_than)
        results = messages.fetch(200)
        db.delete(results)

        message_count = len(results)

        # And do the same with the stats counter shards.
        shards = GeneralCounterShard.all(keys_only=True)
        shards.filter("name <", older_than.strftime("%Y-%m-%d_%H"))
        results = shards.fetch(200)
        db.delete(results)

        shard_count = len(results)

        shardconfigs = GeneralCounterShardConfig.all(keys_only=True)
        shardconfigs.filter("name < ", older_than.strftime("%Y-%m-%d_%H"))
        results = shardconfigs.fetch(200)
        db.delete(results)

        shard_config_count = len(results)

        renderer.addData("number", message_count)
        renderer.addData("shards", shard_count)
        renderer.addData("shardconfig", shard_config_count)
        return renderer.render("cron/deletemessages.html")
Ejemplo n.º 2
0
    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])
Ejemplo n.º 3
0
	def GET(self):
		# Older than 1 day.
		older_than = datetime.datetime.now() - datetime.timedelta(1)
		# Find all messages.
		messages = UserMessage.all(keys_only=True)
		messages.filter("timestamp < ", older_than)
		results = messages.fetch(200)
		db.delete(results)

		message_count = len(results)

		# And do the same with the stats counter shards.
		shards = GeneralCounterShard.all(keys_only=True)
		shards.filter("name <", older_than.strftime("%Y-%m-%d_%H"))
		results = shards.fetch(200)
		db.delete(results)

		shard_count = len(results)

		shardconfigs = GeneralCounterShardConfig.all(keys_only=True)
		shardconfigs.filter("name < ", older_than.strftime("%Y-%m-%d_%H"))
		results = shardconfigs.fetch(200)
		db.delete(results)

		shard_config_count = len(results)

		renderer.addData('number', message_count)
		renderer.addData('shards', shard_count)
		renderer.addData('shardconfig', shard_config_count)
		return renderer.render('cron/deletemessages.html')
Ejemplo n.º 4
0
	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])
Ejemplo n.º 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.
            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')
Ejemplo n.º 6
0
 def get_messages(self):
     return UserMessage.get_by_id(self.messages[-200:], self)
Ejemplo n.º 7
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')
Ejemplo n.º 8
0
	def get_messages(self):
		return UserMessage.get_by_id(self.messages[-200:], self)
Ejemplo n.º 9
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')