Exemple #1
0
    def get(self, request, contest_slug, task_name, task_type):
        user = User.objects.get(username=request.user)
        translation = get_translation_by_contest_and_task_type(
            request, user, contest_slug, task_name, task_type)
        requested_user = get_requested_user(request, task_type)
        if translation.frozen:
            pdf_file_path = final_pdf_path(contest_slug, task_name,
                                           requested_user)
            return pdf_response(pdf_file_path,
                                get_file_name_from_path(pdf_file_path))

        pdf_file_path = unreleased_pdf_path(contest_slug, task_name,
                                            requested_user)
        last_edit_time = translation.get_latest_change_time()
        rebuild_needed = not os.path.exists(
            pdf_file_path) or os.path.getmtime(pdf_file_path) < last_edit_time
        if rebuild_needed:
            html = render_pdf_template(request,
                                       user,
                                       contest_slug,
                                       task_name,
                                       task_type,
                                       static_path=settings.STATIC_ROOT,
                                       images_path=settings.MEDIA_ROOT +
                                       'images/',
                                       pdf_output=True)
            convert_html_to_pdf(html, pdf_file_path)
            add_page_numbers_to_pdf(pdf_file_path, task_name)

        return pdf_response(pdf_file_path,
                            get_file_name_from_path(pdf_file_path))
    def _get_translation_by_contest_and_task_type(self, request, user,
                                                  contest_slug, task_name,
                                                  task_type):
        requested_user = get_requested_user(request, task_type)
        task = get_task_by_contest_and_name(contest_slug, task_name,
                                            user.is_editor())

        if task_type == 'released':
            return task.get_base_translation()
        return get_trans_by_user_and_task(requested_user, task)
    def get(self, request, contest_slug, task_name, task_type):
        user = User.objects.get(username=request.user)
        requested_user = get_requested_user(request, task_type)
        version_id = request.GET.get('ver')
        if version_id:
            content_version = Version.objects.filter(id=version_id).first()
            if not content_version.can_view_by(user):
                return None
            content = content_version.text
        else:
            try:
                task = get_task_by_contest_and_name(contest_slug, task_name,
                                                    user.is_editor())
            except Exception as e:
                return HttpResponseBadRequest(e)
            if task_type == 'released':
                content = task.get_published_text()
            else:
                translation = get_trans_by_user_and_task(requested_user, task)
                content = translation.get_latest_text()

        return HttpResponse(content, content_type='text/plain; charset=UTF-8')