Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions app/db/db_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,18 @@ def add_check(file_id, check):
def update_check(check):
return bool(checks_collection.find_one_and_replace({'_id': check._id}, check.pack()))

def get_pdf_id(file_id=None):
if not file_id: file_id = ObjectId()
return file_id

def write_pdf(filename, filepath):
def write_pdf(filename, filepath, file_id):
converted_filepath = convert_to(filepath, target_format='pdf')
return add_file_to_db(filename, converted_filepath)
return add_file_to_db(filename, converted_filepath, file_id)


def add_file_to_db(filename, filepath, file_id=None):
if not file_id: file_id = ObjectId()
fs.upload_from_stream_with_id(file_id, filename, open(filepath, 'rb'))
def add_file_to_db(filename, filepath, file_id):
pdf_obj_id = ObjectId(file_id)
fs.upload_from_stream_with_id(pdf_obj_id, filename, open(filepath, 'rb'))
return file_id


Expand Down
21 changes: 16 additions & 5 deletions app/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import bson
import pandas as pd
from bson import ObjectId
from celery import chain
from celery.result import AsyncResult
from flask import (Flask, Response, abort, jsonify, redirect, render_template,
request, url_for)
Expand All @@ -27,7 +28,7 @@
init_criterions, BASE_PRES_CRITERION, BASE_REPORT_CRITERION
from root_logger import get_logging_stdout_handler, get_root_logger
from servants import pre_luncher
from tasks import create_task
from tasks import create_task, convert_to_pdf
from utils import checklist_filter, decorator_assertion, get_file_len, format_check
from app.main.checks import CRITERIA_INFO
from routes.admin import admin
Expand Down Expand Up @@ -223,7 +224,11 @@ def run_task():
pdf_file.save(filepathpdf)
converted_id = db_methods.add_file_to_db(filenamepdf, filepathpdf)
else:
converted_id = db_methods.write_pdf(filename, filepath)
logger.info(
f"Запуск конвертации файла '{file.filename}' в pdf")
converted_id = str(db_methods.get_pdf_id(file_id=None))
# convert_to_pdf.delay(filename, filepath, converted_id)

check = Check({
'_id': file_id,
'conv_pdf_fs_id': converted_id,
Expand All @@ -239,9 +244,15 @@ def run_task():
'params_for_passback': current_user.params_for_passback
})
db_methods.add_check(file_id, check) # add check for parsed_file to db
task = create_task.delay(check.pack(to_str=True)) # add check to queue
db_methods.add_celery_task(task.id, file_id) # mapping celery_task to check (check_id = file_id)
return {'task_id': task.id, 'check_id': str(file_id)}
# task = create_task.delay(check.pack(to_str=True)) # add check to queue
# db_methods.add_celery_task(task.id, file_id) # mapping celery_task to check (check_id = file_id)
task_chain = chain(
convert_to_pdf.s(filename, filepath, converted_id),
create_task.s(check.pack(to_str=True))
)
result = task_chain.apply_async()
task_id = result.id
return {'task_id': task_id, 'check_id': str(file_id)}


@app.route("/recheck/<check_id>", methods=["GET"])
Expand Down
11 changes: 10 additions & 1 deletion app/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def at_start(sender, **k):


@celery.task(name="create_task", queue='check-solution', bind=True)
def create_task(self, check_info):
def create_task(self, conv_id, check_info):
check_obj = Check(check_info)
check_id = str(check_obj._id)
# get check files filepath
Expand Down Expand Up @@ -77,6 +77,15 @@ def create_task(self, check_info):
logger.error(f"\tПри обработке произошла ошибка: {e}. Попытка повторного запуска", exc_info=True)
self.retry(countdown=TASK_RETRY_COUNTDOWN) # Retry the task, adding it to the back of the queue.

@celery.task(name="convert_to_pdf", queue='check-solution', bind=True)
def convert_to_pdf(self, filename, filepath, pdf_id):
try:
conv_id = db_methods.write_pdf(filename, filepath, pdf_id)
# converted_id = db_methods.write_pdf(filename, filepath)
return conv_id
except Exception as e:
logger.error(f"При конвертации файла произошла ошибка: {e}. Новая попытка", exc_info=True)
raise self.retry(countdown=TASK_RETRY_COUNTDOWN)

@celery.task(name="passback-task", queue='passback-grade')
def passback_task():
Expand Down
14 changes: 7 additions & 7 deletions app/templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ <h4 id="results_title" class="texteous ins">
{% endfor %}
</tbody>
</table>
<div class="top-bar" style="text-align:center; width:100%">
<button class="btn" id="prev-page"><i class="bi bi-arrow-left"></i></button>
<button class="btn" id="next-page"><i class="bi bi-arrow-right"></i></button>
<span class="page-info">
Слайд <span id="page-num"></span> из <span id="page-count"></span>
</span>
</div>
{% endif %}

<!-- PDF viewer -->
{% if not current_user.is_anonymous and results.conv_pdf_fs_id %}
<div style="text-align:center; width:100%">
<div class="top-bar">
<button class="btn" id="prev-page"><i class="bi bi-arrow-left"></i></button>
<button class="btn" id="next-page"><i class="bi bi-arrow-right"></i></button>
<span class="page-info">
Слайд <span id="page-num"></span> из <span id="page-count"></span>
</span>
</div>
<canvas id="the-canvas" style="border: 1px solid #000000; direction: ltr;"></canvas>
</div>
{% endif %}
Expand Down
6 changes: 5 additions & 1 deletion assets/scripts/results.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ $(function(){

canvas = document.getElementById('the-canvas');
ctx = canvas.getContext('2d');


ctx.font = "20px Times New Roman";
ctx.textAlign = "center";
ctx.fillText("Конвертация файла в PDF...", canvas.width / 2, canvas.height / 2);

pdfjsLib
.getDocument(href)
.promise.then(pdfDoc_ => {
Expand Down