Skip to content
Snippets Groups Projects

Generování protokolů a zpracování scanů

Merged Martin Mareš requested to merge mj/protokoly into devel
1 file
+ 28
24
Compare changes
  • Side-by-side
  • Inline
+ 43
35
@@ -13,37 +13,41 @@ import werkzeug.utils
import zipfile
import mo.db as db
from mo.jobs import TheJob, job_handler, job_file_path
from mo.jobs import TheJob, job_handler
from mo.submit import Submitter, SubmitException
from mo.util import logger, data_dir
from mo.util_format import inflect_number, inflect_by_number, data_size
def schedule_download_submits(paper_ids: List[int], description: str, for_user: db.User, want_subdirs: bool):
#
# Job download_submits: Zazipuje vybrané papíry
#
# Vstupní JSON:
# { 'papers': [ seznam paper_id ke stažení ],
# 'want_feedback': true/false,
# 'out_name': jméno výstupního souboru bez přípony,
# }
#
# Výstupní JSON:
# null
#
def schedule_download_submits(paper_ids: List[int], description: str, for_user: db.User, want_subdirs: bool, out_name: str):
the_job = TheJob()
job = the_job.create(db.JobType.download_submits, for_user)
job.description = description
job.in_json = {'papers': paper_ids, 'want_subdirs': want_subdirs}
job.in_json = {'papers': paper_ids, 'want_subdirs': want_subdirs, 'out_name': out_name}
the_job.submit()
@job_handler(db.JobType.download_submits)
def handle_download_submits(the_job: TheJob):
"""Zazipuje zadané papíry.
Vstupní JSON:
{ 'papers': [ seznam paper_id ke stažení ],
'want_feedback': true/false,
}
Výstupní JSON:
null
"""
job = the_job.job
assert job.in_json is not None
ids: List[int] = job.in_json['papers'] # type: ignore
want_subdirs: bool = job.in_json['want_subdirs'] # type: ignore
out_name: str = job.in_json['out_name'] # type: ignore
sess = db.get_session()
papers = (sess.query(db.Paper, db.User, db.Task.code, db.Place)
@@ -57,11 +61,13 @@ def handle_download_submits(the_job: TheJob):
.all())
papers.sort(key=lambda p: (p[1].sort_key(), p[2]))
temp_file = NamedTemporaryFile(suffix='.zip', dir=data_dir('tmp'), mode='w+b')
logger.debug('Job: Vytvářím archiv %s', temp_file.name)
out_name = werkzeug.utils.secure_filename(out_name + '.zip')
out_path = job.file_path(out_name)
out_file = open(out_path, mode='w+b')
logger.debug('Job: Vytvářím archiv %s', out_path)
cnt = 0
with zipfile.ZipFile(temp_file, mode='w') as zip:
with zipfile.ZipFile(out_file, mode='w') as zip:
for p, u, task_code, place in papers:
cnt += 1
full_name = u.full_name()
@@ -76,10 +82,25 @@ def handle_download_submits(the_job: TheJob):
zip.write(filename=os.path.join(data_dir('submits'), p.file_name or p.orig_file_name),
arcname=fn)
job.out_file = the_job.attach_file(temp_file.name, '.zip')
out_size = temp_file.tell()
job.out_file = out_name
out_size = out_file.tell()
job.result = 'Celkem ' + inflect_number(cnt, 'soubor', 'soubory', 'souborů') + ', ' + data_size(out_size)
temp_file.close()
out_file.close()
#
# Job upload_feedback: Uploaduje opravená řešení
#
# Vstupní JSON:
# { 'round_id': <id>,
# 'only_task_id': <id_or_null>,
# 'only_contest_id': <id_or_null>,
# 'only_site_id': <id_or_null>,
# }
#
# Výstupní JSON:
# null
#
def schedule_upload_feedback(round: db.Round, tmp_file: str, description: str, for_user: db.User,
@@ -97,7 +118,7 @@ def schedule_upload_feedback(round: db.Round, tmp_file: str, description: str, f
'only_region_id': only_region.place_id if only_region is not None else None,
'only_task_id': only_task.task_id if only_task is not None else None,
}
job.in_file = the_job.attach_file(tmp_file, '.zip')
job.in_file = the_job.attach_file(tmp_file, 'upload.zip')
the_job.submit()
@@ -148,19 +169,6 @@ def parse_feedback_name(name: str) -> Optional[UploadFeedback]:
@job_handler(db.JobType.upload_feedback)
def handle_upload_feedback(the_job: TheJob):
"""Uploaduje opravená řešení.
Vstupní JSON:
{ 'round_id': <id>,
'only_task_id': <id_or_null>,
'only_contest_id': <id_or_null>,
'only_site_id': <id_or_null>,
}
Výstupní JSON:
null
"""
job = the_job.job
assert job.in_file is not None
in_json = job.in_json
@@ -280,7 +288,7 @@ def handle_upload_feedback(the_job: TheJob):
return False
cnt_good = 0
parse_zip(job_file_path(job.in_file))
parse_zip(job.file_path(job.in_file))
if not the_job.errors:
resolve_tasks(files)
Loading