<commit_before>"""
********************************************************************************
* Name: job_manager.py
* Author: <NAME>
* Created On: 2015
* Copyright: (c) Brigham Young University 2015
* License: BSD 2-Clause
********************************************************************************
"""
from tethys_compute.models import TethysJob, CondorJob, BasicJob
import re

JOB_TYPES = {'CONDOR': CondorJob,
             'BASIC': BasicJob,
            }

class JobManager(object):
    """

    """

    # for backwards compatibility
    JOB_TYPES_DICT = JOB_TYPES

    def __init__(self, app):
        self.app = app
        self.label = app.package
        self.app_workspace = app.get_app_workspace()
        self.job_templates = dict()
        for template in app.job_templates():
            self.job_templates[template.name] = template

    def create_job(self, name, user, template_name, **kwargs):
        try:
            template = self.job_templates[template_name]
        except KeyError, e:
            raise KeyError('A job template with name %s was not defined' % (template_name,))
        JobClass = template.type
        user_workspace = self.app.get_user_workspace(user)

        kwrgs = dict(name=name, user=user, label=self.label, workspace=user_workspace.path)
        parameters = self._replace_workspaces(template.parameters, user_workspace)
        kwrgs.update(parameters)
        kwrgs.update(kwargs)
        job = JobClass(**kwrgs)
        return job

    def list_jobs(self, user, order_by='id'):
        """
        Lists all the jobs from current app for current user
        """
        jobs = TethysJob.objects.filter(label=self.label, user=user).order_by(order_by)
        jobs = [job.child for job in jobs]
        return jobs

    def get_job(self, job_id):
        """
        Get job by id
        """
        job = TethysJob.objects.filter(label=self.label, id=job_id)
        if job:
            return job[0].child<commit_msg>tethys-152 Job Manager Security Issue #152<commit_after>"""
********************************************************************************
* Name: job_manager.py
* Author: <NAME>
* Created On: 2015
* Copyright: (c) Brigham Young University 2015
* License: BSD 2-Clause
********************************************************************************
"""
from tethys_compute.models import TethysJob, CondorJob, BasicJob
import re

JOB_TYPES = {'CONDOR': CondorJob,
             'BASIC': BasicJob,
            }

class JobManager(object):
    """

    """

    # for backwards compatibility
    JOB_TYPES_DICT = JOB_TYPES

    def __init__(self, app):
        self.app = app
        self.label = app.package
        self.app_workspace = app.get_app_workspace()
        self.job_templates = dict()
        for template in app.job_templates():
            self.job_templates[template.name] = template

    def create_job(self, name, user, template_name, **kwargs):
        try:
            template = self.job_templates[template_name]
        except KeyError, e:
            raise KeyError('A job template with name %s was not defined' % (template_name,))
        JobClass = template.type
        user_workspace = self.app.get_user_workspace(user)

        kwrgs = dict(name=name, user=user, label=self.label, workspace=user_workspace.path)
        parameters = self._replace_workspaces(template.parameters, user_workspace)
        kwrgs.update(parameters)
        kwrgs.update(kwargs)
        job = JobClass(**kwrgs)
        return job

    def list_jobs(self, user=None, order_by='id'):
        """
        Lists all the jobs from current app for current user
        """
        filters = dict()
        filters['label'] = self.label
        if user:
            filters['user'] = user
        jobs = TethysJob.objects.filter(**filters).order_by(order_by)
        jobs = [job.child for job in jobs]
        return jobs

    def get_job(self, job_id, user=None):
        """
        Get job by id
        """
        job = TethysJob.objects.filter(label=self.label, id=job_id)
        if job:
            job = job[0].child

            if user and job.user != user:
                return None

            return job
{'fraction_non_alphanumeric': 0.17261904761904762, 'fraction_numerical': 0.006190476190476191, 'mean_word_length': 2.6657940663176265, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6175730', 'n_tokens_mistral': 1234, 'n_tokens_neox': 1181, 'n_words': 289}