2020-01-21 06:35:58 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.template.loader import get_template
|
|
|
|
from django.utils import translation
|
|
|
|
|
|
|
|
from judge.models import Problem, ProblemTranslation
|
2022-05-14 17:57:27 +00:00
|
|
|
from judge.pdf_problems import (
|
|
|
|
DefaultPdfMaker,
|
|
|
|
PhantomJSPdfMaker,
|
|
|
|
PuppeteerPDFRender,
|
|
|
|
SeleniumPDFRender,
|
|
|
|
SlimerJSPdfMaker,
|
|
|
|
)
|
|
|
|
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2022-05-14 17:57:27 +00:00
|
|
|
help = "renders a PDF file of a problem"
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2022-05-14 17:57:27 +00:00
|
|
|
parser.add_argument("code", help="code of problem to render")
|
|
|
|
parser.add_argument(
|
|
|
|
"directory", nargs="?", help="directory to store temporaries"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--language",
|
|
|
|
default=settings.LANGUAGE_CODE,
|
|
|
|
help="language to render PDF in",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-p",
|
|
|
|
"--phantomjs",
|
|
|
|
action="store_const",
|
|
|
|
const=PhantomJSPdfMaker,
|
|
|
|
default=DefaultPdfMaker,
|
|
|
|
dest="engine",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-s",
|
|
|
|
"--slimerjs",
|
|
|
|
action="store_const",
|
|
|
|
const=SlimerJSPdfMaker,
|
|
|
|
dest="engine",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-c",
|
|
|
|
"--chrome",
|
|
|
|
"--puppeteer",
|
|
|
|
action="store_const",
|
|
|
|
const=PuppeteerPDFRender,
|
|
|
|
dest="engine",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-S",
|
|
|
|
"--selenium",
|
|
|
|
action="store_const",
|
|
|
|
const=SeleniumPDFRender,
|
|
|
|
dest="engine",
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
problem = Problem.objects.get(code=options["code"])
|
2020-01-21 06:35:58 +00:00
|
|
|
except Problem.DoesNotExist:
|
2022-05-14 17:57:27 +00:00
|
|
|
print("Bad problem code")
|
2020-01-21 06:35:58 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2022-05-14 17:57:27 +00:00
|
|
|
trans = problem.translations.get(language=options["language"])
|
2020-01-21 06:35:58 +00:00
|
|
|
except ProblemTranslation.DoesNotExist:
|
|
|
|
trans = None
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
directory = options["directory"]
|
|
|
|
with options["engine"](
|
|
|
|
directory, clean_up=directory is None
|
|
|
|
) as maker, translation.override(options["language"]):
|
2020-01-21 06:35:58 +00:00
|
|
|
problem_name = problem.name if trans is None else trans.name
|
2022-05-14 17:57:27 +00:00
|
|
|
maker.html = (
|
|
|
|
get_template("problem/raw.html")
|
|
|
|
.render(
|
|
|
|
{
|
|
|
|
"problem": problem,
|
|
|
|
"problem_name": problem_name,
|
|
|
|
"description": problem.description
|
|
|
|
if trans is None
|
|
|
|
else trans.description,
|
|
|
|
"url": "",
|
|
|
|
"math_engine": maker.math_engine,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
.replace('"//', '"https://')
|
|
|
|
.replace("'//", "'https://")
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
maker.title = problem_name
|
2024-02-05 23:02:49 +00:00
|
|
|
for file in "style.css":
|
2020-01-21 06:35:58 +00:00
|
|
|
maker.load(file, os.path.join(settings.DMOJ_RESOURCES, file))
|
|
|
|
maker.make(debug=True)
|
|
|
|
if not maker.success:
|
|
|
|
print(maker.log, file=sys.stderr)
|
|
|
|
elif directory is None:
|
2022-05-14 17:57:27 +00:00
|
|
|
shutil.move(maker.pdffile, problem.code + ".pdf")
|