Skip to content
Snippets Groups Projects

Sazba diplomů

Compare and Show latest version
8 files
+ 132
43
Compare changes
  • Side-by-side
  • Inline

Files

+ 23
14
@@ -54,6 +54,9 @@ class CertMaker:
certs: List[Cert]
out_files: Dict[db.CertType, str]
qr_encoder: QREncoder
certs_dir: str
out_dir: str # relativní k certs_dir
job_dir: str # adresář jobu
def __init__(self, the_job: TheJob):
self.the_job = the_job
@@ -80,6 +83,10 @@ class CertMaker:
self.out_files = {}
self.qr_encoder = QREncoder(f'{self.job.dir_path()}/qr')
self.certs_dir = mo.util.data_dir('certs')
self.out_dir = os.path.join(self.round.round_code_short(), str(self.contest.contest_id))
self.job_dir = self.job.dir_path()
def plan(self) -> None:
sess = db.get_session()
pions_pants = (sess.query(db.Participation, db.Participant)
@@ -128,19 +135,24 @@ class CertMaker:
if row is not None and row.get('honorary_mention', False):
add_cert(db.CertType.honorary_mention, 'za úplné vyřešení úlohy', user.sort_key())
def prepare_files(self) -> None:
if self.cset.background_file:
background = os.path.join(self.job_dir, 'background.pdf')
mo.util.unlink_if_exists(background)
os.link(os.path.join(self.certs_dir, self.cset.background_file), background)
def make_certs(self, cert_type: db.CertType) -> None:
certs = [cert for cert in self.certs if cert.type == cert_type]
if not certs:
return
temp_dir = self.job.dir_path()
name = cert_type.file_name()
logger.debug(f'{self.the_job.log_prefix} Vytvářím certifikáty typu {name} v {temp_dir} ({len(certs)} listů)')
logger.debug(f'{self.the_job.log_prefix} Vytvářím certifikáty typu {name} v {self.job_dir} ({len(certs)} listů)')
certs.sort(key=lambda cert: cert.sort_key)
self.make_tex_source(f'{temp_dir}/{name}.tex', certs)
run_tex(temp_dir, f'{name}.tex')
self.out_files[cert_type] = f'{temp_dir}/{name}.pdf'
self.make_tex_source(f'{self.job_dir}/{name}.tex', certs)
run_tex(self.job_dir, f'{name}.tex')
self.out_files[cert_type] = f'{self.job_dir}/{name}.pdf'
def make_tex_source(self, filename: str, certs: List[Cert]) -> None:
with open(filename, 'w') as f:
@@ -159,6 +171,7 @@ class CertMaker:
'signerBtitle': self.cset.signer2_title,
'issueplace': self.cset.issue_place,
'issuedate': self.cset.issue_date,
'background': "background.pdf" if self.cset.background_file else "",
}
if self.round.round_type in (db.RoundType.okresni, db.RoundType.krajske):
ga['oblast'] = self.place.name
@@ -167,7 +180,7 @@ class CertMaker:
for i, cert in enumerate(certs):
qr_url = self._make_qr_url(cert)
qr_file = self.qr_encoder.generate(qr_url)
qr_file = self.qr_encoder.generate(qr_url, border=4)
f.write('\n{\n')
attrs({
'jmeno': cert.user.full_name(),
@@ -199,11 +212,6 @@ class CertMaker:
sess = db.get_session()
conn = sess.connection()
certs_dir = mo.util.data_dir('certs')
out_dir = os.path.join(self.round.round_code_short(), str(self.contest.contest_id))
full_dir = os.path.join(certs_dir, out_dir)
os.makedirs(full_dir, exist_ok=True)
# Nejdříve smažeme už zbytečné soubory s QR kódy
self.qr_encoder.remove_all()
@@ -216,11 +224,11 @@ class CertMaker:
# Založíme nové soubory
for ctype, out_file in self.out_files.items():
file = mo.util.link_to_dir(out_file, full_dir, prefix=f'{ctype.file_name()}-', suffix='.pdf')
pdf_file = mo.util.link_to_dir(out_file, self.out_dir, base_dir=self.certs_dir, prefix=f'{ctype.file_name()}-', suffix='.pdf', make_dirs=True)
sess.add(db.CertFile(
cert_set_id=self.contest_id,
type=ctype,
pdf_file=os.path.join(out_dir, os.path.basename(file)),
pdf_file=pdf_file,
))
# Založíme nové certifikáty
@@ -253,13 +261,14 @@ class CertMaker:
# Nakonec smažeme staré soubory
for old_file in old_files:
mo.util.unlink_if_exists(os.path.join(certs_dir, old_file))
mo.util.unlink_if_exists(os.path.join(self.certs_dir, old_file))
@job_handler(db.JobType.create_certs)
def handle_create_protocols(the_job: TheJob):
cm = CertMaker(the_job)
cm.plan()
cm.prepare_files()
for ctype in db.CertType:
cm.make_certs(ctype)
cm.store_results()
Loading