Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
thalia
concrexit
Commits
e8b71a97
Commit
e8b71a97
authored
Feb 27, 2017
by
Thom Wiggers
📐
Browse files
Merge branch '114-thabloid-save' into 'master'
Improved Thabloid save function Closes
#114
See merge request
!350
parents
7dcddb24
53f1b45e
Changes
3
Hide whitespace changes
Inline
Side-by-side
.gitlab-ci.yml
View file @
e8b71a97
...
...
@@ -20,6 +20,8 @@ pep8:
-
mkdir -p pip-cache
-
git log -1
-
pip install tox coverage
-
apt-get update
-
apt-get install -y ghostscript
# required for Thabloid's tests
script
:
-
tox -e $PYTHON_VERSION
-
cd website
...
...
website/thabloid/models.py
View file @
e8b71a97
...
...
@@ -54,30 +54,17 @@ class Thabloid(models.Model):
return
reverse
(
'thabloid:pages'
,
kwargs
=
{
'year'
:
self
.
year
,
'issue'
:
self
.
issue
})
def
save
(
self
,
*
args
,
nopages
=
False
,
wait
=
False
,
**
kwargs
):
super
(
Thabloid
,
self
).
save
(
*
args
,
**
kwargs
)
src
=
self
.
file
.
path
# For overwriting already existing files
new_name
=
thabloid_filename
(
self
,
self
.
file
.
name
)
new_src
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
new_name
)
if
src
!=
new_src
:
os
.
rename
(
src
,
new_src
)
src
=
new_src
self
.
file
.
name
=
new_name
self
.
save
()
def
extract_thabloid_pages
(
self
,
wait
):
dst
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
self
.
page_url
())
name
=
thabloid_filename
(
self
,
self
.
file
.
name
)
src
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
name
)
try
:
shutil
.
rmtree
(
os
.
path
.
dirname
(
dst
))
# Remove potential remainders
# Remove potential remainders
shutil
.
rmtree
(
os
.
path
.
dirname
(
dst
))
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
p
=
subprocess
.
Popen
([
'gs'
,
'-o'
,
dst
,
...
...
@@ -88,3 +75,45 @@ class Thabloid(models.Model):
)
if
wait
:
# pragma: no cover
p
.
wait
()
def
save
(
self
,
*
args
,
wait
=
False
,
**
kwargs
):
new_file
=
False
if
self
.
pk
is
None
:
new_file
=
True
else
:
old
=
Thabloid
.
objects
.
get
(
pk
=
self
.
pk
)
old_dir
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
old
.
page_url
())
old_dir
=
os
.
path
.
dirname
(
old_dir
)
if
self
.
file
!=
old
.
file
:
new_file
=
True
elif
self
.
year
!=
old
.
year
or
self
.
issue
!=
old
.
issue
:
self
.
file
.
name
=
thabloid_filename
(
self
,
self
.
file
.
name
)
new_dir
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
self
.
page_url
())
new_dir
=
os
.
path
.
dirname
(
new_dir
)
try
:
shutil
.
rmtree
(
new_dir
)
except
FileNotFoundError
:
pass
os
.
rename
(
old_dir
,
new_dir
)
os
.
rename
(
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
old
.
file
.
name
),
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
self
.
file
.
name
))
if
new_file
:
filename
=
thabloid_filename
(
self
,
self
.
file
.
name
)
src
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
filename
)
# Removes the .pdf file if it already exists
try
:
os
.
remove
(
src
)
except
FileNotFoundError
:
pass
super
(
Thabloid
,
self
).
save
(
*
args
,
**
kwargs
)
if
new_file
:
self
.
extract_thabloid_pages
(
wait
)
website/thabloid/tests.py
View file @
e8b71a97
import
shutil
import
tempfile
import
os.path
from
django.core.files
import
File
from
django.conf
import
settings
from
django.test
import
TestCase
from
django.test.utils
import
override_settings
from
.models
import
Thabloid
tmp_MEDIA_ROOT
=
tempfile
.
mkdtemp
()
@
override_settings
(
MEDIA_ROOT
=
tmp_MEDIA_ROOT
)
class
TestThabloid
(
TestCase
):
@
classmethod
def
setUpClass
(
cls
):
super
().
setUpClass
()
cls
.
_old_media_root
=
settings
.
MEDIA_ROOT
settings
.
MEDIA_ROOT
=
tempfile
.
mkdtemp
()
def
setUp
(
self
):
with
open
(
'thabloid/fixtures/thabloid-1998-1999-1.pdf'
,
'rb'
)
as
f
:
cls
.
thabloid
=
Thabloid
(
self
.
thabloid
=
Thabloid
(
year
=
1998
,
issue
=
1
,
file
=
File
(
f
))
# 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
):
# we should wait for gs to be done before we can do cleanup
self
.
thabloid
.
save
(
wait
=
True
)
def
tearDown
(
self
):
"""Clean up remaining Thabloid files"""
shutil
.
rmtree
(
settings
.
MEDIA_ROOT
)
settings
.
MEDIA_ROOT
=
cls
.
_old_media_root
super
().
tearDownClass
()
def
test_thaboid_get_absolute_url
(
self
):
self
.
assertEqual
(
self
.
thabloid
.
get_absolute_url
(),
...
...
@@ -49,3 +43,52 @@ class TestThabloid(TestCase):
self
.
assertEqual
(
self
.
thabloid
.
page_url
(
20
),
'public/thabloids/pages/thabloid-1998-1999-1/020.jpg'
)
@
staticmethod
def
_pdf_exist
(
pdf
):
pdf
=
pdf
.
lstrip
(
settings
.
MEDIA_URL
)
return
os
.
path
.
isfile
(
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
pdf
))
@
staticmethod
def
_jpgs_exist
(
pages
,
inverse
=
False
):
jpgs
=
[
url
.
lstrip
(
settings
.
MEDIA_URL
)
for
url
in
pages
]
for
jpg
in
jpgs
:
jpgpath
=
os
.
path
.
join
(
settings
.
MEDIA_ROOT
,
jpg
)
if
((
not
inverse
and
not
os
.
path
.
isfile
(
jpgpath
))
or
(
inverse
and
os
.
path
.
isfile
(
jpgpath
))):
return
False
return
True
def
test_pdf_existence
(
self
):
TestThabloid
.
_pdf_exist
(
self
.
thabloid
.
file
.
url
)
def
test_jpg_existence
(
self
):
TestThabloid
.
_jpgs_exist
(
self
.
thabloid
.
pages
)
def
test_change_year
(
self
):
self
.
thabloid
.
year
+=
1
self
.
thabloid
.
save
()
self
.
assertTrue
(
TestThabloid
.
_pdf_exist
(
self
.
thabloid
.
file
.
url
))
self
.
assertTrue
(
TestThabloid
.
_jpgs_exist
(
self
.
thabloid
.
pages
))
def
test_change_year_cleanup
(
self
):
oldpages
=
self
.
thabloid
.
pages
oldurl
=
self
.
thabloid
.
file
.
url
self
.
thabloid
.
year
+=
1
self
.
thabloid
.
save
()
self
.
assertFalse
(
TestThabloid
.
_pdf_exist
(
oldurl
))
self
.
assertTrue
(
TestThabloid
.
_jpgs_exist
(
oldpages
,
inverse
=
True
))
def
test_change_issue
(
self
):
self
.
thabloid
.
issue
+=
1
self
.
thabloid
.
save
()
self
.
assertTrue
(
TestThabloid
.
_pdf_exist
(
self
.
thabloid
.
file
.
url
))
self
.
assertTrue
(
TestThabloid
.
_jpgs_exist
(
self
.
thabloid
.
pages
))
def
test_change_issue_cleanup
(
self
):
oldpages
=
self
.
thabloid
.
pages
oldurl
=
self
.
thabloid
.
file
.
url
self
.
thabloid
.
issue
+=
1
self
.
thabloid
.
save
()
self
.
assertFalse
(
TestThabloid
.
_pdf_exist
(
oldurl
))
self
.
assertTrue
(
TestThabloid
.
_jpgs_exist
(
oldpages
,
inverse
=
True
))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment