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

Define custom content type for Page Template (fixes #1212) #1213

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions .meta.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ additional-rules = [
"recursive-include src *.svg",
"recursive-include src *.ttf",
"recursive-include src *.txt",
"recursive-include src *.types",
"recursive-include src *.webmanifest",
"recursive-include src *.woff",
"recursive-include src *.woff2",
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ https://github.com/zopefoundation/Zope/blob/4.x/CHANGES.rst
5.9.1 (unreleased)
------------------

- Define a custom MIME type for Zope Page Templates
instead of relying on platform-dependent MIME type configurations.
(`#1212 <https://github.com/zopefoundation/Zope/issues/1212>`_)

- Clean up and fix installation documentation.

- Officially support Python 3.12.1.
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ recursive-include src *.rst
recursive-include src *.svg
recursive-include src *.ttf
recursive-include src *.txt
recursive-include src *.types
recursive-include src *.webmanifest
recursive-include src *.woff
recursive-include src *.woff2
Expand Down
9 changes: 9 additions & 0 deletions src/Products/PageTemplates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
This wrapper allows the Page Template modules to be segregated in a
separate package.
"""
import os

from zope.contenttype import add_files


# Placeholder for Zope Product data
misc_ = {}
Expand All @@ -29,3 +33,8 @@ def initialize(context):
# Import lazily, and defer initialization to the module
from . import ZopePageTemplate
ZopePageTemplate.initialize(context)

# Add the custom MIME type information for Page Templates
# to the Python mimetypes module so they are recognized correctly
here = os.path.dirname(os.path.abspath(__file__))
add_files([os.path.join(here, 'mime.types')])
1 change: 1 addition & 0 deletions src/Products/PageTemplates/mime.types
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/vnd.zopefoundation.pagetemplate pt zpt
Copy link
Contributor

Choose a reason for hiding this comment

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

In view of https://www.rfc-editor.org/rfc/rfc2046#section-6 we likely should start our subtype with X- (indicating a somewhat unofficial value).
Because a page template is mostly human readable, text might be better than application as top level type.
However, because we only plan to use the type internally (and not for publication), the precise value is not of great importance.

We might also check that the appearance of a ".pt -> MIME type" map in public registries does not pose a problem on a different route: browsers may consult those registries to automatically provide a "Content-Type" header which might prevent our guessing. However, if this really happens, I have no idea how to prevent it (the browser's info may be authoritative and not based on guessing).

4 changes: 3 additions & 1 deletion src/webdav/NullResource.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def _default_PUT_factory(self, name, typ, body):

if ext == '.dtml':
ob = DTMLDocument('', __name__=name)
elif typ in ('text/html', 'text/xml'):
elif typ in ('text/html',
'text/xml',
'application/vnd.zopefoundation.pagetemplate'):
Copy link
Contributor

Choose a reason for hiding this comment

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

Alternatively, we could apply the same logic as for .dtml, e.g. use

elif ext in ('.pt', '.zpt') or typ in (...

Copy link
Member

Choose a reason for hiding this comment

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

Relying on the extension might indeed be better.

Otherwise I wonder what happens when the operating system associates .pt with that other mime type you found. Does the new Zope definition win? Does the OS definition win? Does it matter on the order in which the two definitions get loaded?

ob = ZopePageTemplate(name, body, content_type=typ)
elif typ.startswith('image/'):
ob = Image(name, '', body, content_type=typ)
Expand Down
Loading