Skip to content

Commit

Permalink
Fix text wrapping in watermarked images (#2467)
Browse files Browse the repository at this point in the history
Co-authored-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
zackkrida and obulat authored Jun 23, 2023
1 parent ae8267e commit e131815
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion api/api/utils/watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ def _fit_in_width(text, font, max_width):
"""

char_length = font.getlength("x") # x has the closest to average width
max_chars = max_width // char_length
max_chars = int(
max_width // char_length
) # Must be an integer to be used with `wrap` below

text = "\n".join(["\n".join(wrap(line, max_chars)) for line in text.split("\n")])

Expand Down
15 changes: 15 additions & 0 deletions api/test/unit/utils/test_watermark.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,18 @@ def test_sends_UA_header(requests):
assert len(requests.requests) > 0
for r in requests.requests:
assert r.headers == HEADERS


# Previously, wrapped titles would throw a TypeError:
# slice indices must be integers or None or have an __index__ method.
# See: https://github.com/WordPress/openverse/issues/2466
def test_long_title_wraps_correctly(requests):
# Make the title 400 chars long
_MOCK_IMAGE_INFO_LONG_TITLE = dict(_MOCK_IMAGE_INFO)
_MOCK_IMAGE_INFO_LONG_TITLE["title"] = "a" * 400

watermark("http://example.com/", _MOCK_IMAGE_INFO_LONG_TITLE)

assert len(requests.requests) > 0
for r in requests.requests:
assert r.headers == HEADERS

0 comments on commit e131815

Please sign in to comment.