Skip to content

Extract _scale_url() for overridable scale URL generation#200

Merged
jensens merged 1 commit intomainfrom
feature/scale-url-method
Mar 6, 2026
Merged

Extract _scale_url() for overridable scale URL generation#200
jensens merged 1 commit intomainfrom
feature/scale-url-method

Conversation

@jensens
Copy link
Member

@jensens jensens commented Mar 5, 2026

Summary

  • Extracts hardcoded @@images URL building into an overridable _scale_url(uid, extension, base_url=None) method on both ImageScale and ImageScaling
  • All URL generation in __init__, srcset_attribute(), and srcset() now delegates to _scale_url()
  • Custom image backends (e.g. Thumbor) can override a single method to generate direct URLs instead of @@images redirects

Motivation

Currently, scale URLs are hardcoded inline in multiple places as f"{base_url}/@@images/{uid}.{extension}". Custom scaling backends that want to produce different URLs (e.g. direct CDN/Thumbor URLs) must copy entire methods. This refactor provides a clean override point.

See #199 for the original proposal.

Example usage

class ThumborImageScaling(ImageScaling):
    _scale_view_class = ThumborImageScale

    def _scale_url(self, uid, extension, base_url=None):
        return f"https://thumbor.example.com/{uid}.{extension}"

class ThumborImageScale(ImageScale):
    def _scale_url(self, uid, extension, base_url=None):
        return f"https://thumbor.example.com/{uid}.{extension}"

Test plan

  • 6 new tests in TestScaleUrl covering default behavior, custom base URL, subclass override on both classes, and srcset integration
  • All 126 existing tests pass with 0 failures

Closes #199

Refactor hardcoded @@images URL building into an overridable
_scale_url(uid, extension, base_url=None) method on both ImageScale
and ImageScaling classes. This allows custom image backends (e.g.
Thumbor) to generate direct URLs without copying entire methods.

Refs #199
@mister-roboto
Copy link

@jensens thanks for creating this Pull Request and helping to improve Plone!

TL;DR: Finish pushing changes, pass all other checks, then paste a comment:

@jenkins-plone-org please run jobs

To ensure that these changes do not break other parts of Plone, the Plone test suite matrix needs to pass, but it takes 30-60 min. Other CI checks are usually much faster and the Plone Jenkins resources are limited, so when done pushing changes and all other checks pass either start all Jenkins PR jobs yourself, or simply add the comment above in this PR to start all the jobs automatically.

Happy hacking!

@jensens
Copy link
Member Author

jensens commented Mar 5, 2026

@jenkins-plone-org please run jobs

@jensens jensens requested a review from erral March 5, 2026 12:11
@jensens
Copy link
Member Author

jensens commented Mar 5, 2026

Additional: I first was thinking about using a ZCA utility, but its some overhead to override 3loc in 2 places and IMO not worth the effort.

Copy link
Member

@erral erral left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes total sense to have a method to create such urls

@jensens jensens merged commit 8cff3d2 into main Mar 6, 2026
11 checks passed
@jensens jensens deleted the feature/scale-url-method branch March 6, 2026 10:10
Copy link
Member

@davisagli davisagli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I do wonder if the entire info should be passed in to _scale_url, in case there are other factors needed to build the url (maybe there is some service that puts the dimensions in the url, for example)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make srcset URL building overridable in ImageScaling

4 participants