Skip to content

Commit

Permalink
2322 Added some simple convenience urls
Browse files Browse the repository at this point in the history
Added changes to routes for cosmetically improved urls with a
redirect for the API call, a new package controller function for
redirecting to the actual resource url if it exists.

The /dataset/id/resource/id/viewer url uses the existing embed url
with an improved default that more closely matches the screen size
although doesn't change the embed code to use % instead of px.
  • Loading branch information
rossjones committed May 24, 2012
1 parent b7e235a commit 25ab9f4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 deletions.
17 changes: 14 additions & 3 deletions ckan/config/routing.py
Expand Up @@ -141,7 +141,12 @@ def make_map():
action='read', url='', conditions=GET)
m.connect('datastore_write', '/api/data/{id}{url:(/.*)?}',
action='write', url='', conditions=PUT_POST_DELETE)

m.connect('datastore_read_shortcut',
'/dataset/{dataset}/resource/{id}/api{url:(/.*)?}',
action='read', url='', conditions=GET)
m.connect('datastore_write_shortcut',
'/dataset/{dataset}/resource/{id}/api{url:(/.*)?}',
action='write', url='', conditions=PUT_POST_DELETE)

map.redirect('/packages', '/dataset')
map.redirect('/packages/{url:.*}', '/dataset/{url}')
Expand Down Expand Up @@ -188,8 +193,14 @@ def make_map():
)
m.connect('/dataset/{id}.{format}', action='read')
m.connect('/dataset/{id}', action='read')
m.connect('/dataset/{id}/resource/{resource_id}', action='resource_read')
m.connect('/dataset/{id}/resource/{resource_id}/embed', action='resource_embedded_dataviewer')
m.connect('/dataset/{id}/resource/{resource_id}',
action='resource_read')
m.connect('/dataset/{id}/resource/{resource_id}/download',
action='resource_download')
m.connect('/dataset/{id}/resource/{resource_id}/embed',
action='resource_embedded_dataviewer')
m.connect('/dataset/{id}/resource/{resource_id}/viewer',
action='resource_embedded_dataviewer', width="960", height="800")

# group
map.redirect('/groups', '/group')
Expand Down
31 changes: 27 additions & 4 deletions ckan/controllers/package.py
Expand Up @@ -767,6 +767,26 @@ def resource_read(self, id, resource_id):
self._setup_follow_button(context)
return render('package/resource_read.html')

def resource_download(self, id, resource_id):
"""
Provides a direct download by redirecting the user to the url stored
against this resource.
"""
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}

try:
rsc = get_action('resource_show')(context, {'id': resource_id})
pkg = get_action('package_show')(context, {'id': id})
except NotFound:
abort(404, _('Resource not found'))
except NotAuthorized:
abort(401, _('Unauthorized to read resource %s') % id)

if not 'url' in rsc:
abort(404, _('No download is available'))
redirect( rsc['url'] )

def followers(self, id=None):
context = {'model': model, 'session': model.Session,
'user': c.user or c.author, 'for_view': True}
Expand All @@ -787,9 +807,12 @@ def followers(self, id=None):

return render('package/followers.html')

def resource_embedded_dataviewer(self, id, resource_id):
def resource_embedded_dataviewer(self, id, resource_id,
width=500, height=500):
"""
Embeded page for a read-only resource dataview.
Embeded page for a read-only resource dataview. Allows
for width and height to be specified as part of the
querystring (as well as accepting them via routes).
"""
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}
Expand All @@ -816,8 +839,8 @@ def resource_embedded_dataviewer(self, id, resource_id):

c.recline_state = json.dumps(recline_state)

c.width = max(int(request.params.get('width', 500)), 100)
c.height = max(int(request.params.get('height', 500)), 100)
c.width = max(int(request.params.get('width', width)), 100)
c.height = max(int(request.params.get('height', height)), 100)
c.embedded = True

return render('package/resource_embedded_dataviewer.html')
Expand Down

0 comments on commit 25ab9f4

Please sign in to comment.