Korektury: oprav problém s konfliktem jmen png
Jméno png záviselo jen na čase nahrání pdf s přesností na minuty a části jména pdf do první tečky. Nyní na čase a celém jméně nahrávaného pdf. Z KorekturovanePDF.save() vyčleněn convert().
This commit is contained in:
parent
5bb3a908bf
commit
9572515cea
3 changed files with 33 additions and 18 deletions
|
@ -69,6 +69,34 @@ class KorekturovanePDF(models.Model):
|
||||||
|
|
||||||
#TODO Nepovinný foreign key k číslu
|
#TODO Nepovinný foreign key k číslu
|
||||||
|
|
||||||
|
def get_prefix(self):
|
||||||
|
"""Vrať řetězec, ke kterému se připojí číslo a .png"""
|
||||||
|
# vrátíme jméno souboru bez cesty
|
||||||
|
return os.path.basename(self.pdf.file.name)
|
||||||
|
|
||||||
|
def convert(self):
|
||||||
|
"""Vytvoří jedno png za každou stranu pdf a uloží se"""
|
||||||
|
dirname = os.path.join(settings.MEDIA_ROOT, settings.KOREKTURY_IMG_DIR)
|
||||||
|
if not os.path.exists(dirname):
|
||||||
|
os.mkdir(dirname)
|
||||||
|
self.stran = 0
|
||||||
|
while True:
|
||||||
|
res = subprocess.call([
|
||||||
|
"convert",
|
||||||
|
"-density", "180x180",
|
||||||
|
"-geometry", " 1024x1448",
|
||||||
|
"%s[%d]" % (self.pdf.path, self.stran),
|
||||||
|
os.path.join(
|
||||||
|
dirname,
|
||||||
|
"%s-%d.png" % (self.get_prefix(), self.stran)
|
||||||
|
)
|
||||||
|
])
|
||||||
|
if res == 1:
|
||||||
|
break
|
||||||
|
self.stran += 1
|
||||||
|
# Změnil se počet stran, ukládáme
|
||||||
|
super(KorekturovanePDF, self).save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
# Pokud se nezmenilo PDF, tak nepregenerovavej nahledy
|
# Pokud se nezmenilo PDF, tak nepregenerovavej nahledy
|
||||||
try:
|
try:
|
||||||
|
@ -78,24 +106,11 @@ class KorekturovanePDF(models.Model):
|
||||||
return
|
return
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
pass
|
pass
|
||||||
super(KorekturovanePDF, self).save()
|
# uložíme nahrávané pdf
|
||||||
filename = os.path.split(self.pdf.file.name)[1].split(".")[0]
|
|
||||||
dirname = os.path.join(settings.MEDIA_ROOT, settings.KOREKTURY_IMG_DIR)
|
|
||||||
if not os.path.exists(dirname):
|
|
||||||
os.mkdir(dirname)
|
|
||||||
while True:
|
|
||||||
res = subprocess.call([
|
|
||||||
"convert",
|
|
||||||
"-density", "180x180",
|
|
||||||
"-geometry", " 1024x1448",
|
|
||||||
"%s[%d]" % (self.pdf.path, self.stran),
|
|
||||||
os.path.join(dirname, "%s-%d.png" % (filename, self.stran))
|
|
||||||
])
|
|
||||||
if res==1:
|
|
||||||
break
|
|
||||||
self.stran +=1
|
|
||||||
super(KorekturovanePDF, self).save()
|
super(KorekturovanePDF, self).save()
|
||||||
|
|
||||||
|
# uložíme png a změněný počet stran
|
||||||
|
self.convert()
|
||||||
|
|
||||||
|
|
||||||
@reversion.register(ignore_duplicate_revision=True)
|
@reversion.register(ignore_duplicate_revision=True)
|
||||||
|
|
|
@ -47,7 +47,7 @@
|
||||||
<div class='imgdiv'>
|
<div class='imgdiv'>
|
||||||
<img width='1021' height='1448'
|
<img width='1021' height='1448'
|
||||||
onclick='img_click(this,event)' id='img-{{i}}'
|
onclick='img_click(this,event)' id='img-{{i}}'
|
||||||
src='/media/korektury/img/{{img_name}}-{{i}}.png'/>
|
src='/media/korektury/img/{{img_prefix}}-{{i}}.png'/>
|
||||||
</div>
|
</div>
|
||||||
<hr/>
|
<hr/>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -167,7 +167,7 @@ class KorekturyView(generic.TemplateView):
|
||||||
context = super(KorekturyView,self).get_context_data(**kwargs)
|
context = super(KorekturyView,self).get_context_data(**kwargs)
|
||||||
pdf = get_object_or_404(KorekturovanePDF, id=self.kwargs['pdf'])
|
pdf = get_object_or_404(KorekturovanePDF, id=self.kwargs['pdf'])
|
||||||
context['pdf'] = pdf
|
context['pdf'] = pdf
|
||||||
context['img_name'] = os.path.split(pdf.pdf.path)[1].split('.')[0]
|
context['img_prefix'] = pdf.get_prefix()
|
||||||
context['img_path'] = settings.KOREKTURY_IMG_DIR
|
context['img_path'] = settings.KOREKTURY_IMG_DIR
|
||||||
context['img_indexes'] = range(pdf.stran)
|
context['img_indexes'] = range(pdf.stran)
|
||||||
context['form_oprava'] = OpravaForm()
|
context['form_oprava'] = OpravaForm()
|
||||||
|
|
Loading…
Reference in a new issue