Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
py3o.template
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Florent Aide
py3o.template
Commits
1b9b9ac3262b
Commit
1b9b9ac3262b
authored
6 years ago
by
Tom Gottfried
Browse files
Options
Downloads
Patches
Plain Diff
Add test for keep_ratio without fixed width or height.
parent
4034e1bbc5fa
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!45
Keep aspect ratio of images if width or height is not known
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
py3o/template/tests/templates/py3o_image_keep_ratio.odt
+0
-0
0 additions, 0 deletions
py3o/template/tests/templates/py3o_image_keep_ratio.odt
py3o/template/tests/test_templates.py
+68
-1
68 additions, 1 deletion
py3o/template/tests/test_templates.py
with
68 additions
and
1 deletion
py3o/template/tests/templates/py3o_image_keep_ratio.odt
0 → 100644
+
0
−
0
View file @
1b9b9ac3
File added
This diff is collapsed.
Click to expand it.
py3o/template/tests/test_templates.py
+
68
−
1
View file @
1b9b9ac3
# -*- encoding: utf-8 -*-
# -*- encoding: utf-8 -*-
import
datetime
import
datetime
import
os
import
os
import
re
import
unittest
import
unittest
import
zipfile
import
zipfile
import
traceback
import
traceback
...
@@ -14,6 +15,7 @@
...
@@ -14,6 +15,7 @@
from
io
import
BytesIO
from
io
import
BytesIO
from
genshi.template
import
TemplateError
from
genshi.template
import
TemplateError
from
PIL
import
Image
from
pyjon.utils
import
get_secure_filename
from
pyjon.utils
import
get_secure_filename
from
py3o.template
import
Template
,
TextTemplate
,
TemplateException
from
py3o.template
import
Template
,
TextTemplate
,
TemplateException
...
@@ -17,7 +19,8 @@
...
@@ -17,7 +19,8 @@
from
pyjon.utils
import
get_secure_filename
from
pyjon.utils
import
get_secure_filename
from
py3o.template
import
Template
,
TextTemplate
,
TemplateException
from
py3o.template
import
Template
,
TextTemplate
,
TemplateException
from
py3o.template.main
import
XML_NS
,
get_soft_breaks
,
MANIFEST
from
py3o.template.main
import
(
XML_NS
,
get_image_frames
,
get_soft_breaks
,
MANIFEST
)
if
six
.
PY3
:
if
six
.
PY3
:
# noinspection PyUnresolvedReferences
# noinspection PyUnresolvedReferences
...
@@ -774,6 +777,70 @@
...
@@ -774,6 +777,70 @@
self
.
assertFalse
(
error
)
self
.
assertFalse
(
error
)
def
test_image_keep_ratio
(
self
):
"""
Test keep_ratio parameter with insertion of images
"""
template_name
=
pkg_resources
.
resource_filename
(
'
py3o.template
'
,
'
tests/templates/py3o_image_keep_ratio.odt
'
)
image_name
=
pkg_resources
.
resource_filename
(
'
py3o.template
'
,
'
tests/templates/images/new_logo.png
'
)
image
=
open
(
image_name
,
'
rb
'
).
read
()
pil_img
=
Image
.
open
(
BytesIO
(
image
))
img_ratio
=
pil_img
.
size
[
0
]
/
float
(
pil_img
.
size
[
1
])
outname
=
get_secure_filename
()
template
=
Template
(
template_name
,
outname
)
nmspc
=
template
.
namespaces
image_frames
=
{
elem
.
get
(
'
{%s}name
'
%
nmspc
[
'
draw
'
]):
elem
for
elem
in
get_image_frames
(
template
.
content_trees
[
0
],
nmspc
)
}
data_dict
=
{
'
image
'
:
base64
.
b64encode
(
image
)
}
template
.
render
(
data_dict
)
outodt
=
zipfile
.
ZipFile
(
outname
,
'
r
'
)
image_entries
=
get_image_frames
(
lxml
.
etree
.
parse
(
BytesIO
(
outodt
.
read
(
template
.
templated_files
[
0
]))),
nmspc
)
for
frame
in
image_entries
:
name
=
frame
.
get
(
'
{%s}name
'
%
nmspc
[
'
draw
'
])
tframe
=
image_frames
[
name
]
if
'
keep_ratio=False
'
in
name
:
# result dimension should equal template
for
dim
in
[
'
width
'
,
'
height
'
]:
self
.
assertEqual
(
frame
.
get
(
'
{%s}%s
'
%
(
nmspc
[
'
svg
'
],
dim
)),
tframe
.
get
(
'
{%s}%s
'
%
(
nmspc
[
'
svg
'
],
dim
)),
)
else
:
# Compare frame aspect ratio with that of image and check that
# frame dimensions do not exceed those of the placeholders
height
=
float
(
re
.
sub
(
'
[a-zA-Z]+
'
,
''
,
frame
.
get
(
'
{%s}height
'
%
nmspc
[
'
svg
'
])))
width
=
float
(
re
.
sub
(
'
[a-zA-Z]+
'
,
''
,
frame
.
get
(
'
{%s}width
'
%
nmspc
[
'
svg
'
])))
frame_ratio
=
width
/
height
self
.
assertAlmostEqual
(
frame_ratio
,
img_ratio
)
theight
=
float
(
re
.
sub
(
'
[a-zA-Z]+
'
,
''
,
tframe
.
get
(
'
{%s}height
'
%
nmspc
[
'
svg
'
])))
twidth
=
float
(
re
.
sub
(
'
[a-zA-Z]+
'
,
''
,
tframe
.
get
(
'
{%s}width
'
%
nmspc
[
'
svg
'
])))
self
.
assertLessEqual
(
height
,
theight
)
self
.
assertLessEqual
(
width
,
twidth
)
def
test_text_template
(
self
):
def
test_text_template
(
self
):
template_name
=
pkg_resources
.
resource_filename
(
template_name
=
pkg_resources
.
resource_filename
(
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment