<commit_before>import logging
import json

import paasmaker
from paasmaker.common.controller import BaseController, BaseControllerTest, BaseWebsocketHandler
from paasmaker.common.core import constants

from pubsub import pub
import tornado
import tornado.testing
import colander
from ws4py.client.tornadoclient import TornadoWebSocketClient

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

class JobController(BaseController):
	AUTH_METHODS = [BaseController.SUPER, BaseController.USER]

	def _get_workspace(self, workspace_id):
		workspace = self.db().query(paasmaker.model.Workspace).get(int(workspace_id))
		if not workspace:
			raise tornado.web.HTTPError(404, "No such workspace.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=workspace)
		return workspace

	def _get_application(self, application_id):
		application = self.db().query(paasmaker.model.Application).get(int(application_id))
		if not application:
			raise tornado.web.HTTPError(404, "No such application.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=application.workspace)
		return application

	def _get_version(self, version_id):
		version = self.db().query(paasmaker.model.ApplicationVersion).get(int(version_id))
		if not version:
			raise tornado.web.HTTPError(404, "No such version.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=version.application.workspace)
		return version

	def _get_instance_type(self, instance_type_id):
		instance_type = self.db().query(paasmaker.model.ApplicationInstanceType).get(int(instance_type_id))
		if not instance_type:
			raise tornado.web.HTTPError(404, "No such instance type.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=instance_type.application_version.application.workspace)
		return instance_type

	@tornado.web.asynchronous
	def get(self, job_list_type, input_id):
		tag = None
		job_list = None
		ret = None
		sub_type = ""
		if self.raw_params.has_key('sub'):
			sub_type = self.raw_params['sub'] + ':'
		if job_list_type == 'workspace':
			workspace = self._get_workspace(input_id)
			name = "Workspace %s" % workspace.name
			ret = "/workspace/%d/applications" % workspace.id
			ret_name = name
			tag = "workspace:%s%d" % (sub_type, workspace.id)
		elif job_list_type == 'application':
			application = self._get_application(input_id)
			name = "Application %s" % application.name
			ret = "/application/%d" % application.id
			ret_name = name
			tag = "application:%s%d" % (sub_type, application.id)
		elif job_list_type == 'version':
			version = self._get_version(input_id)
			name = "Version %d of %s" % (version.version, version.application.name)
			ret = "/version/%d" % version.id
			ret_name = name
			tag = "application_version:%s%d" % (sub_type, version.id)
		elif job_list_type == 'instancetype':
			instance_type = self._get_instance_type(input_id)
			name = "Instance type %s of %s version %d" % (
				instance_type.name,
				instance_type.application_version.application.name,
				instance_type.application_version.version
			)
			ret = "/version/%d" % instance_type.application_version.id
			ret_name = name
			tag = "application_instance_type:%s%d" % (sub_type, instance_type.id)
		elif job_list_type == 'detail':
			# TODO: We're not checking permissions here. But the theory is that
			# the job ID will be hard to guess. Revisit this at a later date.
			job_list = [input_id]
			name = "Detail for job"

		# Optional return URL.
		if self.raw_params.has_key('ret'):
			ret = self.raw_params['ret']
			ret_name = "previous"

		self.add_data_template('name', name)
		self.add_data_template('ret', ret)
		self.add_data_template('ret_name', ret_name)

		# TODO: Paginate...
		# TODO: Unit test.
		def on_found_jobs(job_ids):
			self.add_data('jobs', job_ids)
			self.render("job/list.html")

		if tag:<commit_msg>Set a return name in a corner case.<commit_after>import logging
import json

import paasmaker
from paasmaker.common.controller import BaseController, BaseControllerTest, BaseWebsocketHandler
from paasmaker.common.core import constants

from pubsub import pub
import tornado
import tornado.testing
import colander
from ws4py.client.tornadoclient import TornadoWebSocketClient

logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())

class JobController(BaseController):
	AUTH_METHODS = [BaseController.SUPER, BaseController.USER]

	def _get_workspace(self, workspace_id):
		workspace = self.db().query(paasmaker.model.Workspace).get(int(workspace_id))
		if not workspace:
			raise tornado.web.HTTPError(404, "No such workspace.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=workspace)
		return workspace

	def _get_application(self, application_id):
		application = self.db().query(paasmaker.model.Application).get(int(application_id))
		if not application:
			raise tornado.web.HTTPError(404, "No such application.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=application.workspace)
		return application

	def _get_version(self, version_id):
		version = self.db().query(paasmaker.model.ApplicationVersion).get(int(version_id))
		if not version:
			raise tornado.web.HTTPError(404, "No such version.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=version.application.workspace)
		return version

	def _get_instance_type(self, instance_type_id):
		instance_type = self.db().query(paasmaker.model.ApplicationInstanceType).get(int(instance_type_id))
		if not instance_type:
			raise tornado.web.HTTPError(404, "No such instance type.")
		self.require_permission(constants.PERMISSION.WORKSPACE_VIEW, workspace=instance_type.application_version.application.workspace)
		return instance_type

	@tornado.web.asynchronous
	def get(self, job_list_type, input_id):
		tag = None
		job_list = None
		ret = None
		sub_type = ""
		if self.raw_params.has_key('sub'):
			sub_type = self.raw_params['sub'] + ':'
		if job_list_type == 'workspace':
			workspace = self._get_workspace(input_id)
			name = "Workspace %s" % workspace.name
			ret = "/workspace/%d/applications" % workspace.id
			ret_name = name
			tag = "workspace:%s%d" % (sub_type, workspace.id)
		elif job_list_type == 'application':
			application = self._get_application(input_id)
			name = "Application %s" % application.name
			ret = "/application/%d" % application.id
			ret_name = name
			tag = "application:%s%d" % (sub_type, application.id)
		elif job_list_type == 'version':
			version = self._get_version(input_id)
			name = "Version %d of %s" % (version.version, version.application.name)
			ret = "/version/%d" % version.id
			ret_name = name
			tag = "application_version:%s%d" % (sub_type, version.id)
		elif job_list_type == 'instancetype':
			instance_type = self._get_instance_type(input_id)
			name = "Instance type %s of %s version %d" % (
				instance_type.name,
				instance_type.application_version.application.name,
				instance_type.application_version.version
			)
			ret = "/version/%d" % instance_type.application_version.id
			ret_name = name
			tag = "application_instance_type:%s%d" % (sub_type, instance_type.id)
		elif job_list_type == 'detail':
			# TODO: We're not checking permissions here. But the theory is that
			# the job ID will be hard to guess. Revisit this at a later date.
			job_list = [input_id]
			name = "Detail for job"
			ret_name = "previous"

		# Optional return URL.
		if self.raw_params.has_key('ret'):
			ret = self.raw_params['ret']
			ret_name = "previous"

		self.add_data_template('name', name)
		self.add_data_template('ret', ret)
		self.add_data_template('ret_name', ret_name)

		# TODO: Paginate...
		# TODO: Unit test.
		def on_found_jobs(job_ids):
			self.add_data('jobs', job_ids)
			self.render("job/list.html")

		if tag:
{'fraction_non_alphanumeric': 0.11322198138467424, 'fraction_numerical': 0.0033150580135152363, 'mean_word_length': 4.974105102817974, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7541900', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2540, 'n_words': 540}