Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix text wrapping in watermarked images #2467

Merged
merged 3 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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