Skip to content

Commit

Permalink
[#1251] Update IResourceView interface.
Browse files Browse the repository at this point in the history
Use 'view' instead of 'preview' and update docstrings.
Update code that uses IResourceView plugins
accordingly.
  • Loading branch information
johnglover committed Nov 4, 2013
1 parent f0f771e commit 1db8d70
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 22 deletions.
17 changes: 9 additions & 8 deletions ckan/controllers/package.py
Expand Up @@ -1501,7 +1501,6 @@ def edit_view(self, id, resource_id, view_id=None):
except NotAuthorized:
abort(401, _('Unauthorized to view View %s') % view_id)


view_type = view_type or request.GET.get('view_type')
view_plugin = datapreview.get_view_plugin(view_type)
if not view_plugin:
Expand All @@ -1514,17 +1513,19 @@ def edit_view(self, id, resource_id, view_id=None):
'resource_view': data}

view_plugin.setup_template_variables(context, data_dict)
preview_template = view_plugin.preview_template(context, data_dict)
view_template = view_plugin.view_template(context, data_dict)
form_template = view_plugin.form_template(context, data_dict)

vars = {'form_template': view_plugin.info().get('form_template'),
'preview_template': preview_template,
'data': data, 'errors': errors, 'error_summary': error_summary,
vars = {'form_template': form_template,
'view_template': view_template,
'data': data,
'errors': errors,
'error_summary': error_summary,
'to_preview': to_preview}

if view_id:
return render('package/edit_view.html', extra_vars = vars)
return render('package/new_view.html', extra_vars = vars)

return render('package/edit_view.html', extra_vars=vars)
return render('package/new_view.html', extra_vars=vars)

def resource_datapreview(self, id, resource_id):
'''
Expand Down
11 changes: 5 additions & 6 deletions ckan/lib/datapreview.py
Expand Up @@ -133,11 +133,10 @@ def get_view_plugin(view_type):
if name == view_type:
return plugin

def get_allowed_view_plugins(data_dict):

can_preview = []

def get_allowed_view_plugins(data_dict):
can_view = []
for plugin in p.PluginImplementations(p.IResourceView):
if plugin.can_preview(data_dict):
can_preview.append(plugin)
return can_preview
if plugin.can_view(data_dict):
can_view.append(plugin)
return can_view
4 changes: 3 additions & 1 deletion ckan/lib/helpers.py
Expand Up @@ -1613,6 +1613,7 @@ def resource_preview(resource, package):
resource_url=url,
raw_resource_url=resource.get('url'))


def get_allowed_view_types(resource, package):
data_dict = {'resource': resource, 'package': package}
plugins = datapreview.get_allowed_view_plugins(data_dict)
Expand All @@ -1625,6 +1626,7 @@ def get_allowed_view_types(resource, package):
allowed_view_types.sort(key=lambda item: item[1])
return allowed_view_types


def rendered_resource_view(resource_view, resource, package):
'''
Returns a rendered resource view snippet.
Expand All @@ -1635,7 +1637,7 @@ def rendered_resource_view(resource_view, resource, package):
'resource': resource,
'package': package}
vars = view_plugin.setup_template_variables(context, data_dict) or {}
template = view_plugin.preview_template(context, data_dict)
template = view_plugin.view_template(context, data_dict)
data_dict.update(vars)
return snippet(template, **data_dict)

Expand Down
18 changes: 12 additions & 6 deletions ckan/plugins/interfaces.py
Expand Up @@ -225,24 +225,30 @@ def can_view(self, data_dict):

def setup_template_variables(self, context, data_dict):
'''
Add variables to c just prior to the template being rendered.
The ``data_dict`` contains the resource and the package.
Add variables to the ``data_dict`` that is passed to the
template being rendered.
Should return a new dict instead of updating the input ``data_dict``.
Change the url to a proxied domain if necessary.
The ``data_dict`` contains: ``resource_view``, ``resource`` and
``package``.
'''

def view_template(self, context, data_dict):
'''
Returns a string representing the location of the template to be
rendered for the read page.
The ``data_dict`` contains the resource and the package.
The ``data_dict`` contains: ``resource_view``, ``resource`` and
``package``.
'''

def form_template(self, context, data_dict):
'''
Returns a string representing the location of the template to be
rendered for the read page.
The ``data_dict`` contains the resource and the package.
The ``data_dict`` contains: ``resource_view``, ``resource`` and
``package``.
'''


Expand All @@ -253,7 +259,7 @@ def can_preview(self, data_dict):
return self.can_view(data_dict)

def preview_template(self, context, data_dict):
return self.preview_template(context, data_dict)
return self.view_template(context, data_dict)


class ITagController(Interface):
Expand Down
2 changes: 1 addition & 1 deletion ckan/templates/package/view_edit_base.html
Expand Up @@ -19,7 +19,7 @@
{% block primary_content_inner %}
{% block form %}{% endblock %}
{% if to_preview %}
{% snippet preview_template, view=data, resource=c.resource, package=c.pkg_dict %}
{{ h.rendered_resource_view(data, c.resource, c.pkg_dict) }}
{% endif %}
{% endblock %}

Expand Down

0 comments on commit 1db8d70

Please sign in to comment.