Converts a submission to json (keeps only needed fields)

def submission_to_json(self, task, data, debug, reloading=False, replace=False, tags={}):
        """ Converts a submission to json (keeps only needed fields) """

        if "ssh_host" in data:
            return json.dumps({'status': "waiting", 'text': "<b>SSH server active</b>",
                               'ssh_host': data["ssh_host"], 'ssh_port': data["ssh_port"],
                               'ssh_password': data["ssh_password"]})

        # Here we are waiting. Let's send some useful information.
        waiting_data = self.submission_manager.get_job_queue_info(data["jobid"]) if "jobid" in data else None
        if waiting_data is not None and not reloading:
            nb_tasks_before, approx_wait_time = waiting_data
            wait_time = round(approx_wait_time)
            if nb_tasks_before == -1 and wait_time <= 0:
                text = _("<b>INGInious is currently grading your answers.<b/> (almost done)")
            elif nb_tasks_before == -1:
                text = _("<b>INGInious is currently grading your answers.<b/> (Approx. wait time: {} seconds)").format(
                    wait_time)
            elif nb_tasks_before == 0:
                text = _("<b>You are next in the waiting queue!</b>")
            elif nb_tasks_before == 1:
                text = _("<b>There is one task in front of you in the waiting queue.</b>")
            else:
                text = _("<b>There are {} tasks in front of you in the waiting queue.</b>").format(nb_tasks_before)

            return json.dumps({'status': "waiting", 'text': text})

        tojson = {
            'status': data['status'],
            'result': data.get('result', 'crash'),
            'id': str(data["_id"]),
            'submitted_on': str(data['submitted_on']),
            'grade': str(data.get("grade", 0.0)),
            'replace': replace and not reloading  # Replace the evaluated submission
        }

        if "text" in data:
            tojson["text"] = data["text"]
        if "problems" in data:
            tojson["problems"] = data["problems"]

        if debug:
            tojson["debug"] = data

        if tojson['status'] == 'waiting':
            tojson["text"] = _("<b>Your submission has been sent...</b>")
        elif tojson["result"] == "failed":
            tojson["text"] = _("There are some errors in your answer. Your score is {score}%.").format(score=data["grade"])
        elif tojson["result"] == "success":
            tojson["text"] = _("Your answer passed the tests! Your score is {score}%.").format(score=data["grade"])
        elif tojson["result"] == "timeout":
            tojson["text"] = _("Your submission timed out. Your score is {score}%.").format(score=data["grade"])
        elif tojson["result"] == "overflow":
            tojson["text"] = _("Your submission made an overflow. Your score is {score}%.").format(score=data["grade"])
        elif tojson["result"] == "killed":
            tojson["text"] = _("Your submission was killed.")
        else:
            tojson["text"] = _("An internal error occurred. Please retry later. "
                               "If the error persists, send an email to the course administrator.")

        tojson["text"] = "<b>" + tojson["text"] + " " + _("[Submission #{submissionid}]").format(submissionid=data["_id"]) + "</b>" + data.get("text", "")
        tojson["text"] = self.plugin_manager.call_hook_recursive("feedback_text", task=task, submission=data, text=tojson["text"])["text"]

        if reloading:
            # Set status='ok' because we are reloading an old submission.
            tojson["status"] = 'ok'
            # And also include input
            tojson["input"] = data.get('input', {})

        if "tests" in data:
            tojson["tests"] = {}
            if tags:
                for tag in tags[0]+tags[1]: # Tags only visible for admins should not appear in the json for students.
                    if (tag.is_visible_for_student() or debug) and tag.get_id() in data["tests"]:
                        tojson["tests"][tag.get_id()] = data["tests"][tag.get_id()]
            if debug: #We add also auto tags when we are admin
                for tag in data["tests"]:
                    if tag.startswith("*auto-tag-"):
                        tojson["tests"][tag] = data["tests"][tag]

        # allow plugins to insert javascript to be run in the browser after the submission is loaded
        tojson["feedback_script"] = "".join(self.plugin_manager.call_hook("feedback_script", task=task, submission=data))

        return json.dumps(tojson, default=str)