Skip to main content

Hook into repoze.zope2 that allows third party packages to register a sequence of hooks that will be allowed to modify the response before it is returned to the browser

Project description

Introduction

plone.transformchain provides methods to modify the response from a page published with repoze.zope2 or the “classic” ZPublisher before it is returned to the browser.

Register a uniquely named adapter from (published, request) providing the ITransform interface. published is the published object, e.g. a view; request is the current request.

The order of the transforms can be maintained using the order property of the adapter.

One of three methods will be called, depending on what type of input was obtained from the publisher and/or the previous method.

  • transformBytes() is called if the input is a str (bytes) object

  • transformUnicode() is called if the input is a unicode object

  • transformIterable() is called if the input is another type of iterable

Each stage can return a byte string, a unicode string, or an iterable.

Most transformers will have a “natural” representation of the result, and will implement the respective method to return another value of the same representation, e.g. implement transformUnicode() to transform and return a unicode object. The other methods may then either be implemented to return None (do nothing) or convert the value to the appropriate type.

The first transformer in the chain is likely to get:

  • A byte string if the transformer is running under the standard Zope 2 ZPublisher.

  • An iterable if the transformer is running under repoze.zope2 or another WSGI pipeline.

Check self.request.response.getHeader('content-type') to see the type of result. The iterable, when unwound, will conform to this type, e.g. for “text/html”, ''.join(result) should be an HTML string.

The return value is passed to the next transform in the chain. The final transform should return a unicode string, an encoded string, or an iterable.

If a byte string or unicode string is returned by the last transform in the chain, the Content-Length header will be automatically updated.

Return None to signal that the result should not be changed from the previous transform.

Here is an example that uppercases everything:

from zope.component import adapter
from zope.interface import implementer
from zope.interface import Interface
from plone.transformchain.interfaces import ITransform

@implementer(ITransform)
@adapter(Interface, Interface) # any context, any request
class UpperTransform(object):

    order = 1000

    def __init__(self, published, request):
        self.published = published
        self.request = request

    def transformBytes(self, result, encoding):
        return result.upper()

    def transformUnicode(self, result, encoding):
        return result.upper()

    def transformIterable(self, result, encoding):
        return [s.upper() for s in result]

You could register this in ZCML like so:

<adapter factory=".transforms.UpperTransform" name="example.uppertransform" />

If you need to turn off transformations for a particular request, you can set a key in request.environ:

request.environ['plone.transformchain.disable'] = True

This will leave the response untouched and will not invoke any ITransform adapters at all.

Changelog

4.0.0 (2026-05-18)

Internal:

  • Update configuration files. [plone devs]

4.0.0a1 (2025-11-19)

Breaking changes:

  • Replace pkg_resources namespace with PEP 420 native namespace. Support only Plone 6.2 and Python 3.10+. (#3928)

3.0.2 (2025-09-10)

Internal:

  • Move distribution to src layout [gforcada] (#4217)

3.0.1 (2024-01-22)

Internal:

  • Update configuration files. [plone devs] (6e36bcc4, 7723aeaf)

3.0.0 (2023-04-27)

Breaking changes:

  • Drop Python 2 and Plone 5.2 compatibility. [gforcada] (#6)

Internal:

  • Update configuration files. [plone devs] (3333c742)

2.0.2 (2020-04-22)

Bug fixes:

  • Minor packaging updates. (#1)

2.0.1 (2018-11-04)

Bug fixes:

  • More py3 test and functionality fixes. [pbauer, thet]

2.0.0 (2018-06-20)

Breaking changes:

  • Drop support for Python 2.6. [jensens]

New features:

  • Make ZServer optional

Bug fixes:

  • More fixes for Python 2 / 3 compatibility. [pbauer, thet]

1.2.2 (2018-02-11)

Bug fixes:

  • Add Python 2 / 3 compatibility [vincero]

1.2.1 (2017-06-28)

Bug fixes:

  • Remove unittest2 dependency [kakshay21]

1.2.0 (2016-06-21)

New:

  • Added events to notify before/after all/single transform(s) are executed. [jensens]

1.1.0 (2016-02-21)

New:

  • Require Zope2 >= 2.13.23 [jensens]

Fixes:

  • PEP8 et al. use zca decorators, … [jensens]

1.0.4 (2015-05-11)

  • Minor cleanup: whitespace, git ignores, rst. [gforcada, rnix, maurits]

1.0.3 (2013-01-13)

  • There was a problem with the charset regular expression, it expected one space, and only one, between mimetype and charset. So a valid values like “text/html;charset=utf-8” didn’t match and default_encoding was returned. We fixed it by allowing any number of spaces (including zero). [jpgimenez]

1.0.2 (2012-01-26)

  • Fix packaging error. [esteele]

1.0.1 (2012-01-26)

  • Compute error_status and store it on request. Work around bug with Zope 2.13 publication events : response.status is not set when IPubBeforeAbort is notified. [gotcha]

  • Don’t transform FTP requests [rochecompaan]

1.0 - 2011-05-13

  • Release 1.0 Final. [esteele]

1.0b1 - 2010-04-21

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

plone_transformchain-4.0.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plone_transformchain-4.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file plone_transformchain-4.0.0.tar.gz.

File metadata

  • Download URL: plone_transformchain-4.0.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for plone_transformchain-4.0.0.tar.gz
Algorithm Hash digest
SHA256 0e89c256047615c96dca0282b933fbd39fec67535525ed557ecb136b6c5a5aa3
MD5 fb30949e7c90e52ea60552c97932ebb5
BLAKE2b-256 b0ebbd5f1d015a915b9bfb037b8b374328684c4c683f2291ee80042cf555e345

See more details on using hashes here.

File details

Details for the file plone_transformchain-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for plone_transformchain-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20feff9e5ca0eb4651d98f1076f370acecc5815bb5778eec23bcf54cfe950284
MD5 13ed94a211b980808822408f30412f3d
BLAKE2b-256 c6b5789e1b3df1bb72fd54669032962d2ce415a12dfc87749832acd637471d59

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page