Skip to content

Commit

Permalink
[#880] new_resource just creates a new resource
Browse files Browse the repository at this point in the history
new_resource creates a new resouce in pending and redirects to the
edit_resource action. This makes all resources trackable and deletable later.
  • Loading branch information
nigelbabu committed Aug 26, 2013
1 parent d8b9ec8 commit 5f20a3a
Showing 1 changed file with 6 additions and 99 deletions.
105 changes: 6 additions & 99 deletions ckan/controllers/package.py
Expand Up @@ -567,107 +567,14 @@ def resource_edit(self, id, resource_id, data=None, errors=None,
return render('package/resource_edit.html', extra_vars=vars)

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))
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
'''New resources are created before resource upload in the pending
state which will be made active by the edit action'''
data = {'package_id': id, 'url': 'pending', 'state': 'pending'}
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']
return render('package/new_resource.html', extra_vars=vars)
resource = get_action('resource_create')(context, data)
redirect(h.url_for(controller='package', action='resource_edit',
resource_id=resource.get('id'), id=id))

def new_metadata(self, id, data=None, errors=None, error_summary=None):
''' FIXME: This is a temporary action to allow styling of the
Expand Down

0 comments on commit 5f20a3a

Please sign in to comment.