Unverified Commit 2334cbf5 authored by Thom Wiggers's avatar Thom Wiggers 📐
Browse files

Fix thabloid get_absolute_url()

parent c8d2cf89
......@@ -50,10 +50,10 @@ class Thabloid(models.Model):
return [self.page_url(i+1) for i in range(len(pages))]
def get_absolute_url(self):
return reverse('viewer', kwargs={'year': self.year,
'issue': self.issue})
return reverse('thabloid:pages', kwargs={'year': self.year,
'issue': self.issue})
def save(self, *args, **kwargs):
def save(self, *args, nopages=False, wait=False, **kwargs):
super(Thabloid, self).save(*args, **kwargs)
src = self.file.path
......@@ -72,9 +72,18 @@ class Thabloid(models.Model):
shutil.rmtree(os.path.dirname(dst)) # Remove potential remainders
except FileNotFoundError:
pass
# Skip if nopages is supplied
if nopages: # pragma: no cover
return
os.makedirs(os.path.dirname(dst), exist_ok=True)
# TODO reconsider if this resolution / quality is sufficient
subprocess.Popen(['gs', '-o', dst,
# '-g2100x2970', '-dPDFFitPage',
'-g1050x1485', '-dPDFFitPage',
'-sDEVICE=jpeg', '-f', src])
p = subprocess.Popen(['gs', '-o', dst,
# '-g2100x2970', '-dPDFFitPage',
'-g1050x1485', '-dPDFFitPage',
'-sDEVICE=jpeg', '-f', src],
stdout=subprocess.DEVNULL
)
if wait: # pragma: no cover
p.wait()
import shutil
import tempfile
from django.core.files import File
from django.conf import settings
from django.test import TestCase
from .models import Thabloid
class TestThabloid(TestCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._old_media_root = settings.MEDIA_ROOT
settings.MEDIA_ROOT = tempfile.mkdtemp()
cls.thabloid = Thabloid(
year=1998,
issue=1,
file=File(open('thabloid/fixtures/thabloid-1998-1999-1.pdf',
'rb')))
# Only generate pages if we have 'gs'
if shutil.which('gs') is None:
cls.thabloid.save(nopages=True)
else:
# we should wait for gs to be done before we can do cleanup
cls.thabloid.save(wait=True)
@classmethod
def tearDownClass(cls):
"""Clean up remaining Thabloid files"""
shutil.rmtree(settings.MEDIA_ROOT)
settings.MEDIA_ROOT = cls._old_media_root
def test_thaboid_get_absolute_url(self):
self.assertEqual(self.thabloid.get_absolute_url(),
'/thabloid/pages/1998/1/')
def test_page_urls(self):
self.assertEqual(
self.thabloid.cover,
'public/thabloids/pages/thabloid-1998-1999-1/001.jpg')
self.assertEqual(
self.thabloid.page_url(2),
'public/thabloids/pages/thabloid-1998-1999-1/002.jpg')
# check if it's actual zeropadding and not just '00' + i
self.assertEqual(
self.thabloid.page_url(20),
'public/thabloids/pages/thabloid-1998-1999-1/020.jpg')
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment