ckan/ckan — pull request #1852 by amercader (2014-11-04 17:25:28+00:00)
Title: Better handling of default resource views

Parent #1851

In order to create a default view object for certain formats (eg an `img` tag for an image, a grid for a CSV, ...) right now each view plugin implements `IPackageController` to listen to create and edits and create a view of its own type if the formats are correct.

This is not optimal as each view needs to replicate this logic. A different approach could be that the views are left to decide whether if they are suitable against a particular condition (generally a certain format), but the logic to decide if they need to be created or not is left to a system plugin that takes care of that depending on the values of a config option. This config option (`ckan.views.default_views` or similar) will contain the names of the view plugins that users want to get created by default (of course it will have a default as well).

Views that don't depend on the datastore, will get created on the `after_create` and `after_update` methods of `IPackageController`, similarly to how the view plugins [do it now](https://github.com/ckan/ckan/blob/1251-resource-view/ckanext/textpreview/plugin.py#L88:L111).

For those that depend on the datastore being ready, we need a new `IDatapusher` interface that allows to hook into [this particular](https://github.com/ckan/ckan/blob/1251-resource-view/ckanext/datapusher/logic/action.py#L157:L167) point in the code, and allows other views to know when the uploading to the datastore was finished (not just the hardcoded `text_grid`). Of course that depends on the datapusher being used, if people POSTs stuff directly to the datastore it won't work.

This will also help make the paster commands more generic and work for custom view types, as right now they have the view types to create hardcoded.

Quick and dirty pseudo-code for the system plugin that creates default views (we might need to discuss the best way of handling datastore/not datastore based):

```
class ViewsCreator

implements IPackageController
implements IDatapusher

after_create
add_default_views

after_update
add_default_views

after_datastore_upload
add_default_views

add_default_views(dtastore=True/False)

d_v = config.get()

for v in d_v;
info

if v.can_view(resource):
create_view

if info.dastore_based and datastore:
create_view
else
create_view
```