From 524408dc19ae31bc554fdab8577eab019bb9fc72 Mon Sep 17 00:00:00 2001 From: Ross Jones Date: Thu, 15 Mar 2012 15:51:24 +0000 Subject: [PATCH] [2209] Fix the read method in package controller to allow .format requests as well as revisions with . in the name. Could, however be prettier --- ckan/config/routing.py | 1 - ckan/controllers/package.py | 26 ++++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ckan/config/routing.py b/ckan/config/routing.py index 149c01bd2a7..96cd2071ef3 100644 --- a/ckan/config/routing.py +++ b/ckan/config/routing.py @@ -184,7 +184,6 @@ def make_map(): 'history_ajax', ])) ) - m.connect('/dataset/{id}.{format}', action='read') m.connect('/dataset/{id}', action='read') m.connect('/dataset/{id}/resource/{resource_id}', action='resource_read') diff --git a/ckan/controllers/package.py b/ckan/controllers/package.py index a0bc980adec..1253bfa1afe 100644 --- a/ckan/controllers/package.py +++ b/ckan/controllers/package.py @@ -158,20 +158,37 @@ def _content_type_for_format(self, fmt): types = { "html": ("text/html; charset=utf-8", MarkupTemplate), "rdf" : ("application/rdf+xml; charset=utf-8", MarkupTemplate), - "n3" : ("text/plain; charset=utf-8", TextTemplate), } if fmt in types: return types[fmt][0], fmt, types[fmt][1] - return (types["html"][0]), "html", (types["html"][1]) + return None, "html", (types["html"][1]) - def read(self, id, format='html'): + def read(self, id): + # Check if the request was for a different format than html, we have to do + # it this way because if we instead rely on _content_type_for_format failing + # for revisions (with . in the name) then we will have lost the ID by virtue + # of the routing splitting it up. + format = 'html' + if '.' in id: + pos = id.index('.') + format = id[pos+1:] + id = id[:pos] + + ctype,extension,loader = self._content_type_for_format(format) + if not ctype: + # Reconstitute the ID if we don't know what content type to use + ctype = "text/html; charset=utf-8" + id = "%s.%s" % (id, format) + response.headers['Content-Type'] = ctype + package_type = self._get_package_type(id.split('@')[0]) context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'extras_as_string': True, 'for_view': True} data_dict = {'id': id} + # interpret @ or @ suffix split = id.split('@') if len(split) == 2: @@ -221,9 +238,6 @@ def read(self, id, format='html'): redirect(rdf_url, code=303) break - ctype,extension,loader = self._content_type_for_format(format) - response.headers['Content-Type'] = ctype - PackageSaver().render_package(c.pkg_dict, context) return render('package/read.' + extension, loader_class=loader)