From 8515eb27944a74256bd4a6aa5c55d3a3d3a4ffa6 Mon Sep 17 00:00:00 2001 From: Evan Date: Mon, 16 Sep 2019 20:16:22 -0400 Subject: [PATCH] Move ANSI HTML-ifying to site; DMOJ/judge#478 --- judge/jinja2/reference.py | 6 ++++ .../0088_compiler_message_ansi2html.py | 31 +++++++++++++++++++ judge/models/submission.py | 2 +- requirements.txt | 1 + templates/submission/status-testcases.html | 4 +-- 5 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 judge/migrations/0088_compiler_message_ansi2html.py diff --git a/judge/jinja2/reference.py b/judge/jinja2/reference.py index 4f6acdc672..5afc00d061 100644 --- a/judge/jinja2/reference.py +++ b/judge/jinja2/reference.py @@ -2,6 +2,7 @@ from collections import defaultdict from urllib.parse import urljoin +from ansi2html import Ansi2HTMLConverter from django.contrib.auth.models import AbstractUser from django.urls import reverse from lxml.html import Element @@ -178,3 +179,8 @@ def join(first, second, *rest): if not rest: return urljoin(first, second) return urljoin(urljoin(first, second), *rest) + + +@registry.filter(name='ansi2html') +def ansi2html(s): + return Ansi2HTMLConverter(inline=True).convert(s, full=False) diff --git a/judge/migrations/0088_compiler_message_ansi2html.py b/judge/migrations/0088_compiler_message_ansi2html.py new file mode 100644 index 0000000000..7bb306a061 --- /dev/null +++ b/judge/migrations/0088_compiler_message_ansi2html.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 2.1.12 on 2019-09-17 13:30 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion +import lxml.html as lh +from lxml.html.clean import clean_html + + +def strip_error_html(apps, schema_editor): + Submission = apps.get_model('judge', 'Submission') + for sub in Submission.objects.filter(error__isnull=False).iterator(): + sub.error = clean_html(lh.fromstring(sub.error)).text_content() if sub.error else None + sub.save(update_fields=['error']) + + +class Migration(migrations.Migration): + + dependencies = [ + ('judge', '0087_problem_resource_limits'), + ] + + operations = [ + migrations.AlterField( + model_name='submission', + name='error', + field=models.TextField(null=True, verbose_name='compile errors'), + ), + migrations.RunPython(strip_error_html, migrations.RunPython.noop, atomic=True), + ] diff --git a/judge/models/submission.py b/judge/models/submission.py index 8bbefda6a5..94989ddddb 100644 --- a/judge/models/submission.py +++ b/judge/models/submission.py @@ -71,7 +71,7 @@ class Submission(models.Model): status = models.CharField(verbose_name=_('status'), max_length=2, choices=STATUS, default='QU', db_index=True) result = models.CharField(verbose_name=_('result'), max_length=3, choices=SUBMISSION_RESULT, default=None, null=True, blank=True, db_index=True) - error = models.TextField(verbose_name=_('compile errors'), null=True, blank=True) + error = models.TextField(verbose_name=_('compile errors'), null=True) current_testcase = models.IntegerField(default=0) batch = models.BooleanField(verbose_name=_('batched cases'), default=False) case_points = models.FloatField(verbose_name=_('test case points'), default=0) diff --git a/requirements.txt b/requirements.txt index e96eb511a1..3a305689ec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,3 +28,4 @@ jsonfield pymoss packaging celery +ansi2html diff --git a/templates/submission/status-testcases.html b/templates/submission/status-testcases.html index 46eb4f7c9c..2c5740b342 100644 --- a/templates/submission/status-testcases.html +++ b/templates/submission/status-testcases.html @@ -12,11 +12,11 @@

{{ _('We are waiting for a suitable judge to process your submission...') }}

{{ _('Your submission is being processed...') }}

{% elif submission.status == 'CE' %}

{{ _('Compilation Error') }}

-
{{ submission.error|safe }}
+
{{ submission.error|ansi2html }}
{% else %} {% if submission.error %}

{{ _('Compilation Warnings') }}

-
{{ submission.error|safe }}
+
{{ submission.error|ansi2html }}


{% endif %} {% if is_pretest %}