Skip to content

Commit

Permalink
[2209] Fix the read method in package controller to allow .format req…
Browse files Browse the repository at this point in the history
…uests as well as revisions with . in the name. Could, however be prettier
  • Loading branch information
rossjones committed Mar 15, 2012
1 parent 75d25a5 commit 524408d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
1 change: 0 additions & 1 deletion ckan/config/routing.py
Expand Up @@ -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')

Expand Down
26 changes: 20 additions & 6 deletions ckan/controllers/package.py
Expand Up @@ -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 @<revision_id> or @<date> suffix
split = id.split('@')
if len(split) == 2:
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 524408d

Please sign in to comment.