예제 #1
0
    def delete(self, request):
        user = User.objects.get(username=request.user)
        for trans in Translation.objects.filter(user=user):
            if os.path.exists(
                    unreleased_pdf_path(trans.task.contest.slug,
                                        trans.task.name, user)):
                os.remove(
                    unreleased_pdf_path(trans.task.contest.slug,
                                        trans.task.name, user))

        user.text_font_base64 = ''
        user.text_font_name = ''
        user.save()
        return JsonResponse({'message': "Done"})
예제 #2
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))
예제 #3
0
 def __remove_user_related_pdfs(self, user):
     for trans in Translation.objects.filter(user=user):
         slug = trans.task.contest.slug
         task_name = trans.task.name
         pdf_paths = [
             released_pdf_path(slug, task_name, user),
             unreleased_pdf_path(slug, task_name, user)
         ]
         for pdf_path in pdf_paths:
             if os.path.exists(pdf_path):
                 os.remove(pdf_path)
예제 #4
0
 def post(self, request):
     form = UploadFileForm(request.POST, request.FILES)
     if not form.is_valid():
         return HttpResponseBadRequest("You should attach a file")
     font_file = request.FILES['uploaded_file']
     if not font_file:
         return HttpResponseBadRequest("You should attach a file")
     import base64
     text_font_base64 = base64.b64encode(font_file.read())
     user = User.objects.get(username=request.user.username)
     for trans in Translation.objects.filter(user=user):
         if os.path.exists(
                 unreleased_pdf_path(trans.task.contest.slug,
                                     trans.task.name, user)):
             os.remove(
                 unreleased_pdf_path(trans.task.contest.slug,
                                     trans.task.name, user))
     user.text_font_base64 = text_font_base64
     user.text_font_name = font_file.name
     user.save()
     return HttpResponseRedirect(request.META['HTTP_REFERER'])