def get(self, request, *args, **kwargs): if not HAS_PDF: raise Http404() language = kwargs.get('language', self.request.LANGUAGE_CODE) if language not in self.languages: raise Http404() problem = self.get_object() try: trans = problem.translations.get(language=language) except ProblemTranslation.DoesNotExist: trans = None error_cache = os.path.join(settings.PROBLEM_PDF_CACHE, '%s.%s.log' % (problem.code, language)) cache = os.path.join(settings.PROBLEM_PDF_CACHE, '%s.%s.pdf' % (problem.code, language)) 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.%s.pdf', problem.code, language) with DefaultPdfMaker() as maker, translation.override(language): problem_name = problem.name if trans is None else trans.name 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': request.build_absolute_uri(), 'math_engine': maker.math_engine, }).replace('"//', '"https://').replace("'//", "'https://") maker.title = problem_name assets = ['style.css', 'pygment-github.css'] if maker.math_engine == 'jax': assets.append('mathjax_config.js') for file in assets: 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) self.logger.error('Failed to render PDF for %s', problem.code) return HttpResponse(maker.log, status=500, content_type='text/plain') shutil.move(maker.pdffile, cache) response = HttpResponse() if hasattr(settings, 'PROBLEM_PDF_INTERNAL') and request.META.get('SERVER_SOFTWARE', '').startswith('nginx/'): response['X-Accel-Redirect'] = '%s/%s.%s.pdf' % (settings.PROBLEM_PDF_INTERNAL, problem.code, language) else: with open(cache, 'rb') as f: response.content = f.read() response['Content-Type'] = 'application/pdf' response['Content-Disposition'] = 'inline; filename=%s.%s.pdf' % (problem.code, language) return response
def handle(self, *args, **options): try: problem = Problem.objects.get(code=options['code']) except Problem.DoesNotExist: print('Bad problem code') return try: trans = problem.translations.get(language=options['language']) except ProblemTranslation.DoesNotExist: trans = None directory = options['directory'] with DefaultPdfMaker() as maker, \ translation.override(options['language']): problem_name = problem.name if trans is None else trans.name 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': '', }).replace('"//', '"https://').replace("'//", "'https://") maker.title = problem_name maker.load('style.css', os.path.join(settings.STATIC_ROOT, 'css', 'style.css')) maker.load( 'pygment-github.css', os.path.join(settings.STATIC_ROOT, 'css', 'pygment-github.css')) maker.load( 'mathjax_config.js', os.path.join(settings.STATIC_ROOT, 'js', 'mathjax_config.js')) maker.load( 'mathjax_config.MathJax', os.path.join(settings.STATIC_ROOT, 'libs', 'mathjax', 'MathJax.js')) maker.make(debug=True) if not maker.success: print(maker.log, file=sys.stderr) elif directory is None: shutil.move(maker.pdffile, problem.code + '.pdf')