Seminar | models | Pridan nahled titulky cisla.
This commit is contained in:
parent
374a97e28a
commit
03696d79ab
2 changed files with 67 additions and 2 deletions
24
seminar/migrations/0080_auto_20200408_2221.py
Normal file
24
seminar/migrations/0080_auto_20200408_2221.py
Normal file
|
@ -0,0 +1,24 @@
|
|||
# Generated by Django 2.2.9 on 2020-04-08 20:21
|
||||
|
||||
from django.db import migrations, models
|
||||
import seminar.models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('seminar', '0079_clanek_resitelsky'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='cislo',
|
||||
name='titulka_nahled',
|
||||
field=models.ImageField(blank=True, help_text='Obrázek titulní strany, generuje se automaticky', null=True, upload_to='', verbose_name='Obrázek titulní strany'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='cislo',
|
||||
name='pdf',
|
||||
field=models.FileField(blank=True, help_text='PDF čísla, které si mohou řešitelé stáhnout', null=True, upload_to=seminar.models.cislo_pdf_filename, verbose_name='pdf'),
|
||||
),
|
||||
]
|
|
@ -1,6 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import os
|
||||
import random
|
||||
import subprocess
|
||||
import pathlib
|
||||
import tempfile
|
||||
|
||||
from django.db import models
|
||||
from django.contrib import auth
|
||||
|
@ -27,6 +30,7 @@ from unidecode import unidecode # Používám pro získání ID odkazu (ještě
|
|||
|
||||
from polymorphic.models import PolymorphicModel
|
||||
|
||||
|
||||
class SeminarModelBase(models.Model):
|
||||
|
||||
class Meta:
|
||||
|
@ -407,7 +411,11 @@ class Rocnik(SeminarModelBase):
|
|||
|
||||
def cislo_pdf_filename(self, filename):
|
||||
rocnik = str(self.rocnik.rocnik)
|
||||
return os.path.join('cislo', 'pdf', rocnik, '{}-{}.pdf'.format(rocnik, self.poradi))
|
||||
return pathlib.Path('cislo', 'pdf', rocnik, '{}-{}.pdf'.format(rocnik, self.poradi))
|
||||
|
||||
def cislo_png_filename(self, filename):
|
||||
rocnik = str(self.rocnik.rocnik)
|
||||
return pathlib.Path('cislo', 'png', rocnik, '{}-{}.png'.format(rocnik, self.poradi))
|
||||
|
||||
@reversion.register(ignore_duplicates=True)
|
||||
class Cislo(SeminarModelBase):
|
||||
|
@ -451,7 +459,10 @@ class Cislo(SeminarModelBase):
|
|||
help_text='Neveřejná poznámka k číslu (plain text)')
|
||||
|
||||
pdf = models.FileField('pdf', upload_to=cislo_pdf_filename, null=True, blank=True,
|
||||
help_text='Pdf čísla, které si mohou řešitelé stáhnout')
|
||||
help_text='PDF čísla, které si mohou řešitelé stáhnout')
|
||||
|
||||
titulka_nahled = models.ImageField('Obrázek titulní strany', upload_to=cislo_png_filename, null=True, blank=True,
|
||||
help_text='Obrázek titulní strany, generuje se automaticky')
|
||||
|
||||
# má OneToOneField s:
|
||||
# CisloNode
|
||||
|
@ -488,6 +499,35 @@ class Cislo(SeminarModelBase):
|
|||
return None
|
||||
return cs[i]
|
||||
|
||||
def vygeneruj_nahled(self):
|
||||
VYSKA = 594
|
||||
sirka = int(VYSKA*210/297)
|
||||
if not self.pdf:
|
||||
return
|
||||
|
||||
|
||||
# Pokud obrázek neexistuje nebo není aktuální, vytvoř jej
|
||||
if not self.titulka_nahled or os.path.getmtime(self.titulka_nahled.path) < os.path.getmtime(self.pdf.path):
|
||||
png_filename = pathlib.Path(tempfile.mkdtemp(), 'nahled.png')
|
||||
|
||||
subprocess.call([
|
||||
"convert",
|
||||
"-density", "300x300",
|
||||
"-geometry", "{}x{}".format(VYSKA, sirka),
|
||||
"-background", "white",
|
||||
"-flatten",
|
||||
"{}[0]".format(self.pdf.path), # titulní strana
|
||||
png_filename
|
||||
])
|
||||
|
||||
with open(png_filename,'rb') as f:
|
||||
self.titulka_nahled.save('',f,True)
|
||||
|
||||
png_filename.unlink()
|
||||
png_filename.parent.rmdir()
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def get(cls, rocnik, cislo):
|
||||
try:
|
||||
|
@ -499,6 +539,7 @@ class Cislo(SeminarModelBase):
|
|||
|
||||
def save(self, *args, **kwargs):
|
||||
super().save(*args, **kwargs)
|
||||
self.vygeneruj_nahled()
|
||||
# *Node.save() aktualizuje název *Nodu.
|
||||
try:
|
||||
self.cislonode.save()
|
||||
|
|
Loading…
Reference in a new issue