Skip to content

Commit

Permalink
[#1011] Fix a 500 to a 404
Browse files Browse the repository at this point in the history
Catch Genshi TemplateNotFound errors when rendering the package read
page. This can happen if an IDatasetForm plugin returns a custom package
read template, e.g. 'read.html', but the user has requested the dataset
in RDF format and the plugin does not provide a corresponding 'read.rdf'
template. (CKAN will take the path to read.html, replace the filename
extension with rdf, try to render this non-existing template file, and
crash.)

Replace this crash with a proper 404.

Add to the IDatasetForm docs, explaining how to provide RDF templates
for datasets.

Fixes #1011.
  • Loading branch information
Sean Hammond committed Jun 19, 2013
1 parent 83b7758 commit 1eb9d94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion ckan/controllers/package.py
Expand Up @@ -5,6 +5,7 @@
from pylons import config
from genshi.template import MarkupTemplate
from genshi.template.text import NewTextTemplate
import genshi.template.loader
from paste.deploy.converters import asbool

import ckan.logic as logic
Expand Down Expand Up @@ -352,7 +353,15 @@ def read(self, id, format='html'):
template = self._read_template(package_type)
template = template[:template.index('.') + 1] + format

return render(template, loader_class=loader)
try:
return render(template, loader_class=loader)
except genshi.template.loader.TemplateNotFound:
msg = _("Viewing {package_type} datasets in {format} format is "
"not supported (template file {file} not found).".format(
package_type=package_type, format=format, file=template))
abort(404, msg)

assert False, "We should never get here"

def history(self, id):
package_type = self._get_package_type(id.split('@')[0])
Expand Down
9 changes: 9 additions & 0 deletions ckan/plugins/interfaces.py
Expand Up @@ -676,6 +676,15 @@ def read_template(self):
The path should be relative to the plugin's templates dir, e.g.
``'package/read.html'``.
If the user requests the dataset in a format other than HTML
(CKAN supports returning datasets in RDF or N3 format by appending .rdf
or .n3 to the dataset read URL, see :doc:`linked-data-and-rdf`) then
CKAN will try to render
a template file with the same path as returned by this function,
but a different filename extension, e.g. ``'package/read.rdf'``.
If your extension doesn't have this RDF version of the template
file, the user will get a 404 error.
:rtype: string
'''
Expand Down

0 comments on commit 1eb9d94

Please sign in to comment.