Skip to content

Commit

Permalink
Merge branch 'dataviewer-iframe'
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Sep 28, 2012
2 parents 0e9ff2d + 8b0975d commit 5173eca
Show file tree
Hide file tree
Showing 78 changed files with 58,366 additions and 3,287 deletions.
14 changes: 6 additions & 8 deletions ckan/config/environment.py
Expand Up @@ -126,7 +126,7 @@ def find_controller(self, controller):
paths = dict(root=root,
controllers=os.path.join(root, 'controllers'),
static_files=os.path.join(root, 'public'),
templates=[os.path.join(root, 'templates')])
templates=[])

# Initialize config with the basic options

Expand Down Expand Up @@ -189,15 +189,13 @@ def find_controller(self, controller):
## redo template setup to use genshi.search_path
## (so remove std template setup)
legacy_templates_path = os.path.join(root, 'templates_legacy')
jinja2_templates_path = os.path.join(root, 'templates')
if asbool(config.get('ckan.legacy_templates', 'no')):
template_paths = [legacy_templates_path]
# if we are testing allow new templates
if asbool(config.get('ckan.enable_testing', 'false')):
jinja2_templates_path = os.path.join(root, 'templates')
template_paths.append(jinja2_templates_path)
# We want the new template path for extra snippets like the
# dataviewer and also for some testing stuff
template_paths = [legacy_templates_path, jinja2_templates_path]
else:
template_paths = [paths['templates'][0]]
template_paths.append(legacy_templates_path)
template_paths = [jinja2_templates_path, legacy_templates_path]

extra_template_paths = config.get('extra_template_paths', '')
if extra_template_paths:
Expand Down
2 changes: 2 additions & 0 deletions ckan/config/routing.py
Expand Up @@ -213,6 +213,8 @@ def make_map():
action='resource_embedded_dataviewer')
m.connect('/dataset/{id}/resource/{resource_id}/viewer',
action='resource_embedded_dataviewer', width="960", height="800")
m.connect('/dataset/{id}/resource/{resource_id}/preview/{preview_type}',
action='resource_datapreview')

# group
map.redirect('/groups', '/group')
Expand Down
20 changes: 18 additions & 2 deletions ckan/controllers/package.py
Expand Up @@ -310,7 +310,6 @@ def read(self, id, format='html'):
try:
c.pkg_dict = get_action('package_show')(context, data_dict)
c.pkg = context['package']
c.resources_json = json.dumps(c.pkg_dict.get('resources', []))
except NotFound:
abort(404, _('Dataset not found'))
except NotAuthorized:
Expand Down Expand Up @@ -1146,7 +1145,6 @@ def resource_read(self, id, resource_id):
c.package = get_action('package_show')(context, {'id': id})
# required for nav menu
c.pkg = context['package']
c.resource_json = json.dumps(c.resource)
c.pkg_dict = c.package
except NotFound:
abort(404, _('Resource not found'))
Expand Down Expand Up @@ -1280,3 +1278,21 @@ def _parse_recline_state(self, params):
not k.endswith(recline_state['currentView']):
recline_state.pop(k)
return recline_state

def resource_datapreview(self, id, resource_id, preview_type):
'''
Embeded page for a resource data-preview.
'''
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}

try:
c.resource = get_action('resource_show')(context,
{'id': resource_id})
c.package = get_action('package_show')(context, {'id': id})
c.resource_json = json.dumps(c.resource)
except NotFound:
abort(404, _('Resource not found'))
except NotAuthorized:
abort(401, _('Unauthorized to read resource %s') % id)
return render('dataviewer/{type}.html'.format(type=preview_type))
4 changes: 2 additions & 2 deletions ckan/lib/fanstatic_resources.py
Expand Up @@ -248,10 +248,10 @@ def create_resource(path, lib_name, count, inline=False):

create_library('vendor', os.path.join(base_path, 'vendor'), depend_base=False)

create_library('datapreview', os.path.join(base_path, 'datapreview'),
create_library('base', os.path.join(base_path, 'javascript'),
depend_base=False)

create_library('base', os.path.join(base_path, 'javascript'),
create_library('datapreview', os.path.join(base_path, 'datapreview'),
depend_base=False)

create_library('css', os.path.join(base_path, 'css'), depend_base=False)
78 changes: 78 additions & 0 deletions ckan/lib/helpers.py
Expand Up @@ -1158,6 +1158,35 @@ def include_resource(resource):
r = getattr(fanstatic_resources, resource)
r.need()

def urls_for_resource(resource):
''' Returns a list of urls for the resource specified. If the resource
is a group or has dependencies then there can be multiple urls.
NOTE: This is for special situations only and is not the way to generaly
include resources. It is advised not to use this function.'''
r = getattr(fanstatic_resources, resource)
resources = list(r.resources)
core = fanstatic_resources.fanstatic_extensions.core
f = core.get_needed()
lib = resources[0].library
root_path = f.library_url(lib)

resources = core.sort_resources(resources)
if f._bundle:
resources = core.bundle_resources(resources)
out = []
for resource in resources:
if isinstance(resource, core.Bundle):
paths = [resource.relpath for resource in resource.resources()]
relpath = ';'.join(paths)
relpath = core.BUNDLE_PREFIX + relpath
else:
relpath = resource.relpath

out.append('%s/%s' % (root_path, relpath))
return out


def debug_inspect(arg):
''' Output pprint.pformat view of supplied arg '''
return literal('<pre>') + pprint.pformat(arg) + literal('</pre>')
Expand Down Expand Up @@ -1304,6 +1333,53 @@ def format_resource_items(items):
return sorted(output, key=lambda x:x[0])


def resource_preview(resource, pkg_id):
'''
Returns a rendered snippet for a embeded resource preview.
Depending on the type, different previews are loaded.
This could be an img tag where the image is loaded directly or an iframe that
embeds a webpage, recline or a pdf preview.
'''

DIRECT_EMBEDS = ['png', 'jpg', 'gif']
LOADABLE = ['html', 'htm', 'rdf+xml', 'owl+xml', 'xml', 'n3',
'n-triples', 'turtle', 'plain', 'atom', 'tsv', 'rss',
'txt', 'json']
PDF = ['pdf', 'x-pdf', 'acrobat', 'vnd.pdf']

format_lower = resource['format'].lower()
directly = False
url = ''

if resource.get('datastore_active') or format_lower in ['csv', 'xls', 'tsv']:
url = url_for(controller='package', action='resource_datapreview',
resource_id=resource['id'], preview_type='recline', id=pkg_id, qualified=True)
elif format_lower in PDF:
url = url_for(controller='package', action='resource_datapreview',
resource_id=resource['id'], preview_type='pdf', id=pkg_id, qualified=True)
elif format_lower == 'jsonp':
url = url_for(controller='package', action='resource_datapreview',
resource_id=resource['id'], preview_type='json', id=pkg_id, qualified=True)
elif format_lower in LOADABLE:
url = resource['url']
elif format_lower in DIRECT_EMBEDS:
directly = True
url = resource['url']
else:
log.info('no handler for {}'.format(resource['format']))
return snippet(
"dataviewer/snippets/no_preview.html",
resource_type=format_lower
)

return snippet(
"dataviewer/snippets/data_preview.html",
embed=directly,
resource_url=url
)


# these are the functions that will end up in `h` template helpers
# if config option restrict_template_vars is true
__allowed_functions__ = [
Expand Down Expand Up @@ -1358,6 +1434,7 @@ def format_resource_items(items):
'get_facet_items_dict',
'unselected_facet_items',
'include_resource',
'urls_for_resource',
'build_nav_main',
'debug_inspect',
'dict_list_reduce',
Expand All @@ -1378,6 +1455,7 @@ def format_resource_items(items):
'get_request_param',
'render_markdown',
'format_resource_items',
'resource_preview',
# imported into ckan.lib.helpers
'literal',
'link_to',
Expand Down
33 changes: 33 additions & 0 deletions ckan/public/base/datapreview/css/json.css
@@ -0,0 +1,33 @@
body {
width: 500px;
}

pre {
font-size: 13px;
}

.loading {
font-weight: bold;
font-family: sans-serif;
font-size: 16px;
position: fixed;
left: -20px;
top: 20px;
}

.string {
color: #009900;
}
.number {
color: #0066FF;
}
.boolean {
color: #E62E00;
}
.null {
color: #E62E00;
}
.key {
color: #222;
font-weight: bold;
}
4 changes: 4 additions & 0 deletions ckan/public/base/datapreview/css/pdf.css
@@ -0,0 +1,4 @@
body {
height: 500px;
overflow: hidden;
}
@@ -1,3 +1,7 @@
body {
background-color: #fff;
}

.recline-query-editor form, .recline-query-editor .text-query {
height: 28px;
}
Expand Down Expand Up @@ -36,7 +40,6 @@
border-radius: 0 3px 3px 0;
}


.recline-slickgrid {
height: 550px;
}
Expand All @@ -51,8 +54,14 @@
vertical-align: top;
}

#ckanext-datapreview iframe {
min-height: 480px;
border: 1px solid #ccc;
padding: 7px;
.loading-dialog {
width: 120px;

}

.loading-spinner {
float: right;
background-image: url('../img/ajaxload-circle.gif');
width: 32px;
height: 32px;
}

0 comments on commit 5173eca

Please sign in to comment.