Repository: OCHA-DAP/hdx-ckan

Issue:
Refactoring: click on submit - error
IndentationError: expected an indented block (dataset_controller.py, line 169)



Discussion:

Resolving patch:
diff --git a/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py b/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py
--- a/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py
+++ b/ckanext-hdx_package/ckanext/hdx_package/controllers/dataset_controller.py
@@ -10,6 +10,7 @@
 from genshi.template.text import NewTextTemplate
 from paste.deploy.converters import asbool
 
+
 import ckan.logic as logic
 import ckan.lib.base as base
 import ckan.lib.maintain as maintain
@@ -22,7 +23,7 @@
 import ckan.lib.datapreview as datapreview
 import ckan.lib.plugins
 import ckan.new_authz as new_authz
-
+from ckanext.hdx_theme.helpers import helpers as hdx_h
 
 from ckan.common import OrderedDict, _, json, request, c, g, response
 from ckan.controllers.home import CACHE_PARAMETERS
@@ -44,9 +45,9 @@
 flatten_to_string_key = logic.flatten_to_string_key
 
 CONTENT_TYPES = {
-	'text': 'text/plain;charset=utf-8',
-	'html': 'text/html;charset=utf-8',
-	'json': 'application/json;charset=utf-8',
+    'text': 'text/plain;charset=utf-8',
+    'html': 'text/html;charset=utf-8',
+    'json': 'application/json;charset=utf-8',
 }
 
 lookup_package_plugin = ckan.lib.plugins.lookup_package_plugin
@@ -55,122 +56,122 @@
 
 class DatasetController(PackageController):
 
-	def _finish(self, status_int, response_data=None,
-				content_type='text'):
-		'''When a controller method has completed, call this method
-		to prepare the response.
-		@return response message - return this value from the controller
-								   method
-				 e.g. return self._finish(404, 'Package not found')
-		'''
-		assert(isinstance(status_int, int))
-		response.status_int = status_int
-		response_msg = ''
-		if response_data is not None:
-			response.headers['Content-Type'] = CONTENT_TYPES[content_type]
-			if content_type == 'json':
-				response_msg = h.json.dumps(response_data)
-			else:
-				response_msg = response_data
-			# Support "JSONP" callback.
-			if status_int == 200 and 'callback' in request.params and \
-				(request.method == 'GET' or
-				 c.logic_function and request.method == 'POST'):
-				# escape callback to remove '<', '&', '>' chars
-				callback = cgi.escape(request.params['callback'])
-				response_msg = self._wrap_jsonp(callback, response_msg)
-		return response_msg
-
-	def _save_new(self, context, package_type=None):
-		# The staged add dataset used the new functionality when the dataset is
-		# partially created so we need to know if we actually are updating or
-		# this is a real new.
-		is_an_update = False
-		ckan_phase = request.params.get('_ckan_phase')
-		from ckan.lib.search import SearchIndexError
-		try:
-			data_dict = clean_dict(dict_fns.unflatten(
-				tuplize_dict(parse_params(request.POST))))
-			if ckan_phase:
-				# prevent clearing of groups etc
-				context['allow_partial_update'] = True
-				# sort the tags
-				data_dict['tags'] = self._tag_string_to_list(
-					data_dict['tag_string'])
-				if data_dict.get('pkg_name'):
-					is_an_update = True
-					# This is actually an update not a save
-					data_dict['id'] = data_dict['pkg_name']
-					del data_dict['pkg_name']
-					# this is actually an edit not a save
-					pkg_dict = get_action('package_update')(context, data_dict)
-
-					if request.params['save'] == 'go-metadata':
-						# redirect to add metadata
-						url = h.url_for(controller='package',
-										action='new_metadata',
-										id=pkg_dict['name'])
-					else:
-						# redirect to add dataset resources
-						url = h.url_for(controller='package',
-										action='new_resource',
-										id=pkg_dict['name'])
-					redirect(url)
-				# Make sure we don't index this dataset
-				if request.params['save'] not in ['go-resource', 'go-metadata']:
-					data_dict['state'] = 'draft'
-				# allow the state to be changed
-				context['allow_state_change'] = True
-
-			data_dict['type'] = package_type
-			context['message'] = data_dict.get('log_message', '')
-			print data_dict
-			pkg_dict = get_action('package_create')(context, data_dict)
-
-			#A hack to handle the metadata correctly
-			data_dict['id'] = pkg_dict['id']
-			pkg_dict = get_action('package_update')(context, data_dict)
-
-			if ckan_phase:
-				print 'World'
-				# redirect to add dataset resources
-				url = h.url_for(controller='package',
-								action='new_resource',
-								id=pkg_dict['name'])
-				redirect(url)
-
-			self._form_save_redirect(pkg_dict['name'], 'new', package_type=package_type)
-		except NotAuthorized:
-			abort(401, _('Unauthorized to read package %s') % '')
-		except NotFound, e:
-			abort(404, _('Dataset not found'))
-		except dict_fns.DataError:
-			abort(400, _(u'Integrity Error'))
-		except SearchIndexError, e:
-			try:
-				exc_str = unicode(repr(e.args))
-			except Exception:  # We don't like bare excepts
-				exc_str = unicode(str(e))
-			abort(500, _(u'Unable to add package to search index.') + exc_str)
-		except ValidationError, e:
-			errors = e.error_dict
-			error_summary = e.error_summary
-			if is_an_update:
-				# we need to get the state of the dataset to show the stage we
-				# are on.
-				pkg_dict = get_action('package_show')(context, data_dict)
-				data_dict['state'] = pkg_dict['state']
-				return self.edit(data_dict['id'], data_dict,
-								 errors, error_summary)
-			data_dict['state'] = 'none'
-			return self.new(data_dict, errors, error_summary)
-
-	def contribute(self, error=None):
+    def _finish(self, status_int, response_data=None,
+                content_type='text'):
+        '''When a controller method has completed, call this method
+        to prepare the response.
+        @return response message - return this value from the controller
+                                   method
+                 e.g. return self._finish(404, 'Package not found')
+        '''
+        assert(isinstance(status_int, int))
+        response.status_int = status_int
+        response_msg = ''
+        if response_data is not None:
+            response.headers['Content-Type'] = CONTENT_TYPES[content_type]
+            if content_type == 'json':
+                response_msg = h.json.dumps(response_data)
+            else:
+                response_msg = response_data
+            # Support "JSONP" callback.
+            if status_int == 200 and 'callback' in request.params and \
+                (request.method == 'GET' or
+                 c.logic_function and request.method == 'POST'):
+                # escape callback to remove '<', '&', '>' chars
+                callback = cgi.escape(request.params['callback'])
+                response_msg = self._wrap_jsonp(callback, response_msg)
+        return response_msg
+
+    def _save_new(self, context, package_type=None):
+        # The staged add dataset used the new functionality when the dataset is
+        # partially created so we need to know if we actually are updating or
+        # this is a real new.
+        is_an_update = False
+        ckan_phase = request.params.get('_ckan_phase')
+        from ckan.lib.search import SearchIndexError
+        try:
+            data_dict = clean_dict(dict_fns.unflatten(
+                tuplize_dict(parse_params(request.POST))))
+            if ckan_phase:
+                # prevent clearing of groups etc
+                context['allow_partial_update'] = True
+                # sort the tags
+                data_dict['tags'] = self._tag_string_to_list(
+                    data_dict['tag_string'])
+                if data_dict.get('pkg_name'):
+                    is_an_update = True
+                    # This is actually an update not a save
+                    data_dict['id'] = data_dict['pkg_name']
+                    del data_dict['pkg_name']
+                    # this is actually an edit not a save
+                    pkg_dict = get_action('package_update')(context, data_dict)
+
+                    if request.params['save'] == 'go-metadata':
+                        # redirect to add metadata
+                        url = h.url_for(controller='package',
+                                        action='new_metadata',
+                                        id=pkg_dict['name'])
+                    else:
+                        # redirect to add dataset resources
+                        url = h.url_for(controller='package',
+                                        action='new_resource',
+                                        id=pkg_dict['name'])
+                    redirect(url)
+                # Make sure we don't index this dataset
+                if request.params['save'] not in ['go-resource', 'go-metadata']:
+                    data_dict['state'] = 'draft'
+                # allow the state to be changed
+                context['allow_state_change'] = True
+
+            data_dict['type'] = package_type
+            context['message'] = data_dict.get('log_message', '')
+            print data_dict
+            pkg_dict = get_action('package_create')(context, data_dict)
+
+            #A hack to handle the metadata correctly
+            data_dict['id'] = pkg_dict['id']
+            pkg_dict = get_action('package_update')(context, data_dict)
+
+            if ckan_phase:
+                print 'World'
+                # redirect to add dataset resources
+                url = h.url_for(controller='package',
+                                action='new_resource',
+                                id=pkg_dict['name'])
+                redirect(url)
+
+            self._form_save_redirect(pkg_dict['name'], 'new', package_type=package_type)
+        except NotAuthorized:
+            abort(401, _('Unauthorized to read package %s') % '')
+        except NotFound, e:
+            abort(404, _('Dataset not found'))
+        except dict_fns.DataError:
+            abort(400, _(u'Integrity Error'))
+        except SearchIndexError, e:
+            try:
+                exc_str = unicode(repr(e.args))
+            except Exception:  # We don't like bare excepts
+                exc_str = unicode(str(e))
+            abort(500, _(u'Unable to add package to search index.') + exc_str)
+        except ValidationError, e:
+            errors = e.error_dict
+            error_summary = e.error_summary
+            if is_an_update:
+                # we need to get the state of the dataset to show the stage we
+                # are on.
+                pkg_dict = get_action('package_show')(context, data_dict)
+                data_dict['state'] = pkg_dict['state']
+                return self.edit(data_dict['id'], data_dict,
+                                 errors, error_summary)
+            data_dict['state'] = 'none'
+            return self.new(data_dict, errors, error_summary)
+
+    def contribute(self, error=None):
         self.login(error)
         vars    = {'contribute':True}
         return render('user/login.html', extra_vars=vars)
 
-	def preselect(self):
+    def preselect(self):
         c.am_sysadmin = new_authz.is_sysadmin(c.user)
         c.organizations_available = hdx_h.hdx_organizations_available_with_roles()
         if c.organizations_available and len(c.organizations_available) > 0:
@@ -178,262 +179,262 @@ def preselect(self):
         else:
             return base.render('organization/request_mem_or_org.html')
 
-	def new(self, data=None, errors=None, error_summary=None):
-		#Is the user a member of any orgs? If not make them join one first
-		try:
-			user_orgs = helpers.hdx_user_org_num(c.userobj.id)
-			if len(user_orgs) == 0:
-				return render('organization/request_mem_or_org.html')
-			#If there's an org and the user is not a member of this org redirect back to org select
-				this_org = request.params['organization_id']
-				if this_org in user_orgs:
-					return render('organization/request_mem_or_org.html')
-		except:
-			return render('user/login.html', extra_vars={'contribute':True})
-
-		package_type = self._guess_package_type(True)
-
-		context = {'model': model, 'session': model.Session,
-				   'user': c.user or c.author,
-				   'save': 'save' in request.params}
-
-		# Package needs to have a organization group in the call to
-		# check_access and also to save it
-		try:
-			check_access('package_create', context)
-		except NotAuthorized:
-			abort(401, _('Unauthorized to create a package'))
-
-		if context['save'] and not data:
-			return self._save_new(context, package_type=package_type)
-
-		data = data or clean_dict(dict_fns.unflatten(tuplize_dict(parse_params(
-			request.params, ignore_keys=CACHE_PARAMETERS))))
-		c.resources_json = h.json.dumps(data.get('resources', []))
-		# convert tags if not supplied in data
-		if data and not data.get('tag_string'):
-			data['tag_string'] = ', '.join(
-				h.dict_list_reduce(data.get('tags', {}), 'name'))
-
-		errors = errors or {}
-		error_summary = error_summary or {}
-		# in the phased add dataset we need to know that
-		# we have already completed stage 1
-		stage = ['inactive']
-		if data.get('state') == 'draft':
-			stage = ['inactive', 'complete']
-		elif data.get('state') == 'draft-complete':
-			stage = ['inactive', 'complete', 'complete']
-
-		# if we are creating from a group then this allows the group to be
-		# set automatically
-		data['group_id'] = request.params.get('group') or \
-			request.params.get('groups__0__id')
-
-		vars = {'data': data, 'errors': errors,
-				'error_summary': error_summary,
-				'action': 'new', 'stage': stage, 'validation_fail':0}
-		c.errors_json = h.json.dumps(errors)
-
-		self._setup_template_variables(context, {},
-									   package_type=package_type)
-
-		# TODO: This check is to maintain backwards compatibility with the
-		# old way of creating custom forms. This behaviour is now deprecated.
-		if hasattr(self, 'package_form'):
-			c.form = render(self.package_form, extra_vars=vars)
-		else:
-			print self._package_form(package_type=package_type)
-			c.form = render(self._package_form(package_type=package_type),
-							extra_vars=vars)
-		
-		if not request.is_xhr:
-			return render(self._new_template(package_type), extra_vars={'stage': stage})
-		else:
-			return self._finish(200, {'validation_fail':1, 'errors':vars['errors'], 'error_summary':vars['error_summary']}, content_type='json')
-		#return render(self._new_template(package_type), extra_vars={'stage': stage})
-		
-
-	def new_resource(self, id, data=None, errors=None, error_summary=None):
-		''' FIXME: This is a temporary action to allow styling of the
-		forms. '''
-		if request.method == 'POST' and not data:
-			save_action = request.params.get('save')
-			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']
-			resource_id = data['id']
-			del data['id']
-
-			context = {'model': model, 'session': model.Session,
-					   'user': c.user or c.author}
-
-			# see if we have any data that we are trying to save
-			data_provided = False
-			for key, value in data.iteritems():
-				if value and key != 'resource_type':
-					data_provided = True
-					break
-
-			if not data_provided and save_action != "go-dataset-complete":
-				if save_action == 'go-dataset':
-					# go to final stage of adddataset
-					redirect(h.url_for(controller='package',
-									   action='edit', id=id))
-				# see if we have added any resources
-				try:
-					data_dict = get_action('package_show')(context, {'id': id})
-
-				except NotAuthorized:
-					abort(401, _('Unauthorized to update dataset'))
-				except NotFound:
-					abort(404,
-					  _('The dataset {id} could not be found.').format(id=id))
-				if not len(data_dict['resources']):
-					# no data so keep on page
-					msg = _('You must add at least one data resource')
-					# On new templates do not use flash message
-					if g.legacy_templates:
-						h.flash_error(msg)
-						redirect(h.url_for(controller='package',
-										   action='new_resource', id=id))
-					else:
-						errors = {}
-						error_summary = {_('Error'): msg}
-						return self.new_resource(id, data, errors, error_summary)
-				# we have a resource so let them add metadata
-				redirect(h.url_for(controller='package',
-								   action='new_metadata', id=id))
-
-			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.new_resource(id, data, errors, error_summary)
-			except NotAuthorized:
-				abort(401, _('Unauthorized to create a resource'))
-			except NotFound:
-				abort(404,
-					_('The dataset {id} could not be found.').format(id=id))
-			if save_action == 'go-metadata':
-				# go to final stage of add dataset
-				redirect(h.url_for(controller='package',
-								   action='new_metadata', id=id))
-			elif save_action == 'go-dataset':
-				# go to first stage of add dataset
-				redirect(h.url_for(controller='package',
-								   action='edit', id=id))
-			elif save_action == 'go-dataset-complete':
-				# go to first stage of add dataset
-				redirect(h.url_for(controller='package',
-								   action='read', id=id))
-			elif save_action == 'finish':
-				redirect(h.url_for(controller='package',
-								   action='read', id=id))
-			else:
-				# add more resources
-				redirect(h.url_for(controller='package',
-								   action='new_resource', id=id))
-		errors = errors or {}
-		error_summary = error_summary or {}
-		vars = {'data': data, 'errors': errors,
-				'error_summary': error_summary, 'action': 'new'}
-		vars['pkg_name'] = id
-		# get resources for sidebar
-		context = {'model': model, 'session': model.Session,
-				   'user': c.user or c.author}
-		try:
-			pkg_dict = get_action('package_show')(context, {'id': id})
-		except NotFound:
-			abort(404, _('The dataset {id} could not be found.').format(id=id))
-		# required for nav menu
-		vars['pkg_dict'] = pkg_dict
-		if pkg_dict['state'] == 'draft':
-			vars['stage'] = ['complete', 'active']
-		elif pkg_dict['state'] == 'draft-complete':
-			vars['stage'] = ['complete', 'active', 'complete']
-		
-		if not request.is_xhr:
-			return render('package/new_resource.html', extra_vars=vars)
-		else:
-			##Adding url for easy update
-			vars['action_url'] = h.url_for(controller='package', action='new_resource', id=vars['pkg_name'])
-			return self._finish(200, vars, content_type='json')
-
-
-	def new_metadata(self, id, data=None, errors=None, error_summary=None):
-		''' FIXME: This is a temporary action to allow styling of the
-		forms. '''
-		context = {'model': model, 'session': model.Session,
-				   'user': c.user or c.author}
-
-		if request.method == 'POST' and not data:
-			save_action = request.params.get('save')
-			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']
-
-			data_dict = get_action('package_show')(context, {'id': id})
-
-			data_dict['id'] = id
-			# update the state
-			if save_action == 'finish' or save_action == 'finish-ajax':
-				# we want this to go live when saved
-				data_dict['state'] = 'active'
-			elif save_action in ['go-resources', 'go-dataset']:
-				data_dict['state'] = 'draft-complete'
-			# allow the state to be changed
-			context['allow_state_change'] = True
-			data_dict.update(data)
-			try:
-				get_action('package_update')(context, data_dict)
-			except ValidationError, e:
-				errors = e.error_dict
-				error_summary = e.error_summary
-				return self.new_metadata(id, data, errors, error_summary)
-			except NotAuthorized:
-				abort(401, _('Unauthorized to update dataset'))
-			if save_action == 'go-resources' or 'finish-ajax':
-				# we want to go back to the add resources form stage
-				redirect(h.url_for(controller='package',
-								   action='new_resource', id=id))
-			elif save_action == 'go-dataset':
-				# we want to go back to the add dataset stage
-				redirect(h.url_for(controller='package',
-								   action='edit', id=id))
-			elif save_action == 'go-dataset-complete':
-				# go to first stage of add dataset
-				redirect(h.url_for(controller='package',
-								   action='read', id=id))
-			redirect(h.url_for(controller='package', action='read', id=id))
-
-		if not data:
-			data = get_action('package_show')(context, {'id': id})
-		errors = errors or {}
-		error_summary = error_summary or {}
-		vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
-		vars['pkg_name'] = id
-
-		package_type = self._get_package_type(id)
-		self._setup_template_variables(context, {},
-									   package_type=package_type)
-
-		if not request.is_xhr:
-			return render('package/new_package_metadata.html', extra_vars=vars)
-		else:
-			vars['action_url'] = h.url_for(controller='package', action='new_metadata', id=vars['pkg_name'])
-			return self._finish(200, vars, content_type='json')
-
-
-	def resource_edit(self, id, resource_id, data=None, errors=None,
+    def new(self, data=None, errors=None, error_summary=None):
+        #Is the user a member of any orgs? If not make them join one first
+        try:
+            user_orgs = helpers.hdx_user_org_num(c.userobj.id)
+            if len(user_orgs) == 0:
+                return render('organization/request_mem_or_org.html')
+            #If there's an org and the user is not a member of this org redirect back to org select
+                this_org = request.params['organization_id']
+                if this_org in user_orgs:
+                    return render('organization/request_mem_or_org.html')
+        except:
+            return render('user/login.html', extra_vars={'contribute':True})
+
+        package_type = self._guess_package_type(True)
+
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author,
+                   'save': 'save' in request.params}
+
+        # Package needs to have a organization group in the call to
+        # check_access and also to save it
+        try:
+            check_access('package_create', context)
+        except NotAuthorized:
+            abort(401, _('Unauthorized to create a package'))
+
+        if context['save'] and not data:
+            return self._save_new(context, package_type=package_type)
+
+        data = data or clean_dict(dict_fns.unflatten(tuplize_dict(parse_params(
+            request.params, ignore_keys=CACHE_PARAMETERS))))
+        c.resources_json = h.json.dumps(data.get('resources', []))
+        # convert tags if not supplied in data
+        if data and not data.get('tag_string'):
+            data['tag_string'] = ', '.join(
+                h.dict_list_reduce(data.get('tags', {}), 'name'))
+
+        errors = errors or {}
+        error_summary = error_summary or {}
+        # in the phased add dataset we need to know that
+        # we have already completed stage 1
+        stage = ['inactive']
+        if data.get('state') == 'draft':
+            stage = ['inactive', 'complete']
+        elif data.get('state') == 'draft-complete':
+            stage = ['inactive', 'complete', 'complete']
+
+        # if we are creating from a group then this allows the group to be
+        # set automatically
+        data['group_id'] = request.params.get('group') or \
+            request.params.get('groups__0__id')
+
+        vars = {'data': data, 'errors': errors,
+                'error_summary': error_summary,
+                'action': 'new', 'stage': stage, 'validation_fail':0}
+        c.errors_json = h.json.dumps(errors)
+
+        self._setup_template_variables(context, {},
+                                       package_type=package_type)
+
+        # TODO: This check is to maintain backwards compatibility with the
+        # old way of creating custom forms. This behaviour is now deprecated.
+        if hasattr(self, 'package_form'):
+            c.form = render(self.package_form, extra_vars=vars)
+        else:
+            print self._package_form(package_type=package_type)
+            c.form = render(self._package_form(package_type=package_type),
+                            extra_vars=vars)
+        
+        if not request.is_xhr:
+            return render(self._new_template(package_type), extra_vars={'stage': stage})
+        else:
+            return self._finish(200, {'validation_fail':1, 'errors':vars['errors'], 'error_summary':vars['error_summary']}, content_type='json')
+        #return render(self._new_template(package_type), extra_vars={'stage': stage})
+        
+
+    def new_resource(self, id, data=None, errors=None, error_summary=None):
+        ''' FIXME: This is a temporary action to allow styling of the
+        forms. '''
+        if request.method == 'POST' and not data:
+            save_action = request.params.get('save')
+            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']
+            resource_id = data['id']
+            del data['id']
+
+            context = {'model': model, 'session': model.Session,
+                       'user': c.user or c.author}
+
+            # see if we have any data that we are trying to save
+            data_provided = False
+            for key, value in data.iteritems():
+                if value and key != 'resource_type':
+                    data_provided = True
+                    break
+
+            if not data_provided and save_action != "go-dataset-complete":
+                if save_action == 'go-dataset':
+                    # go to final stage of adddataset
+                    redirect(h.url_for(controller='package',
+                                       action='edit', id=id))
+                # see if we have added any resources
+                try:
+                    data_dict = get_action('package_show')(context, {'id': id})
+
+                except NotAuthorized:
+                    abort(401, _('Unauthorized to update dataset'))
+                except NotFound:
+                    abort(404,
+                      _('The dataset {id} could not be found.').format(id=id))
+                if not len(data_dict['resources']):
+                    # no data so keep on page
+                    msg = _('You must add at least one data resource')
+                    # On new templates do not use flash message
+                    if g.legacy_templates:
+                        h.flash_error(msg)
+                        redirect(h.url_for(controller='package',
+                                           action='new_resource', id=id))
+                    else:
+                        errors = {}
+                        error_summary = {_('Error'): msg}
+                        return self.new_resource(id, data, errors, error_summary)
+                # we have a resource so let them add metadata
+                redirect(h.url_for(controller='package',
+                                   action='new_metadata', id=id))
+
+            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.new_resource(id, data, errors, error_summary)
+            except NotAuthorized:
+                abort(401, _('Unauthorized to create a resource'))
+            except NotFound:
+                abort(404,
+                    _('The dataset {id} could not be found.').format(id=id))
+            if save_action == 'go-metadata':
+                # go to final stage of add dataset
+                redirect(h.url_for(controller='package',
+                                   action='new_metadata', id=id))
+            elif save_action == 'go-dataset':
+                # go to first stage of add dataset
+                redirect(h.url_for(controller='package',
+                                   action='edit', id=id))
+            elif save_action == 'go-dataset-complete':
+                # go to first stage of add dataset
+                redirect(h.url_for(controller='package',
+                                   action='read', id=id))
+            elif save_action == 'finish':
+                redirect(h.url_for(controller='package',
+                                   action='read', id=id))
+            else:
+                # add more resources
+                redirect(h.url_for(controller='package',
+                                   action='new_resource', id=id))
+        errors = errors or {}
+        error_summary = error_summary or {}
+        vars = {'data': data, 'errors': errors,
+                'error_summary': error_summary, 'action': 'new'}
+        vars['pkg_name'] = id
+        # get resources for sidebar
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author}
+        try:
+            pkg_dict = get_action('package_show')(context, {'id': id})
+        except NotFound:
+            abort(404, _('The dataset {id} could not be found.').format(id=id))
+        # required for nav menu
+        vars['pkg_dict'] = pkg_dict
+        if pkg_dict['state'] == 'draft':
+            vars['stage'] = ['complete', 'active']
+        elif pkg_dict['state'] == 'draft-complete':
+            vars['stage'] = ['complete', 'active', 'complete']
+        
+        if not request.is_xhr:
+            return render('package/new_resource.html', extra_vars=vars)
+        else:
+            ##Adding url for easy update
+            vars['action_url'] = h.url_for(controller='package', action='new_resource', id=vars['pkg_name'])
+            return self._finish(200, vars, content_type='json')
+
+
+    def new_metadata(self, id, data=None, errors=None, error_summary=None):
+        ''' FIXME: This is a temporary action to allow styling of the
+        forms. '''
+        context = {'model': model, 'session': model.Session,
+                   'user': c.user or c.author}
+
+        if request.method == 'POST' and not data:
+            save_action = request.params.get('save')
+            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']
+
+            data_dict = get_action('package_show')(context, {'id': id})
+
+            data_dict['id'] = id
+            # update the state
+            if save_action == 'finish' or save_action == 'finish-ajax':
+                # we want this to go live when saved
+                data_dict['state'] = 'active'
+            elif save_action in ['go-resources', 'go-dataset']:
+                data_dict['state'] = 'draft-complete'
+            # allow the state to be changed
+            context['allow_state_change'] = True
+            data_dict.update(data)
+            try:
+                get_action('package_update')(context, data_dict)
+            except ValidationError, e:
+                errors = e.error_dict
+                error_summary = e.error_summary
+                return self.new_metadata(id, data, errors, error_summary)
+            except NotAuthorized:
+                abort(401, _('Unauthorized to update dataset'))
+            if save_action == 'go-resources' or 'finish-ajax':
+                # we want to go back to the add resources form stage
+                redirect(h.url_for(controller='package',
+                                   action='new_resource', id=id))
+            elif save_action == 'go-dataset':
+                # we want to go back to the add dataset stage
+                redirect(h.url_for(controller='package',
+                                   action='edit', id=id))
+            elif save_action == 'go-dataset-complete':
+                # go to first stage of add dataset
+                redirect(h.url_for(controller='package',
+                                   action='read', id=id))
+            redirect(h.url_for(controller='package', action='read', id=id))
+
+        if not data:
+            data = get_action('package_show')(context, {'id': id})
+        errors = errors or {}
+        error_summary = error_summary or {}
+        vars = {'data': data, 'errors': errors, 'error_summary': error_summary}
+        vars['pkg_name'] = id
+
+        package_type = self._get_package_type(id)
+        self._setup_template_variables(context, {},
+                                       package_type=package_type)
+
+        if not request.is_xhr:
+            return render('package/new_package_metadata.html', extra_vars=vars)
+        else:
+            vars['action_url'] = h.url_for(controller='package', action='new_metadata', id=vars['pkg_name'])
+            return self._finish(200, vars, content_type='json')
+
+
+    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(


Test patch: