diff --git a/.gitignore b/.gitignore index e9876d1..467fe2e 100644 --- a/.gitignore +++ b/.gitignore @@ -39,4 +39,6 @@ nosetests.xml coverage.xml # Sphinx documentation -docs/_build/ \ No newline at end of file +docs/_build/ + +.DS_Store diff --git a/README.rst b/README.rst index 2f86e07..255d8c5 100644 --- a/README.rst +++ b/README.rst @@ -2,7 +2,7 @@ ckanext-officedocs ============= -This plugin provides the option of using the Microsoft Office viewer for +This plugin provides the option of using the `Microsoft Online Doc Viewer `_ for previewing both MS Office and OpenOffice documents as an IResourceView ------------ @@ -31,11 +31,17 @@ To install ckanext-officedocs: cd ckanext-officedocs python setup.py install -3. Add ``officedocs_view`` to the ``ckan.plugins`` setting in your CKAN +4. Add ``officedocs_view`` to the ``ckan.plugins`` setting in your CKAN config file (by default the config file is located at ``/etc/ckan/default/production.ini``). + +5. If you wish for views to be created automatically for you, then you should + add ``officedocs_view`` to the end of the ``ckan.views.default_views`` option in your + config file. -4. Restart CKAN. For example if you've deployed CKAN with Apache on Ubuntu:: + ckan.views.default_views = ... officedocs_view + +6. Restart CKAN. For example if you've deployed CKAN with Apache on Ubuntu:: sudo service apache2 reload diff --git a/ckanext/officedocs/plugin.py b/ckanext/officedocs/plugin.py index b61c2fb..a8d3e73 100644 --- a/ckanext/officedocs/plugin.py +++ b/ckanext/officedocs/plugin.py @@ -1,5 +1,6 @@ import ckan.plugins as plugins import ckan.plugins.toolkit as toolkit +from six.moves.urllib.parse import quote_plus class OfficeDocsPlugin(plugins.SingletonPlugin): @@ -22,7 +23,6 @@ def info(self): } def setup_template_variables(self, context, data_dict): - from urllib import quote_plus url = quote_plus(data_dict["resource"]["url"]) private_package = data_dict["package"]["private"] return { @@ -34,10 +34,10 @@ def can_view(self, data_dict): supported_formats = [ "DOC", "DOCX", "XLS", "XLSX", "PPT", "PPTX", "PPS", "ODT", "ODS", "ODP" ] - try: - return data_dict['resource'].get('format', '').upper() in supported_formats - except: - return False + format_upper = data_dict['resource'].get('format', '').upper() + if format_upper in supported_formats: + return True + return False def view_template(self, context, data_dict): return "officedocs/preview.html" diff --git a/ckanext/officedocs/tests/test_plugin.py b/ckanext/officedocs/tests/test_plugin.py deleted file mode 100644 index 631778a..0000000 --- a/ckanext/officedocs/tests/test_plugin.py +++ /dev/null @@ -1,5 +0,0 @@ -"""Tests for plugin.py.""" -import ckanext.gdoc.plugin as plugin - -def test_plugin(): - pass \ No newline at end of file diff --git a/ckanext/officedocs/tests/test_view.py b/ckanext/officedocs/tests/test_view.py new file mode 100644 index 0000000..09aab60 --- /dev/null +++ b/ckanext/officedocs/tests/test_view.py @@ -0,0 +1,24 @@ +import ckan.plugins as p +from ckan.tests import factories + +def test_view_on_resource_page(): + sysadmin = factories.Sysadmin() + dataset = factories.Dataset() + resource = factories.Resource( + package_id = dataset['id'], + url = 'http://link.to.some.data', + format = 'XLS' + ) + resource_view = factories.ResourceView( + resource_id = resource['id'], + title = 'Preview', + view_type = 'officedocs_view' + ) + + response = p.toolkit.get_action('resource_view_show')( + {'user': sysadmin.get('name')}, + {'id': resource_view.get('id')} + ) + + assert response.get('title') == 'Preview' + assert response.get('view_type') == 'officedocs_view' diff --git a/test.ini b/test.ini index c7c70e7..d64bd7f 100644 --- a/test.ini +++ b/test.ini @@ -13,6 +13,7 @@ use = config:../ckan/test-core.ini # Insert any custom config settings to be used when running your extension's # tests here. +ckan.plugins = officedocs_view # Logging configuration