Пример #1
0
 def get(self, request, *args, **kwargs):
     if not request.user.has_perm('judge.change_problem'):
         raise Http404()
     problem = self.get_object()
     authors = ', '.join(map(attrgetter('user.username'), problem.authors.select_related('user')))
     document = latex_document(problem.name, authors, make_latex(format_markdown(problem.description)))
     return HttpResponse(document, content_type='text/plain')
Пример #2
0
    def get(self, request, *args, **kwargs):
        if not hasattr(settings, 'PROBLEM_PDF_CACHE'):
            raise Http404()

        problem = self.get_object()
        error_cache = os.path.join(settings.PROBLEM_PDF_CACHE, '%s.log' % problem.code)
        cache = os.path.join(settings.PROBLEM_PDF_CACHE, '%s.pdf' % problem.code)

        if os.path.exists(error_cache):
            with open(error_cache) as f:
                return HttpResponse(f.read(), status=500, content_type='text/plain')

        if not os.path.exists(cache):
            self.logger.info('Rendering: %s.pdf', problem.code)
            if getattr(settings, 'WEBKIT_PDF', False):
                with WebKitPdfMaker() as maker:
                    maker.html = get_template('problem/raw.jade').render(Context({
                        'problem': problem
                    })).replace('"//', '"http://').replace("'//", "'http://")
                    for file in ('style.css', 'pygment-github.css'):
                        maker.load(file, os.path.join(settings.DMOJ_RESOURCES, file))
                    maker.make()
                    if not maker.success:
                        with open(error_cache, 'wb') as f:
                            f.write(maker.log)
                        return HttpResponse(maker.log, status=500, content_type='text/plain')
                    os.rename(maker.pdffile, cache)
            else:
                try:
                    authors = ', '.join(map(attrgetter('user.username'), problem.authors.select_related('user')))
                    document = latex_document(problem.name, authors, make_latex(format_markdown(problem.description)))
                    with LatexPdfMaker(document) as latex:
                        latex.make()
                        if not latex.success:
                            # try:
                            # raise LatexError(latex.log)
                            # except LatexError:
                            #     self.logger.exception('Latex error while rendering: %s.pdf', problem.code)
                            if not latex.created:
                                with open(error_cache, 'wb') as f:
                                    f.write(latex.log)
                                return HttpResponse(latex.log, status=500, content_type='text/plain')
                        os.rename(latex.pdffile, cache)
                except:
                    self.logger.exception('Error while rendering: %s.pdf', problem.code)
                    raise
                else:
                    self.logger.info('Successfully rendered: %s.pdf', problem.code)

        response = HttpResponse()
        if hasattr(settings, 'PROBLEM_PDF_INTERNAL') and request.META.get('SERVER_SOFTWARE', '').startswith('nginx/'):
            response['X-Accel-Redirect'] = '%s/%s.pdf' % (settings.PROBLEM_PDF_INTERNAL, problem.code)
        else:
            with open(cache, 'rb') as f:
                response.content = f.read()

        response['Content-Type'] = 'application/pdf'
        response['Content-Disposition'] = 'inline; filename=%s.pdf' % problem.code
        return response
Пример #3
0
 def get(self, request, *args, **kwargs):
     if not request.user.has_perm('judge.change_problem'):
         raise Http404()
     problem = self.get_object()
     authors = ', '.join(
         map(attrgetter('user.username'),
             problem.authors.select_related('user')))
     document = latex_document(
         problem.name, authors,
         make_latex(format_markdown(problem.description)))
     return HttpResponse(document, content_type='text/plain')
Пример #4
0
    def get(self, request, *args, **kwargs):
        if not hasattr(settings, 'PROBLEM_PDF_CACHE'):
            raise Http404()

        problem = self.get_object()
        error_cache = os.path.join(settings.PROBLEM_PDF_CACHE,
                                   '%s.log' % problem.code)
        cache = os.path.join(settings.PROBLEM_PDF_CACHE,
                             '%s.pdf' % problem.code)

        if os.path.exists(error_cache):
            with open(error_cache) as f:
                return HttpResponse(f.read(),
                                    status=500,
                                    content_type='text/plain')

        if not os.path.exists(cache):
            self.logger.info('Rendering: %s.pdf', problem.code)
            if getattr(settings, 'WEBKIT_PDF', False):
                with WebKitPdfMaker() as maker:
                    maker.html = get_template('problem/raw.jade').render(
                        Context({'problem': problem})).replace(
                            '"//', '"http://').replace("'//", "'http://")
                    for file in ('style.css', 'pygment-github.css'):
                        maker.load(file,
                                   os.path.join(settings.DMOJ_RESOURCES, file))
                    maker.make()
                    if not maker.success:
                        with open(error_cache, 'wb') as f:
                            f.write(maker.log)
                        return HttpResponse(maker.log,
                                            status=500,
                                            content_type='text/plain')
                    os.rename(maker.pdffile, cache)
            else:
                try:
                    authors = ', '.join(
                        map(attrgetter('user.username'),
                            problem.authors.select_related('user')))
                    document = latex_document(
                        problem.name, authors,
                        make_latex(format_markdown(problem.description)))
                    with LatexPdfMaker(document) as latex:
                        latex.make()
                        if not latex.success:
                            # try:
                            # raise LatexError(latex.log)
                            # except LatexError:
                            #     self.logger.exception('Latex error while rendering: %s.pdf', problem.code)
                            if not latex.created:
                                with open(error_cache, 'wb') as f:
                                    f.write(latex.log)
                                return HttpResponse(latex.log,
                                                    status=500,
                                                    content_type='text/plain')
                        os.rename(latex.pdffile, cache)
                except:
                    self.logger.exception('Error while rendering: %s.pdf',
                                          problem.code)
                    raise
                else:
                    self.logger.info('Successfully rendered: %s.pdf',
                                     problem.code)

        response = HttpResponse()
        if hasattr(settings, 'PROBLEM_PDF_INTERNAL') and request.META.get(
                'SERVER_SOFTWARE', '').startswith('nginx/'):
            response['X-Accel-Redirect'] = '%s/%s.pdf' % (
                settings.PROBLEM_PDF_INTERNAL, problem.code)
        else:
            with open(cache, 'rb') as f:
                response.content = f.read()

        response['Content-Type'] = 'application/pdf'
        response[
            'Content-Disposition'] = 'inline; filename=%s.pdf' % problem.code
        return response
Пример #5
0
    def get(self, request, *args, **kwargs):
        if not hasattr(settings, "PROBLEM_PDF_CACHE"):
            raise Http404()

        problem = self.get_object()
        error_cache = os.path.join(settings.PROBLEM_PDF_CACHE, "%s.log" % problem.code)
        cache = os.path.join(settings.PROBLEM_PDF_CACHE, "%s.pdf" % problem.code)

        if os.path.exists(error_cache):
            with open(error_cache) as f:
                return HttpResponse(f.read(), status=500, content_type="text/plain")

        if not os.path.exists(cache):
            self.logger.info("Rendering: %s.pdf", problem.code)
            if getattr(settings, "WEBKIT_PDF", False):
                with WebKitPdfMaker() as maker:
                    maker.html = (
                        get_template("problem/raw.jade")
                        .render(Context({"problem": problem}))
                        .replace('"//', '"http://')
                        .replace("'//", "'http://")
                    )
                    for file in ("style.css", "pygment-github.css"):
                        maker.load(file, os.path.join(settings.DMOJ_RESOURCES, file))
                    maker.make()
                    if not maker.success:
                        with open(error_cache, "wb") as f:
                            f.write(maker.log)
                        return HttpResponse(maker.log, status=500, content_type="text/plain")
                    os.rename(maker.pdffile, cache)
            else:
                try:
                    authors = ", ".join(map(attrgetter("user.username"), problem.authors.select_related("user")))
                    document = latex_document(problem.name, authors, make_latex(format_markdown(problem.description)))
                    with LatexPdfMaker(document) as latex:
                        latex.make()
                        if not latex.success:
                            # try:
                            # raise LatexError(latex.log)
                            # except LatexError:
                            #     self.logger.exception('Latex error while rendering: %s.pdf', problem.code)
                            if not latex.created:
                                with open(error_cache, "wb") as f:
                                    f.write(latex.log)
                                return HttpResponse(latex.log, status=500, content_type="text/plain")
                        os.rename(latex.pdffile, cache)
                except:
                    self.logger.exception("Error while rendering: %s.pdf", problem.code)
                    raise
                else:
                    self.logger.info("Successfully rendered: %s.pdf", problem.code)

        response = HttpResponse()
        if hasattr(settings, "PROBLEM_PDF_INTERNAL") and request.META.get("SERVER_SOFTWARE", "").startswith("nginx/"):
            response["X-Accel-Redirect"] = "%s/%s.pdf" % (settings.PROBLEM_PDF_INTERNAL, problem.code)
        else:
            with open(cache, "rb") as f:
                response.content = f.read()

        response["Content-Type"] = "application/pdf"
        response["Content-Disposition"] = "inline; filename=%s.pdf" % problem.code
        return response