Repository: OCHA-DAP/hdx-ckan

Issue:
Dataset Edit: Saving an edit to a File redirects to preview page
We have been hiding the preview page for datasets like xlsx that don't  have a useful preview.  

Change the redirect after saving an edit to a File to go back to the Dataset View page.



Discussion:

Resolving patch:
diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/package_controller.py b/ckanext-hdx_theme/ckanext/hdx_theme/package_controller.py
new file mode 100644
--- /dev/null
+++ b/ckanext-hdx_theme/ckanext/hdx_theme/package_controller.py
@@ -0,0 +1,111 @@
+import logging
+from urllib import urlencode
+import datetime
+import os
+import mimetypes
+import cgi
+
+from pylons import config
+from genshi.template import MarkupTemplate
+from genshi.template.text import NewTextTemplate
+from paste.deploy.converters import asbool
+import paste.fileapp
+from ckan.controllers.package import PackageController
+
+import ckan.logic as logic
+import ckan.lib.base as base
+import ckan.lib.maintain as maintain
+import ckan.lib.package_saver as package_saver
+import ckan.lib.i18n as i18n
+import ckan.lib.navl.dictization_functions as dict_fns
+import ckan.lib.accept as accept
+import ckan.lib.helpers as h
+import ckan.model as model
+import ckan.lib.datapreview as datapreview
+import ckan.lib.plugins
+import ckan.lib.uploader as uploader
+import ckan.plugins as p
+import ckan.lib.render
+
+from ckan.common import OrderedDict, _, json, request, c, g, response
+
+log = logging.getLogger(__name__)
+
+render = base.render
+abort = base.abort
+redirect = base.redirect
+
+NotFound = logic.NotFound
+NotAuthorized = logic.NotAuthorized
+ValidationError = logic.ValidationError
+check_access = logic.check_access
+get_action = logic.get_action
+tuplize_dict = logic.tuplize_dict
+clean_dict = logic.clean_dict
+parse_params = logic.parse_params
+flatten_to_string_key = logic.flatten_to_string_key
+
+lookup_package_plugin = ckan.lib.plugins.lookup_package_plugin
+
+class HDXPackageController(PackageController):
+    def resource_edit(self, id, resource_id, data=None, errors=None,
+                      error_summary=None):
+        if request.method == 'POST' and not data:
+            data = data or clean_dict(dict_fns.unflatten(tuplize_dict(parse_params(
+                request.POST))))
+            # we don't want to include save as it is part of the form
+            del data['save']
+
+            context = {'model': model, 'session': model.Session,
+                       'api_version': 3, 'for_edit': True,
+                       'user': c.user or c.author, 'auth_user_obj': c.userobj}
+
+            data['package_id'] = id
+            try:
+                if resource_id:
+                    data['id'] = resource_id
+                    get_action('resource_update')(context, data)
+                else:
+                    get_action('resource_create')(context, data)
+            except ValidationError, e:
+                errors = e.error_dict
+                error_summary = e.error_summary
+                return self.resource_edit(id, resource_id, data,
+                                          errors, error_summary)
+            except NotAuthorized:
+                abort(401, _('Unauthorized to edit this resource'))
+            redirect(h.url_for(controller='package', action='resources',
+                               id=id))
+
+        context = {'model': model, 'session': model.Session,
+                   'api_version': 3, 'for_edit': True,
+                   'user': c.user or c.author, 'auth_user_obj': c.userobj}
+        pkg_dict = get_action('package_show')(context, {'id': id})
+        if pkg_dict['state'].startswith('draft'):
+            # dataset has not yet been fully created
+            resource_dict = get_action('resource_show')(context, {'id': resource_id})
+            fields = ['url', 'resource_type', 'format', 'name', 'description', 'id']
+            data = {}
+            for field in fields:
+                data[field] = resource_dict[field]
+            return self.new_resource(id, data=data)
+        # resource is fully created
+        try:
+            resource_dict = get_action('resource_show')(context, {'id': resource_id})
+        except NotFound:
+            abort(404, _('Resource not found'))
+        c.pkg_dict = pkg_dict
+        c.resource = resource_dict
+        # set the form action
+        c.form_action = h.url_for(controller='package',
+                                  action='resource_edit',
+                                  resource_id=resource_id,
+                                  id=id)
+        if not data:
+            data = resource_dict
+
+        errors = errors or {}
+        error_summary = error_summary or {}
+        vars = {'data': data, 'errors': errors,
+                'error_summary': error_summary, 'action': 'new'}
+        return render('package/resource_edit.html', extra_vars=vars)
diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/plugin.py b/ckanext-hdx_theme/ckanext/hdx_theme/plugin.py
--- a/ckanext-hdx_theme/ckanext/hdx_theme/plugin.py
+++ b/ckanext-hdx_theme/ckanext/hdx_theme/plugin.py
@@ -60,6 +60,9 @@ def before_map(self, map):
         map.connect('/organization/member_new/{id}', controller='ckanext.hdx_theme.member_controller:HDXOrgMemberController', action='member_new')
 
         map.connect('/about/{page}', controller='ckanext.hdx_theme.splash_page:SplashPageController', action='about')
+
+        map.connect('resource_edit', '/dataset/{id}/resource_edit/{resource_id}', controller='ckanext.hdx_theme.package_controller:HDXPackageController', action='resource_edit', ckan_icon='edit')
+
         return map
     
     def create(self, entity):


Test patch: